Skip to content

Commit ba7f36d

Browse files
committed
Rework
1 parent cb5129c commit ba7f36d

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

src/Rules/Functions/SuperGlobalParameterRule.php renamed to src/Rules/Functions/InvalidParameterNameRule.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @implements Rule<Node\FunctionLike>
1616
*/
1717
#[RegisteredRule(level: 0)]
18-
final class SuperGlobalParameterRule implements Rule
18+
final class InvalidParameterNameRule implements Rule
1919
{
2020

2121
public function getNodeType(): string
@@ -36,17 +36,22 @@ public function processNode(Node $node, Scope $scope): array
3636
continue;
3737
}
3838

39-
$var = $param->var->name;
40-
41-
if (!in_array($var, Scope::SUPERGLOBAL_VARIABLES, true)) {
42-
continue;
39+
$variableName = $param->var->name;
40+
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')
45+
->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();
4353
}
4454

45-
$errors[] = RuleErrorBuilder::message(sprintf('Cannot re-assign auto-global variable $%s.', $var))
46-
->line($param->getStartLine())
47-
->identifier('parameter.superGlobal')
48-
->nonIgnorable()
49-
->build();
5055
}
5156

5257
return $errors;

tests/PHPStan/Rules/Functions/SuperGlobalParameterRuleTest.php renamed to tests/PHPStan/Rules/Functions/InvalidParameterNameRuleTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
use PHPStan\Testing\RuleTestCase;
77

88
/**
9-
* @extends RuleTestCase<SuperGlobalParameterRule>
9+
* @extends RuleTestCase<InvalidParameterNameRule>
1010
*/
11-
class SuperGlobalParameterRuleTest extends RuleTestCase
11+
class InvalidParameterNameRuleTest extends RuleTestCase
1212
{
1313

1414
protected function getRule(): Rule
1515
{
16-
return new SuperGlobalParameterRule();
16+
return new InvalidParameterNameRule();
1717
}
1818

1919
public function testRule(): void
@@ -55,6 +55,10 @@ public function testRule(): void
5555
'Cannot re-assign auto-global variable $GLOBALS.',
5656
24,
5757
],
58+
[
59+
'Cannot use $this as parameter.',
60+
26,
61+
],
5862
]);
5963
}
6064

tests/PHPStan/Rules/Functions/data/bug-14241.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ public static function doBar($_SESSION): void {}
2222
function doQux($_ENV): void {}
2323

2424
function doQuux($GLOBALS): void {}
25+
26+
function doThis($this) {}

0 commit comments

Comments
 (0)