Skip to content

Fix phpstan/phpstan#14241: Cannot re-assign auto-global variable in parameters#5153

Open
phpstan-bot wants to merge 2 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-lgqy7io
Open

Fix phpstan/phpstan#14241: Cannot re-assign auto-global variable in parameters#5153
phpstan-bot wants to merge 2 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-lgqy7io

Conversation

@phpstan-bot
Copy link
Collaborator

Summary

PHP throws a fatal error when a superglobal variable name (like $_FILES, $_GET, $_POST, etc.) is used as a function, method, or closure parameter. PHPStan was not detecting this and only reported missing type hints, missing the more fundamental issue.

Changes

  • Added src/Rules/Functions/SuperGlobalParameterRule.php — a new level-0 rule that checks all FunctionLike nodes for parameters named after superglobal variables
  • Added tests/PHPStan/Rules/Functions/SuperGlobalParameterRuleTest.php with test covering all superglobal variables across functions, methods, closures, and arrow functions
  • Added tests/PHPStan/Rules/Functions/data/bug-14241.php — test data file
  • Updated Makefile to exclude the test data file from PHP lint (since PHP itself cannot parse files with superglobal parameters)

Root cause

There was no rule checking whether function/method/closure parameters use superglobal variable names. PHP prohibits this at the parser level with "Cannot re-assign auto-global variable" fatal error, but PHPStan's parser (nikic/php-parser) successfully parses the code without error. A dedicated rule was needed to replicate this PHP compile-time check.

Test

The regression test SuperGlobalParameterRuleTest::testRule() verifies that errors are reported for $_FILES, $_GET, $_POST, $_SERVER, $_SESSION, $_COOKIE, $_REQUEST, $_ENV, and $GLOBALS when used as parameters in functions, methods, closures, and arrow functions. Regular parameter names like $ok produce no error.

Fixes phpstan/phpstan#14241

@clxmstaab clxmstaab force-pushed the create-pull-request/patch-lgqy7io branch from a2ae368 to ba7f36d Compare March 9, 2026 10:23
@staabm staabm requested a review from ondrejmirtes March 9, 2026 10:28
@staabm
Copy link
Contributor

staabm commented Mar 9, 2026

(requesting ondrejs review, as we are adding a new rule)

@VincentLanglet VincentLanglet force-pushed the create-pull-request/patch-lgqy7io branch from ba7f36d to e7b86ab Compare March 19, 2026 22:29
@VincentLanglet VincentLanglet requested a review from staabm March 20, 2026 08:31
@phpstan-bot
Copy link
Collaborator Author

Tests pass. The review from staabm is just an approval — no changes were requested. The PR is ready to merge as-is.

phpstan-bot and others added 2 commits March 20, 2026 10:16
…arameters

- Added SuperGlobalParameterRule to detect superglobal variable names used as function/method/closure parameters
- PHP throws a fatal error for this (e.g. `function doFoo($_FILES): void {}`), but PHPStan was not reporting it
- New regression test in tests/PHPStan/Rules/Functions/data/bug-14241.php
- Excluded test data file from Makefile lint step since PHP cannot parse files with superglobal parameters
@staabm staabm force-pushed the create-pull-request/patch-lgqy7io branch from e7b86ab to dff450d Compare March 20, 2026 09:16
Copy link
Contributor

@staabm staabm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test bot reaction after rebase to latest action workflows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants