Fix #8950: define() expressions before use statements breaks class autoloading#4977
Open
phpstan-bot wants to merge 4 commits into2.1.xfrom
Open
Fix #8950: define() expressions before use statements breaks class autoloading#4977phpstan-bot wants to merge 4 commits into2.1.xfrom
define() expressions before use statements breaks class autoloading#4977phpstan-bot wants to merge 4 commits into2.1.xfrom
Conversation
- Removed !array_key_exists guard in FileTypeMapper::createPhpDocNodeMap() that prevented updating the name scope map entry after it was first set - The first non-skipped statement (e.g. define()) would lock in an empty $uses array for that scope key, causing later PHPDocs to fail resolving class names from use statements - New regression test in tests/PHPStan/Analyser/nsrt/bug-8950.php
Closes phpstan/phpstan#11145 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Closes phpstan/phpstan#12639 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Automated fix attempt 1 for CI failures.
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 a
define()call (or any expression statement) appeared beforeusestatements in a PHP file, PHPDoc type resolution would fail to resolve class names imported viause. For example,@var FooInterface $foowould resolve to the unqualifiedFooInterfaceinstead ofSome\Namespace\FooInterface.Changes
!array_key_exists($nameScopeKey, $nameScopeMap)guard insrc/Type/FileTypeMapper.php(line 544) so the name scope map entry is always updated with the latest$usesarraytests/PHPStan/Analyser/nsrt/bug-8950.phpRoot cause
In
FileTypeMapper::createPhpDocNodeMap(), the name scope map entry for a given scope key was only set once (guarded by!array_key_exists). The$usesarray is populated progressively asusestatements are encountered during AST traversal. When a non-skipped statement likedefine()appeared beforeusestatements, it would create the map entry with an empty$usesarray. The guard then prevented any later statement from updating the entry with the correct$uses, causing all subsequent PHPDoc class name resolution in that scope to fail.The fix removes this guard so that each qualifying statement updates the map entry with the current
$uses. Sinceusestatements in PHP are scoped to the entire file/namespace (not position-dependent), the final entry in the map correctly reflects all imports.Test
The regression test defines an interface in a
Bug8950namespace block, then in a global namespace block placesdefine()beforeuse Bug8950\FooInterface. It verifies that@var FooInterface $foocorrectly resolves toBug8950\FooInterface.Fixes phpstan/phpstan#8950
Closes phpstan/phpstan#11145
Closes phpstan/phpstan#12639