diff --git a/Makefile b/Makefile index 3663878387..96836bff44 100644 --- a/Makefile +++ b/Makefile @@ -131,6 +131,7 @@ lint: --exclude tests/PHPStan/Rules/Classes/data/bug-14250.php \ --exclude tests/PHPStan/Rules/Classes/data/bug-14250-promoted-properties.php \ --exclude tests/PHPStan/Rules/Operators/data/bug-3585.php \ + --exclude tests/PHPStan/Rules/Functions/data/bug-14241.php \ src tests install-paratest: diff --git a/src/Rules/Functions/InvalidParameterNameRule.php b/src/Rules/Functions/InvalidParameterNameRule.php new file mode 100644 index 0000000000..5619b8f161 --- /dev/null +++ b/src/Rules/Functions/InvalidParameterNameRule.php @@ -0,0 +1,60 @@ + + */ +#[RegisteredRule(level: 0)] +final class InvalidParameterNameRule implements Rule +{ + + public function getNodeType(): string + { + return Node\FunctionLike::class; + } + + public function processNode(Node $node, Scope $scope): array + { + $errors = []; + + foreach ($node->getParams() as $param) { + if (!$param->var instanceof Node\Expr\Variable) { + continue; + } + + if (!is_string($param->var->name)) { + continue; + } + + $variableName = $param->var->name; + + if (in_array($variableName, Scope::SUPERGLOBAL_VARIABLES, true)) { + $errors[] = RuleErrorBuilder::message(sprintf('Cannot re-assign auto-global variable $%s.', $variableName)) + ->line($param->getStartLine()) + ->identifier('parameter.invalidExpr') + ->nonIgnorable() + ->build(); + } elseif ($variableName === 'this') { + $errors[] = RuleErrorBuilder::message('Cannot use $this as parameter.') + ->line($param->getStartLine()) + ->identifier('parameter.invalidExpr') + ->nonIgnorable() + ->build(); + } + + } + + return $errors; + } + +} diff --git a/tests/PHPStan/Rules/Functions/InvalidParameterNameRuleTest.php b/tests/PHPStan/Rules/Functions/InvalidParameterNameRuleTest.php new file mode 100644 index 0000000000..5593475d42 --- /dev/null +++ b/tests/PHPStan/Rules/Functions/InvalidParameterNameRuleTest.php @@ -0,0 +1,65 @@ + + */ +class InvalidParameterNameRuleTest extends RuleTestCase +{ + + protected function getRule(): Rule + { + return new InvalidParameterNameRule(); + } + + public function testRule(): void + { + $this->analyse([__DIR__ . '/data/bug-14241.php'], [ + [ + 'Cannot re-assign auto-global variable $_FILES.', + 5, + ], + [ + 'Cannot re-assign auto-global variable $_GET.', + 7, + ], + [ + 'Cannot re-assign auto-global variable $_POST.', + 7, + ], + [ + 'Cannot re-assign auto-global variable $_SERVER.', + 13, + ], + [ + 'Cannot re-assign auto-global variable $_SESSION.', + 15, + ], + [ + 'Cannot re-assign auto-global variable $_COOKIE.', + 18, + ], + [ + 'Cannot re-assign auto-global variable $_REQUEST.', + 20, + ], + [ + 'Cannot re-assign auto-global variable $_ENV.', + 22, + ], + [ + 'Cannot re-assign auto-global variable $GLOBALS.', + 24, + ], + [ + 'Cannot use $this as parameter.', + 26, + ], + ]); + } + +} diff --git a/tests/PHPStan/Rules/Functions/data/bug-14241.php b/tests/PHPStan/Rules/Functions/data/bug-14241.php new file mode 100644 index 0000000000..127541424c --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-14241.php @@ -0,0 +1,26 @@ + $_REQUEST; + +function doQux($_ENV): void {} + +function doQuux($GLOBALS): void {} + +function doThis($this) {}