Fix #13831: spurious error in specific combination of namespace blocks and function declaration with phpdoc type annotation#5077
Merged
staabm merged 1 commit intophpstan:2.1.xfrom Feb 27, 2026
Conversation
- In NodeScopeResolver, when processing a `namespace { }` block (global namespace)
after a named namespace block, the scope's namespace was not being reset to null
- This caused PHPDoc resolution to use the wrong function name (prefixed with the
previous namespace), so PHPDoc annotations like `@return list<string>` were lost
- Added `else` branch in namespace handling to call `enterNamespace('')` for global
namespace blocks, which MutatingScope normalizes to `null`
- New regression test in tests/PHPStan/Rules/Functions/data/bug-13831.php
Closes phpstan/phpstan#13831
VincentLanglet
approved these changes
Feb 27, 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
Functions declared in a global namespace block (
namespace { ... }) that follows a named namespace block (namespace foo { ... }) incorrectly reportedmissingType.iterableValueerrors even when PHPDoc annotations fully specified the iterable value type (e.g.,@return list<string>). The fix ensures the scope's namespace is properly reset when entering a global namespace block.Changes
src/Analyser/NodeScopeResolver.php: Added anelsebranch in theStmt\Namespace_handling (around line 1010) to reset the scope namespace to global when$stmt->nameisnulltests/PHPStan/Rules/Functions/data/bug-13831.phpwith a function in a global namespace block preceded by a named namespace blocktestBug13831intests/PHPStan/Rules/Functions/MissingFunctionReturnTypehintRuleTest.phpRoot cause
When
NodeScopeResolverencountered aStmt\Namespace_node with$stmt->name === null(global namespace block), it did not call$scope->enterNamespace()to reset the namespace. If a named namespace block had been processed earlier, the scope retained the previous namespace. This causedgetPhpDocs()to construct the wrong function name (e.g.,foo\quxinstead ofqux), sogetResolvedPhpDoc()could not find the PHPDoc annotations for the function, making it appear as if the function had no PHPDoc return type.The fix adds
$scope = $scope->enterNamespace('')for global namespace blocks.MutatingScopealready normalizes empty string namespaces tonull, matching the behavior inFileTypeMapperwhich correctly handles this case.Test
The regression test declares a function
bug13831Qux(): arraywith@return list<string>inside a global namespace block preceded by a named namespace block. Before the fix, PHPStan reported a false positivemissingType.iterableValueerror. After the fix, no errors are reported.Fixes phpstan/phpstan#13831