Skip to content

Find the ancestors of a class value without resolving PHPDocs - #7101

Open
canvural wants to merge 1 commit into
phpstan:2.3.xfrom
canvural:bug-15375
Open

canvural wants to merge 1 commit into
phpstan:2.3.xfrom
canvural:bug-15375

Conversation

@canvural

@canvural canvural commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

As suggested in the issue, ClassResultCacheValueExtension::getValue() no longer calls ClassReflection::getAncestors(), which resolves the @extends/@implements PHPDocs and so reaches StubPhpDocProvider. It now walks the parents, interfaces and traits on BetterReflection's ReflectionClass.

I compared the walk with getAncestors() for every class in the classmap. It never finds fewer classes. It only finds more in invalid hierarchies, like a class implementing a trait or an enum.

Covered by e2e/bug-15375 and a stub files extension in ClassResultCacheValueExtensionTest that always throws.

This PR was AI generated, but reviewed by me.

Closes phpstan/phpstan#15375

continue;
}

$queue[] = $this->reflectionProvider->getClass($ancestorName);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this method could just return an array of BetterReflection's ReflectionClass, no need to convert back again to ClassReflection?

Additionally, there might be more paths in the code that are just interested in parent class names essentially, without the need for any extra work that ClassReflection is doing when resolving ancestors. Try to look for these paths and try to convert them too, measure whether the performance and/or memory consumption improves.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, getDeclaringClasses() now walks and returns BetterReflection's ReflectionClass.

Agent looked for other paths that only need the names: The only one that runs on every analysis is ValueDependencyCollector::noteClassDependency(), which calls getAncestors() just for the names. I timed that loop on a cold self-analysis of phpstan-src: 336,519 calls take 0.39 s summed over all 9 processes, in a 54 s run. So there's nothing to gain there. It also isn't a free swap: getAncestors() creates the ClassReflection of every ancestor, which is what records their missing parents through recordMissingAncestors(). I left it as is.

The rest are cold paths: the purity lookup in PhpClassReflectionExtension::createMethod(), ExtensionClassHelper, and the array_keys($classReflection->getInterfaces()) loops in four Rules/Api rules. I didn't convert them because I wouldn't be able to measure a difference. Happy to do it if you want them anyway

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.

ClassResultCacheValueExtension can run stub files extensions before bootstrapFiles

2 participants