-
-
Notifications
You must be signed in to change notification settings - Fork 680
/
MbStrrposEncodingArgumentPositionRector.php
60 lines (58 loc) · 1.85 KB
/
MbStrrposEncodingArgumentPositionRector.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
<?php
declare (strict_types=1);
namespace Rector\Php74\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\LNumber;
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 \Rector\Tests\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector\MbStrrposEncodingArgumentPositionRectorTest
*/
final class MbStrrposEncodingArgumentPositionRector extends AbstractRector implements MinPhpVersionInterface
{
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::CHANGE_MB_STRPOS_ARG_POSITION;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Change mb_strrpos() encoding argument position', [new CodeSample('mb_strrpos($text, "abc", "UTF-8");', 'mb_strrpos($text, "abc", 0, "UTF-8");')]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node) : ?Node
{
if (!$this->isName($node, 'mb_strrpos')) {
return null;
}
if (!isset($node->args[2])) {
return null;
}
if (isset($node->args[3])) {
return null;
}
if (!$node->args[2] instanceof Arg) {
return null;
}
$secondArgType = $this->getType($node->args[2]->value);
if ($secondArgType->isInteger()->yes()) {
return null;
}
$node->args[3] = $node->args[2];
$node->args[2] = new Arg(new LNumber(0));
return $node;
}
}