-
-
Notifications
You must be signed in to change notification settings - Fork 688
/
Utf8DecodeEncodeToMbConvertEncodingRector.php
66 lines (64 loc) · 2.07 KB
/
Utf8DecodeEncodeToMbConvertEncodingRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare (strict_types=1);
namespace Rector\Php82\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://3v4l.org/Q14UR
* @see \Rector\Tests\Php82\Rector\FuncCall\Utf8DecodeEncodeToMbConvertEncodingRector\Utf8DecodeEncodeToMbConvertEncodingRectorTest
*/
final class Utf8DecodeEncodeToMbConvertEncodingRector extends AbstractRector implements MinPhpVersionInterface
{
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Change deprecated utf8_decode and utf8_encode to mb_convert_encoding', [new CodeSample(<<<'CODE_SAMPLE'
utf8_decode($value);
utf8_encode($value);
CODE_SAMPLE
, <<<'CODE_SAMPLE'
mb_convert_encoding($value, 'ISO-8859-1');
mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1');
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node) : ?Node
{
if ($node->isFirstClassCallable()) {
return null;
}
if ($this->isName($node, 'utf8_decode')) {
$node->name = new Name('mb_convert_encoding');
$node->args[1] = new Arg(new String_('ISO-8859-1'));
return $node;
}
if ($this->isName($node, 'utf8_encode')) {
$node->name = new Name('mb_convert_encoding');
$node->args[1] = new Arg(new String_('UTF-8'));
$node->args[2] = new Arg(new String_('ISO-8859-1'));
return $node;
}
return null;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::DEPRECATE_UTF8_DECODE_ENCODE_FUNCTION;
}
}