diff --git a/rules-tests/DowngradePhp83/Rector/FuncCall/DowngradeJsonValidateRector/Fixture/with_first_class_callable.php.inc b/rules-tests/DowngradePhp83/Rector/FuncCall/DowngradeJsonValidateRector/Fixture/with_first_class_callable.php.inc new file mode 100644 index 00000000..a69c397a --- /dev/null +++ b/rules-tests/DowngradePhp83/Rector/FuncCall/DowngradeJsonValidateRector/Fixture/with_first_class_callable.php.inc @@ -0,0 +1,44 @@ + +----- + $maxDepth) { + throw new \ValueError(sprintf('json_validate(): Argument #2 ($depth) must be less than %d', $maxDepth)); + } + json_decode($json, true, $depth, $flags); + return \JSON_ERROR_NONE === json_last_error(); + }; + echo (int) $jsonValidate(...)($json); + } +} + +?> diff --git a/rules/DowngradePhp83/Rector/FuncCall/DowngradeJsonValidateRector.php b/rules/DowngradePhp83/Rector/FuncCall/DowngradeJsonValidateRector.php index 2e52991a..0d86cbf5 100644 --- a/rules/DowngradePhp83/Rector/FuncCall/DowngradeJsonValidateRector.php +++ b/rules/DowngradePhp83/Rector/FuncCall/DowngradeJsonValidateRector.php @@ -17,9 +17,11 @@ use PHPStan\Analyser\Scope; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Exception\ShouldNotHappenException; +use Rector\Naming\Naming\VariableNaming; use Rector\NodeAnalyzer\ExprInTopStmtMatcher; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Parser\InlineCodeParser; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -35,6 +37,7 @@ final class DowngradeJsonValidateRector extends AbstractRector public function __construct( private readonly InlineCodeParser $inlineCodeParser, + private readonly VariableNaming $variableNaming, private readonly ExprInTopStmtMatcher $exprInTopStmtMatcher ) { } @@ -106,7 +109,8 @@ function (Node $subNode): bool { return null; } - $variable = new Variable('jsonValidate'); + $scope = ScopeFetcher::fetch($node); + $variable = new Variable($this->variableNaming->createCountedValueName('jsonValidate', $scope)); $function = $this->createClosure(); $expression = new Expression(new Assign($variable, $function)); @@ -152,11 +156,7 @@ private function shouldSkip(CallLike $callLike): bool return true; } - if ($callLike->isFirstClassCallable()) { - return true; - } - - $args = $callLike->getArgs(); + $args = $callLike->args; return count($args) < 1; } }