2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-12-31 09:40:51 +00:00
|
|
|
|
2019-09-22 18:57:03 +00:00
|
|
|
namespace Rector\Php74\Rector\FuncCall;
|
2018-12-31 09:40:51 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Scalar\LNumber;
|
|
|
|
use PHPStan\Type\IntegerType;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-12-31 09:40:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://wiki.php.net/rfc/deprecations_php_7_4 (not confirmed yet)
|
|
|
|
* @see https://3v4l.org/kLdtB
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector\MbStrrposEncodingArgumentPositionRectorTest
|
2018-12-31 09:40:51 +00:00
|
|
|
*/
|
|
|
|
final class MbStrrposEncodingArgumentPositionRector extends AbstractRector
|
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2018-12-31 09:40:51 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition(
|
|
|
|
'Change mb_strrpos() encoding argument position',
|
|
|
|
[new CodeSample('mb_strrpos($text, "abc", "UTF-8");', 'mb_strrpos($text, "abc", 0, "UTF-8");')]);
|
2018-12-31 09:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-31 09:40:51 +00:00
|
|
|
*/
|
|
|
|
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 ($this->getStaticType($node->args[2]->value) instanceof IntegerType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$node->args[3] = $node->args[2];
|
|
|
|
$node->args[2] = new Arg(new LNumber(0));
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|