Skip to content

Commit f8d1ea1

Browse files
authored
[DowngradePhp83] Allow with first class callable on DowngradeJsonValidateRector (#332)
1 parent 6d54350 commit f8d1ea1

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp83\Rector\FuncCall\DowngradeJsonValidateRector\Fixture;
4+
5+
class WithFirstClassCallable
6+
{
7+
public function run($json)
8+
{
9+
echo (int) json_validate(...)($json);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DowngradePhp83\Rector\FuncCall\DowngradeJsonValidateRector\Fixture;
18+
19+
class WithFirstClassCallable
20+
{
21+
public function run($json)
22+
{
23+
$jsonValidate = function (string $json, int $depth = 512, int $flags = 0) {
24+
if (function_exists('json_validate')) {
25+
return json_validate($json, $depth, $flags);
26+
}
27+
$maxDepth = 0x7fffffff;
28+
if (0 !== $flags && \defined('JSON_INVALID_UTF8_IGNORE') && \JSON_INVALID_UTF8_IGNORE !== $flags) {
29+
throw new \ValueError('json_validate(): Argument #3 ($flags) must be a valid flag (allowed flags: JSON_INVALID_UTF8_IGNORE)');
30+
}
31+
if ($depth <= 0) {
32+
throw new \ValueError('json_validate(): Argument #2 ($depth) must be greater than 0');
33+
}
34+
if ($depth > $maxDepth) {
35+
throw new \ValueError(sprintf('json_validate(): Argument #2 ($depth) must be less than %d', $maxDepth));
36+
}
37+
json_decode($json, true, $depth, $flags);
38+
return \JSON_ERROR_NONE === json_last_error();
39+
};
40+
echo (int) $jsonValidate(...)($json);
41+
}
42+
}
43+
44+
?>

rules/DowngradePhp83/Rector/FuncCall/DowngradeJsonValidateRector.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
use PHPStan\Analyser\Scope;
1818
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1919
use Rector\Exception\ShouldNotHappenException;
20+
use Rector\Naming\Naming\VariableNaming;
2021
use Rector\NodeAnalyzer\ExprInTopStmtMatcher;
2122
use Rector\NodeTypeResolver\Node\AttributeKey;
2223
use Rector\PhpParser\Parser\InlineCodeParser;
24+
use Rector\PHPStan\ScopeFetcher;
2325
use Rector\Rector\AbstractRector;
2426
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2527
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -35,6 +37,7 @@ final class DowngradeJsonValidateRector extends AbstractRector
3537

3638
public function __construct(
3739
private readonly InlineCodeParser $inlineCodeParser,
40+
private readonly VariableNaming $variableNaming,
3841
private readonly ExprInTopStmtMatcher $exprInTopStmtMatcher
3942
) {
4043
}
@@ -106,7 +109,8 @@ function (Node $subNode): bool {
106109
return null;
107110
}
108111

109-
$variable = new Variable('jsonValidate');
112+
$scope = ScopeFetcher::fetch($node);
113+
$variable = new Variable($this->variableNaming->createCountedValueName('jsonValidate', $scope));
110114

111115
$function = $this->createClosure();
112116
$expression = new Expression(new Assign($variable, $function));
@@ -152,11 +156,7 @@ private function shouldSkip(CallLike $callLike): bool
152156
return true;
153157
}
154158

155-
if ($callLike->isFirstClassCallable()) {
156-
return true;
157-
}
158-
159-
$args = $callLike->getArgs();
159+
$args = $callLike->args;
160160
return count($args) < 1;
161161
}
162162
}

0 commit comments

Comments
 (0)