Skip to content

Commit 6eb1bb1

Browse files
phpstan-botclaude
authored andcommitted
Address review: hook onto Param node, improve error messages
- Change rule to listen on Node\Param instead of Node\FunctionLike - Replace "Cannot re-assign auto-global variable" with clearer "Superglobal variable $X cannot be used as a parameter." - Use distinct identifiers: parameter.superglobal and parameter.this Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4f0c9d4 commit 6eb1bb1

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

src/Rules/Functions/InvalidParameterNameRule.php

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,48 @@
1212
use function sprintf;
1313

1414
/**
15-
* @implements Rule<Node\FunctionLike>
15+
* @implements Rule<Node\Param>
1616
*/
1717
#[RegisteredRule(level: 0)]
1818
final class InvalidParameterNameRule implements Rule
1919
{
2020

2121
public function getNodeType(): string
2222
{
23-
return Node\FunctionLike::class;
23+
return Node\Param::class;
2424
}
2525

2626
public function processNode(Node $node, Scope $scope): array
2727
{
28-
$errors = [];
29-
30-
foreach ($node->getParams() as $param) {
31-
if (!$param->var instanceof Node\Expr\Variable) {
32-
continue;
33-
}
28+
if (!$node->var instanceof Node\Expr\Variable) {
29+
return [];
30+
}
3431

35-
if (!is_string($param->var->name)) {
36-
continue;
37-
}
32+
if (!is_string($node->var->name)) {
33+
return [];
34+
}
3835

39-
$variableName = $param->var->name;
36+
$variableName = $node->var->name;
4037

41-
if (in_array($variableName, Scope::SUPERGLOBAL_VARIABLES, true)) {
42-
$errors[] = RuleErrorBuilder::message(sprintf('Cannot re-assign auto-global variable $%s.', $variableName))
43-
->line($param->getStartLine())
44-
->identifier('parameter.invalidExpr')
38+
if (in_array($variableName, Scope::SUPERGLOBAL_VARIABLES, true)) {
39+
return [
40+
RuleErrorBuilder::message(sprintf('Superglobal variable $%s cannot be used as a parameter.', $variableName))
41+
->identifier('parameter.superglobal')
4542
->nonIgnorable()
46-
->build();
47-
} elseif ($variableName === 'this') {
48-
$errors[] = RuleErrorBuilder::message('Cannot use $this as parameter.')
49-
->line($param->getStartLine())
50-
->identifier('parameter.invalidExpr')
51-
->nonIgnorable()
52-
->build();
53-
}
43+
->build(),
44+
];
45+
}
5446

47+
if ($variableName === 'this') {
48+
return [
49+
RuleErrorBuilder::message('Cannot use $this as parameter.')
50+
->identifier('parameter.this')
51+
->nonIgnorable()
52+
->build(),
53+
];
5554
}
5655

57-
return $errors;
56+
return [];
5857
}
5958

6059
}

tests/PHPStan/Rules/Functions/InvalidParameterNameRuleTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,39 @@ public function testRule(): void
2020
{
2121
$this->analyse([__DIR__ . '/data/bug-14241.php'], [
2222
[
23-
'Cannot re-assign auto-global variable $_FILES.',
23+
'Superglobal variable $_FILES cannot be used as a parameter.',
2424
5,
2525
],
2626
[
27-
'Cannot re-assign auto-global variable $_GET.',
27+
'Superglobal variable $_GET cannot be used as a parameter.',
2828
7,
2929
],
3030
[
31-
'Cannot re-assign auto-global variable $_POST.',
31+
'Superglobal variable $_POST cannot be used as a parameter.',
3232
7,
3333
],
3434
[
35-
'Cannot re-assign auto-global variable $_SERVER.',
35+
'Superglobal variable $_SERVER cannot be used as a parameter.',
3636
13,
3737
],
3838
[
39-
'Cannot re-assign auto-global variable $_SESSION.',
39+
'Superglobal variable $_SESSION cannot be used as a parameter.',
4040
15,
4141
],
4242
[
43-
'Cannot re-assign auto-global variable $_COOKIE.',
43+
'Superglobal variable $_COOKIE cannot be used as a parameter.',
4444
18,
4545
],
4646
[
47-
'Cannot re-assign auto-global variable $_REQUEST.',
47+
'Superglobal variable $_REQUEST cannot be used as a parameter.',
4848
20,
4949
],
5050
[
51-
'Cannot re-assign auto-global variable $_ENV.',
51+
'Superglobal variable $_ENV cannot be used as a parameter.',
5252
22,
5353
],
5454
[
55-
'Cannot re-assign auto-global variable $GLOBALS.',
55+
'Superglobal variable $GLOBALS cannot be used as a parameter.',
5656
24,
5757
],
5858
[

0 commit comments

Comments
 (0)