Skip to content

Commit 6379c5a

Browse files
authored
Fix rules missing named arguments (#7028)
1 parent b179760 commit 6379c5a

25 files changed

Lines changed: 222 additions & 83 deletions

‎src/Analyser/ArgumentsNormalizer.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ public static function reorderNewArguments(
297297
* The argument bound to the parameter at $position named $name: the
298298
* positional argument at that index or the one passed by that name.
299299
*
300+
* This method can be useful in simple cases when no reflection is needed.
301+
* In more complex cases where reflection information is available,
302+
* use ParametersAcceptorSelector::selectFromArgs() and ArgumentsNormalizer::reorder*Arguments() instead.
303+
*
300304
* @param Arg[] $args
301305
* @param 0|positive-int $position
302306
* @param non-empty-string $name

‎src/Rules/Api/NodeConnectingVisitorAttributesRule.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node;
66
use PhpParser\Node\Expr\MethodCall;
7+
use PHPStan\Analyser\ArgumentsNormalizer;
78
use PHPStan\Analyser\Scope;
89
use PHPStan\DependencyInjection\RegisteredRule;
910
use PHPStan\Rules\Rule;
@@ -36,7 +37,8 @@ public function processNode(Node $node, Scope $scope): array
3637
}
3738

3839
$args = $node->getArgs();
39-
if (!isset($args[0])) {
40+
$keyArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'key');
41+
if ($keyArg === null) {
4042
return [];
4143
}
4244

@@ -46,7 +48,7 @@ public function processNode(Node $node, Scope $scope): array
4648
}
4749

4850
$messages = [];
49-
$argType = $scope->getType($args[0]->value);
51+
$argType = $scope->getType($keyArg->value);
5052
foreach ($argType->getConstantStrings() as $constantString) {
5153
$argValue = $constantString->getValue();
5254
if (!in_array($argValue, ['parent', 'previous', 'next'], true)) {

‎src/Rules/Constants/ValueAssignedToDefineRule.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node;
66
use PhpParser\Node\Expr\FuncCall;
7+
use PHPStan\Analyser\ArgumentsNormalizer;
78
use PHPStan\Analyser\ConstantResolver;
89
use PHPStan\Analyser\Scope;
910
use PHPStan\DependencyInjection\Conditional;
@@ -49,12 +50,18 @@ public function processNode(Node $node, Scope $scope): array
4950
return [];
5051
}
5152

52-
$constantNameStrings = $scope->getType($args[0]->value)->getConstantStrings();
53+
$constantNameArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'constant_name');
54+
$valueArg = ArgumentsNormalizer::getArgByPositionOrName($args, 1, 'value');
55+
if ($constantNameArg === null || $valueArg === null) {
56+
return [];
57+
}
58+
59+
$constantNameStrings = $scope->getType($constantNameArg->value)->getConstantStrings();
5360
if (count($constantNameStrings) === 0) {
5461
return [];
5562
}
5663

57-
$valueType = $scope->getType($args[1]->value);
64+
$valueType = $scope->getType($valueArg->value);
5865
$errors = [];
5966

6067
foreach ($constantNameStrings as $constantNameString) {

‎src/Rules/DateIntervalInstantiationRule.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DateInterval;
66
use PhpParser\Node;
77
use PhpParser\Node\Expr\New_;
8+
use PHPStan\Analyser\ArgumentsNormalizer;
89
use PHPStan\Analyser\Scope;
910
use PHPStan\DependencyInjection\Conditional;
1011
use PHPStan\DependencyInjection\RegisteredRule;
@@ -37,14 +38,20 @@ public function processNode(Node $node, Scope $scope): array
3738
return [];
3839
}
3940

41+
$args = $node->getArgs();
4042
if (
41-
count($node->getArgs()) === 0
43+
count($args) === 0
4244
|| strtolower((string) $node->class) !== 'dateinterval'
4345
) {
4446
return [];
4547
}
4648

47-
$arg = $scope->getType($node->getArgs()[0]->value);
49+
$arg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'duration');
50+
if ($arg === null) {
51+
return [];
52+
}
53+
54+
$arg = $scope->getType($arg->value);
4855
$errors = [];
4956

5057
foreach ($arg->getConstantStrings() as $constantString) {

‎src/Rules/DateTimeInstantiationRule.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DateTime;
66
use PhpParser\Node;
77
use PhpParser\Node\Expr\New_;
8+
use PHPStan\Analyser\ArgumentsNormalizer;
89
use PHPStan\Analyser\Scope;
910
use PHPStan\DependencyInjection\RegisteredRule;
1011
use Throwable;
@@ -35,14 +36,20 @@ public function processNode(Node $node, Scope $scope): array
3536
}
3637

3738
$lowerClassName = strtolower((string) $node->class);
39+
$args = $node->getArgs();
3840
if (
39-
count($node->getArgs()) === 0
41+
count($args) === 0
4042
|| !in_array($lowerClassName, ['datetime', 'datetimeimmutable'], true)
4143
) {
4244
return [];
4345
}
4446

45-
$arg = $scope->getType($node->getArgs()[0]->value);
47+
$arg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'datetime');
48+
if ($arg === null) {
49+
return [];
50+
}
51+
52+
$arg = $scope->getType($arg->value);
4653
$errors = [];
4754

4855
foreach ($arg->getConstantStrings() as $constantString) {

‎src/Rules/Debug/FileAssertRule.php‎

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node;
66
use PhpParser\Node\Expr\StaticCall;
7+
use PHPStan\Analyser\ArgumentsNormalizer;
78
use PHPStan\Analyser\Scope;
89
use PHPStan\DependencyInjection\AutowiredService;
910
use PHPStan\PhpDoc\TypeStringResolver;
@@ -46,25 +47,42 @@ public function processNode(Node $node, Scope $scope): array
4647
return [];
4748
}
4849

49-
if (!$this->reflectionProvider->hasFunction($node->name, $scope)) {
50-
return [];
51-
}
52-
53-
$function = $this->reflectionProvider->getFunction($node->name, $scope);
54-
if ($function->getName() === 'PHPStan\\Testing\\assertType') {
55-
return $this->processAssertType($node->getArgs(), $scope);
56-
}
57-
58-
if ($function->getName() === 'PHPStan\\Testing\\assertNativeType') {
59-
return $this->processAssertNativeType($node->getArgs(), $scope);
60-
}
61-
62-
if ($function->getName() === 'PHPStan\\Testing\\assertSuperType') {
63-
return $this->processAssertSuperType($node->getArgs(), $scope);
64-
}
65-
66-
if ($function->getName() === 'PHPStan\\Testing\\assertVariableCertainty') {
67-
return $this->processAssertVariableCertainty($node->getArgs(), $scope);
50+
$name = $this->reflectionProvider->resolveFunctionName($node->name, $scope);
51+
$args = $node->getArgs();
52+
if ($name === 'PHPStan\\Testing\\assertType') {
53+
$typeArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'type');
54+
$valueArg = ArgumentsNormalizer::getArgByPositionOrName($args, 1, 'value');
55+
if ($typeArg === null || $valueArg === null) {
56+
return [];
57+
}
58+
return $this->processAssertType([$typeArg, $valueArg], $scope);
59+
}
60+
61+
if ($name === 'PHPStan\\Testing\\assertNativeType') {
62+
$typeArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'type');
63+
$valueArg = ArgumentsNormalizer::getArgByPositionOrName($args, 1, 'value');
64+
if ($typeArg === null || $valueArg === null) {
65+
return [];
66+
}
67+
return $this->processAssertNativeType([$typeArg, $valueArg], $scope);
68+
}
69+
70+
if ($name === 'PHPStan\\Testing\\assertSuperType') {
71+
$typeArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'superType');
72+
$valueArg = ArgumentsNormalizer::getArgByPositionOrName($args, 1, 'value');
73+
if ($typeArg === null || $valueArg === null) {
74+
return [];
75+
}
76+
return $this->processAssertSuperType([$typeArg, $valueArg], $scope);
77+
}
78+
79+
if ($name === 'PHPStan\\Testing\\assertVariableCertainty') {
80+
$certaintyArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'certainty');
81+
$variableArg = ArgumentsNormalizer::getArgByPositionOrName($args, 1, 'variable');
82+
if ($certaintyArg === null || $variableArg === null) {
83+
return [];
84+
}
85+
return $this->processAssertVariableCertainty([$certaintyArg, $variableArg], $scope);
6886
}
6987

7088
return [];

‎src/Rules/Functions/PrintfArrayParametersRule.php‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node;
66
use PhpParser\Node\Expr\FuncCall;
7+
use PHPStan\Analyser\ArgumentsNormalizer;
78
use PHPStan\Analyser\Scope;
89
use PHPStan\DependencyInjection\RegisteredRule;
910
use PHPStan\Reflection\ReflectionProvider;
@@ -46,23 +47,22 @@ public function processNode(Node $node, Scope $scope): array
4647
return [];
4748
}
4849

49-
if (!$this->reflectionProvider->hasFunction($node->name, $scope)) {
50-
return [];
51-
}
52-
53-
$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope);
54-
$name = $functionReflection->getName();
50+
$name = $this->reflectionProvider->resolveFunctionName($node->name, $scope);
5551
if (!in_array($name, ['vprintf', 'vsprintf'], true)) {
5652
return [];
5753
}
5854

5955
$args = $node->getArgs();
60-
$argsCount = count($args);
61-
if ($argsCount < 1) {
56+
if (count($args) < 1) {
6257
return []; // caught by CallToFunctionParametersRule
6358
}
6459

65-
$formatArgType = $scope->getType($args[0]->value);
60+
$formatArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'format');
61+
if ($formatArg === null) {
62+
return [];
63+
}
64+
65+
$formatArgType = $scope->getType($formatArg->value);
6666
$placeHoldersCounts = [];
6767
foreach ($formatArgType->getConstantStrings() as $formatString) {
6868
$format = $formatString->getValue();
@@ -97,8 +97,9 @@ public function processNode(Node $node, Scope $scope): array
9797
}
9898

9999
$formatArgsCounts = [];
100-
if (isset($args[1])) {
101-
$formatArgsType = $scope->getType($args[1]->value);
100+
$formatValuesArg = ArgumentsNormalizer::getArgByPositionOrName($args, 1, 'values');
101+
if ($formatValuesArg !== null) {
102+
$formatArgsType = $scope->getType($formatValuesArg->value);
102103

103104
$constantArrays = $formatArgsType->getConstantArrays();
104105
if ($constantArrays === []) {

‎src/Rules/Functions/RandomIntParametersRule.php‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node;
66
use PhpParser\Node\Expr\FuncCall;
7+
use PHPStan\Analyser\ArgumentsNormalizer;
78
use PHPStan\Analyser\Scope;
89
use PHPStan\DependencyInjection\AutowiredParameter;
910
use PHPStan\DependencyInjection\RegisteredRule;
@@ -44,17 +45,23 @@ public function processNode(Node $node, Scope $scope): array
4445
return [];
4546
}
4647

48+
$args = $node->getArgs();
49+
if (count($args) < 2) {
50+
return [];
51+
}
52+
4753
if ($this->reflectionProvider->resolveFunctionName($node->name, $scope) !== 'random_int') {
4854
return [];
4955
}
5056

51-
$args = $node->getArgs();
52-
if (count($args) < 2) {
57+
$minArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'min');
58+
$maxArg = ArgumentsNormalizer::getArgByPositionOrName($args, 1, 'max');
59+
if ($minArg === null || $maxArg === null) {
5360
return [];
5461
}
5562

56-
$minType = $scope->getType($args[0]->value)->toInteger();
57-
$maxType = $scope->getType($args[1]->value)->toInteger();
63+
$minType = $scope->getType($minArg->value)->toInteger();
64+
$maxType = $scope->getType($maxArg->value)->toInteger();
5865

5966
if (
6067
!$minType instanceof ConstantIntegerType && !$minType instanceof IntegerRangeType

‎src/Rules/Regexp/RegularExpressionPatternRule.php‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Nette\Utils\Strings;
77
use PhpParser\Node;
88
use PhpParser\Node\Expr\FuncCall;
9+
use PHPStan\Analyser\ArgumentsNormalizer;
910
use PHPStan\Analyser\Scope;
1011
use PHPStan\DependencyInjection\RegisteredRule;
1112
use PHPStan\Rules\Rule;
@@ -36,6 +37,15 @@ public function getNodeType(): string
3637

3738
public function processNode(Node $node, Scope $scope): array
3839
{
40+
if (!$node->name instanceof Node\Name) {
41+
return [];
42+
}
43+
44+
$functionName = strtolower((string) $node->name);
45+
if (!str_starts_with($functionName, 'preg_')) {
46+
return [];
47+
}
48+
3949
$patterns = $this->extractPatterns($node, $scope);
4050

4151
$errors = [];
@@ -60,14 +70,17 @@ private function extractPatterns(FuncCall $functionCall, Scope $scope): array
6070
return [];
6171
}
6272
$functionName = strtolower((string) $functionCall->name);
63-
if (!str_starts_with($functionName, 'preg_')) {
73+
74+
$args = $functionCall->getArgs();
75+
if (!isset($args[0])) {
6476
return [];
6577
}
6678

67-
if (!isset($functionCall->getArgs()[0])) {
79+
$patternArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'pattern');
80+
if ($patternArg === null) {
6881
return [];
6982
}
70-
$patternNode = $functionCall->getArgs()[0]->value;
83+
$patternNode = $patternArg->value;
7184
$patternType = $scope->getType($patternNode);
7285

7386
$patternStrings = [];

‎tests/PHPStan/Rules/Api/NodeConnectingVisitorAttributesRuleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public function testRule(): void
2929
24,
3030
'See: https://phpstan.org/blog/preprocessing-ast-for-custom-rules',
3131
],
32+
[
33+
'Node attribute \'parent\' is no longer available.',
34+
25,
35+
'See: https://phpstan.org/blog/preprocessing-ast-for-custom-rules',
36+
],
3237
]);
3338
}
3439

0 commit comments

Comments
 (0)