Assign proper types for $argc and $argv in global statements#5623
Open
phpstan-bot wants to merge 3 commits intophpstan:2.1.xfrom
Open
Assign proper types for $argc and $argv in global statements#5623phpstan-bot wants to merge 3 commits intophpstan:2.1.xfrom
$argc and $argv in global statements#5623phpstan-bot wants to merge 3 commits intophpstan:2.1.xfrom
Conversation
- NodeScopeResolver now assigns `int<1, max>` for `$argc` and `non-empty-list<string>` for `$argv` when processing `global` statements, instead of unconditionally assigning `MixedType` - Extract `getGlobalVariableType()` helper in NodeScopeResolver to determine the correct type for a global variable name - Update cli-globals.php test assertions to expect the correct types and add test cases for methods, static methods, closures, and reassignment after `global`
Collaborator
Author
|
I processed this review but have nothing to report. |
staabm
approved these changes
May 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When
global $argv;orglobal $argc;is used inside a function or method, PHPStan assignedMixedTypeto the variable instead of the correct CLI types (int<1, max>for$argc,non-empty-list<string>for$argv). This fix teachesNodeScopeResolverto assign the proper types for these variables inglobalstatements.Changes
getGlobalVariableType()private method toNodeScopeResolver(src/Analyser/NodeScopeResolver.php) that returns the correct type for$argcand$argv, falling back toMixedTypefor other global variablesglobalstatement handling inNodeScopeResolverto usegetGlobalVariableType()instead of unconditionally assigningMixedTypetests/PHPStan/Analyser/nsrt/cli-globals.phpassertions to expect the correct types and added test cases for:global(ensures user assignments still override)Root cause
In
NodeScopeResolver::processStmtNodes(), theglobalstatement handler (around line 2184) unconditionally assignednew MixedType()to every variable declared viaglobal. The special type handling for$argc/$argvinMutatingScope::getVariableType()only triggers whenhasVariableType()returnsmaybe(i.e., in global scope where these variables aren't explicitly assigned). Afterglobal $argv;, the variable was explicitly inexpressionTypeswithMixedTypeand certaintyYes, bypassing the special handling.The fix moves the type determination to the assignment site in
NodeScopeResolver, so the correct type is stored from the start.Analogous cases probed
$_SERVER,$_GET, etc.): Not affected — these don't needglobaldeclarations and are handled separately viaisGlobalVariable()$GLOBALS['argv']: Different code path, returnsmixedfor all$GLOBALSvalues — separate concern$http_response_header: Assigned at specific function call sites, not viaglobal— not analogousTest
Updated
tests/PHPStan/Analyser/nsrt/cli-globals.phpto assertint<1, max>for$argcandnon-empty-list<string>for$argvafterglobaldeclarations. Added coverage for methods, static methods, closures, and reassignment.Fixes phpstan/phpstan#12392