Repository navigation
Partial reflection of classes whose ancestors can not be located - #252
Merged
Merged
Conversation
lisachenko
marked this pull request as ready for review
November 2, 2026 21:33
Member
Author
|
@claude could you please rebase, so PR will be for 4.x branch by default - I want to have this feature for both major versions |
Reflecting a class requires its parent class, interfaces and traits. When the locator could not find one of them, every method that needs it threw, so the class could not be reflected at all, even for its own methods. ReflectionEngine now takes a MissingAncestorPolicy (via init() or setMissingAncestorPolicy()): - Strict (default): the current behaviour, a ClassNotFoundException (which extends InvalidArgumentException) is thrown; - Partial: missing ancestors are left out, and the reflection covers what the class and its available ancestors declare. ReflectionClass and ReflectionEnum gain isPartial() and getMissingClassNames(), which report missing ancestors transitively and never throw, whatever the policy. All ancestor reflections now go through ReflectionClass::reflectAncestor(). Closes #251 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FxhC1CBg4rnTu9UjAMcCpL
Passing the result of class_exists() and alike as a bool lost the narrowing of the ancestor name to a class-string, so PHPStan rejected the native reflection of a declared ancestor. The helper now checks it itself, as ReflectionEngine::locateClassFile() does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FxhC1CBg4rnTu9UjAMcCpL
lisachenko
force-pushed
the
feature/friendly-cori-zyci9y
branch
from
November 2, 2026 21:36
96c4a0c to
702e4bf
Compare
Member
Author
|
Done: the branch is rebased onto Generated by Claude Code |
This was referenced Nov 3, 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.
Closes #251.
Problem
Reflecting a class needs its parent class, interfaces and traits. When the locator can't find one of them (generated at runtime, declared by
class_alias(), outside the Composer autoloader), every method that touches the hierarchy throws, so the class can't be reflected at all, even for its own methods.Change
A policy on the engine decides what happens to a missing ancestor:
MissingAncestorPolicy::Strict(default): same behaviour as before. The exception is now aClassNotFoundException, which extendsInvalidArgumentExceptionand keeps the same message, so existingcatchblocks still work.MissingAncestorPolicy::Partial: missing ancestors are left out.getParentClass()returnsfalsefor a missing parent. Missing interfaces and traits are left out ofgetInterfaces()/getInterfaceNames()/getTraits()/getTraitNames(), along with the members inherited from them. A prototype that would come from a missing ancestor is not reported.isSubclassOf()still recognises ancestors the class declares directly, because those are in its source. The names as written stay available ongetNode().isPartial()/getMissingClassNames()onReflectionClassandReflectionEnum: they include ancestors missing further up the hierarchy (e.g. a missing grandparent) and never throw, under either policy. A strict-mode user can call them to check a class before reflecting it.Implementation notes
ReflectionClass::reflectAncestor(). It is used bycollectInterfacesFromClassNode(),collectTraitsFromClassNode()andgetParentClass(). It reflects an ancestor that is already declared (class, interface or trait) natively and parses the others. The duplicatedcreateReflectionForClass()was removed from the trait and fromReflectionClass; both classes are final.ClassNotFoundException, i.e. the locator returned nothing. Other failures, such as a located file that doesn't declare the class, still throw.Tests
tests/PartialReflectionTest.phpwith the stubtests/Stub/FileWithMissingAncestors.php. The stub is only parsed, never included. The tests cover: the default policy, theinit()argument and the setter, strict-mode throwing, own members, attributes and constants in partial mode, omitted ancestors, prototypes,__toString(), a missing grandparent reported through the parent, an interface extending a missing interface, a class with no missing ancestors under both policies, theReflectionFileroute, and a class that is itself missing.4.xso the feature is available in both major versions. CI is green on PHP 8.4 and 8.5 with lowest and highest dependencies, and on the experimental PHP 8.6 jobs. PHPStan (level 10) passes.Docs
The README has a new "Classes whose ancestors can not be located" section, and
docs/reflection_class.mdandCLAUDE.mdare updated.Follow-up
goaop/framework can opt in with
ReflectionEngine::setMissingAncestorPolicy(MissingAncestorPolicy::Partial)once this is released, and then decide what to do withisPartial()classes (weave their own methods, or skip them with a clear message).🤖 Generated with Claude Code
https://claude.ai/code/session_01FxhC1CBg4rnTu9UjAMcCpL