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\Php72\Rector\FuncCall;
|
2018-12-15 11:34:42 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-12-15 11:34:42 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://github.com/gueff/blogimus/commit/04086a10320595470efe446c7ddd90e602aa7228 https://github.com/pxgamer/youtube-dl-php/commit/83cb32b8b36844f2e39f82a862a5ab73da77b608
|
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php72\Rector\FuncCall\ParseStrWithResultArgumentRector\ParseStrWithResultArgumentRectorTest
|
2018-12-15 11:34:42 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ParseStrWithResultArgumentRector extends AbstractRector implements MinPhpVersionInterface
|
2018-12-15 11:34:42 +00:00
|
|
|
{
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::RESULT_ARG_IN_PARSE_STR;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-12-15 11:34:42 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Use $result argument in parse_str() function', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-12-15 11:34:42 +00:00
|
|
|
parse_str($this->query);
|
|
|
|
$data = get_defined_vars();
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-12-15 11:34:42 +00:00
|
|
|
parse_str($this->query, $result);
|
|
|
|
$data = $result;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-12-15 11:34:42 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-15 11:34:42 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-15 11:34:42 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [FuncCall::class];
|
2018-12-15 11:34:42 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param FuncCall $node
|
2018-12-15 11:34:42 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-12-15 11:34:42 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isName($node, 'parse_str')) {
|
2018-12-15 11:34:42 +00:00
|
|
|
return null;
|
|
|
|
}
|
2019-07-08 17:32:22 +00:00
|
|
|
if (isset($node->args[1])) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$resultVariable = new Variable('result');
|
|
|
|
$node->args[1] = new Arg($resultVariable);
|
2022-07-18 15:25:27 +00:00
|
|
|
$currentStmt = $this->betterNodeFinder->resolveCurrentStatement($node);
|
|
|
|
if (!$currentStmt instanceof Stmt) {
|
2019-01-25 00:49:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-07-18 15:25:27 +00:00
|
|
|
$nextExpression = $currentStmt->getAttribute(AttributeKey::NEXT_NODE);
|
2022-08-09 19:37:14 +00:00
|
|
|
if (!$nextExpression instanceof Node) {
|
2019-01-25 00:49:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$this->traverseNodesWithCallable($nextExpression, function (Node $node) use($resultVariable) : ?Variable {
|
|
|
|
if (!$node instanceof FuncCall) {
|
2021-03-19 23:11:36 +00:00
|
|
|
return null;
|
2018-12-15 11:34:42 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isName($node, 'get_defined_vars')) {
|
2021-03-19 23:11:36 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $resultVariable;
|
2018-12-15 11:34:42 +00:00
|
|
|
});
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|