2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php55\Rector\FuncCall;
|
2019-03-08 14:01:52 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
2024-04-17 10:04:21 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use Rector\Php55\RegexMatcher;
|
|
|
|
use Rector\Php72\NodeFactory\AnonymousFunctionFactory;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-03-08 14:01:52 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://wiki.php.net/rfc/remove_preg_replace_eval_modifier https://stackoverflow.com/q/19245205/1348344
|
2019-09-23 15:58:49 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php55\Rector\FuncCall\PregReplaceEModifierRector\PregReplaceEModifierRectorTest
|
2019-03-08 14:01:52 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class PregReplaceEModifierRector extends AbstractRector implements MinPhpVersionInterface
|
2019-03-08 14:01:52 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-06-10 10:46:24 +00:00
|
|
|
* @var \Rector\Php72\NodeFactory\AnonymousFunctionFactory
|
2019-03-08 14:01:52 +00:00
|
|
|
*/
|
2021-06-10 10:46:24 +00:00
|
|
|
private $anonymousFunctionFactory;
|
2020-03-25 13:15:41 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Php55\RegexMatcher
|
2020-03-25 13:15:41 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $regexMatcher;
|
2023-05-13 17:20:02 +00:00
|
|
|
public function __construct(AnonymousFunctionFactory $anonymousFunctionFactory, RegexMatcher $regexMatcher)
|
2020-07-26 07:49:22 +00:00
|
|
|
{
|
2021-06-10 10:46:24 +00:00
|
|
|
$this->anonymousFunctionFactory = $anonymousFunctionFactory;
|
2021-05-10 23:39:21 +00:00
|
|
|
$this->regexMatcher = $regexMatcher;
|
2019-03-08 14:01:52 +00:00
|
|
|
}
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::PREG_REPLACE_CALLBACK_E_MODIFIER;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-03-08 14:01:52 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('The /e modifier is no longer supported, use preg_replace_callback instead', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-03-08 14:01:52 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$comment = preg_replace('~\b(\w)(\w+)~e', '"$1".strtolower("$2")', $comment);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-03-08 14:01:52 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$comment = preg_replace_callback('~\b(\w)(\w+)~', function ($matches) {
|
|
|
|
return($matches[1].strtolower($matches[2]));
|
2021-07-28 12:55:48 +00:00
|
|
|
}, $comment);
|
2019-03-08 14:01:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-03-08 14:01:52 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-03-08 14:01:52 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-03-08 14:01:52 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [FuncCall::class];
|
2019-03-08 14:01:52 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param FuncCall $node
|
2019-03-08 14:01:52 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-03-08 14:01:52 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isName($node, 'preg_replace')) {
|
2019-03-08 14:01:52 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-13 17:20:02 +00:00
|
|
|
if ($node->isFirstClassCallable()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (\count($node->getArgs()) < 2) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-13 17:20:02 +00:00
|
|
|
$firstArgument = $node->getArgs()[0];
|
2021-09-27 15:43:15 +00:00
|
|
|
$firstArgumentValue = $firstArgument->value;
|
2022-07-18 15:25:27 +00:00
|
|
|
$patternWithoutEExpr = $this->regexMatcher->resolvePatternExpressionWithoutEIfFound($firstArgumentValue);
|
2024-04-17 10:04:21 +00:00
|
|
|
if (!$patternWithoutEExpr instanceof Expr) {
|
2019-03-08 14:01:52 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-04-17 10:04:21 +00:00
|
|
|
$secondArgument = $node->getArgs()[1];
|
|
|
|
$anonymousFunction = $this->createAnonymousFunction($secondArgument);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$anonymousFunction instanceof Closure) {
|
2019-03-08 14:01:52 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->name = new Name('preg_replace_callback');
|
2022-07-18 15:25:27 +00:00
|
|
|
$firstArgument->value = $patternWithoutEExpr;
|
2021-09-27 15:43:15 +00:00
|
|
|
$secondArgument->value = $anonymousFunction;
|
2019-03-08 14:01:52 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2024-04-17 10:04:21 +00:00
|
|
|
private function createAnonymousFunction(Arg $arg) : ?Closure
|
|
|
|
{
|
|
|
|
return $this->anonymousFunctionFactory->createAnonymousFunctionFromExpr($arg->value);
|
|
|
|
}
|
2019-03-08 14:01:52 +00:00
|
|
|
}
|