Resolve nested PHPDoc unions in linear time - #334
Merged
ondrejmirtes merged 1 commit intoSep 26, 2026
Merged
Conversation
MockObjectTypeNodeResolverExtension resolved every member of every union before it found that the union holds no mock class. TypeNodeResolver then resolved the same union again, so each nesting level doubled the work. The extension now resolves the direct identifier members first. It resolves the whole union only when one of them is a mock class. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Member
|
Thank you! |
This was referenced Sep 26, 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.
MockObjectTypeNodeResolverExtensionresolved every member of a union before it found that the union holds no mock class.TypeNodeResolverthen resolved the same union again, so each nesting level of a PHPDoc union doubled the work.Now the extension resolves only the direct identifier members first. It resolves the whole union only when one of them is a mock class. Identifiers have no children, so resolving them twice costs a constant amount per member.
Measured with PHPStan 2.2.16 on a file with one nested union,
/** @var ((((int|string)|string)|string)|string) $x */at increasing depth, level 9, single process, one run each:The inferred types do not change. Before the fix I dumped the types of six mock unions and one union without a mock, and the new test pins those types. The mock unions are
MockObject|Foo, the same name with a leading backslash, ause ... asalias,Stub|Foo, a local@phpstan-typealias, and a mock union nested in(...)|null. A type alias from the globaltypeAliasesconfig also still gives the intersection. The check runs on the resolved identifier, not on its name.The test also has a depth-30 union without a mock, and a depth-30 union with a mock at the centre. Before the fix it was still running after 90 seconds. With the fix it runs in 0.1s.
🤖 Generated with Claude Code