Skip to content

[Feature] Implement complete trait adaptation support (insteadof + alias) in getMethods()#177

Merged
lisachenko merged 3 commits intomasterfrom
copilot/add-trait-support-in-reflection
Apr 27, 2026
Merged

[Feature] Implement complete trait adaptation support (insteadof + alias) in getMethods()#177
lisachenko merged 3 commits intomasterfrom
copilot/add-trait-support-in-reflection

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 27, 2026

Classes using traits with insteadof/as adaptations only returned their own directly-defined methods — trait-contributed methods were absent, and adapted aliases were never created.

Changes

  • getMethods() rewrite (ReflectionClassLikeTrait): replaces the flat recursiveCollect call for traits with a new collectTraitMethods() that applies adaptations. Parent class and interface methods are now inlined directly.

  • collectTraitMethods() (new private method): pure AST-based trait resolution pipeline:

    1. Parses each used trait's AST via ReflectionEngine::parseClass() to extract ClassMethod nodes
    2. Builds an exclusion map from Precedence (insteadof) adaptations and skips excluded methods per trait
    3. Processes Alias (as) adaptations by cloning the source ClassMethod node, patching name and flags for visibility, then constructing a new ReflectionMethod
    4. Sets $className to the using class (matches native class property / __debugInfo) and $declaringClass to the trait
  • Adaptation accumulation fix (ReflectionClass::collectTraitsFromClassNode): multiple use blocks now array_merge adaptations instead of overwriting.

Example

class MyHelloWorld {
    use TraitA, TraitB {
        sayHello as protected sayHelloProtected;  // alias with visibility change
        TraitA::smallTalk as smallTalkTraitA;     // alias from specific trait
        TraitB::smallTalk insteadof TraitA;       // precedence
    }
}

// Before: only own methods returned
// After: matches native ReflectionClass::getMethods() exactly
$rc = new ReflectionClass(MyHelloWorld::class);
$rc->getMethods(); // sayHello, sayHelloProtected, sayWorld, smallTalk (TraitB), smallTalkTraitA
  • Test stubs added: ClassWithPhp54TraitAndAdaptation and ClassWithPhp54TraitAndConflict exercise both adaptation types
  • testGetMethodCount: removed markTestIncomplete guard that was bypassing trait-alias classes

Copilot AI linked an issue Apr 27, 2026 that may be closed by this pull request
@lisachenko
Copy link
Copy Markdown
Member

@copilot also, support this #54 - it is relevant

@lisachenko lisachenko linked an issue Apr 27, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Add support for traits in class reflection Implement complete trait adaptation support (insteadof + alias) in getMethods() Apr 27, 2026
Copilot AI requested a review from lisachenko April 27, 2026 16:11
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 27, 2026

@copilot also, support this #54 - it is relevant

Issue #54's two patterns are already covered by this PR:

  1. foo as protected fooBar; foo as private fooBaz; → handled by the Alias adaptation path in collectTraitMethods(), tested via ClassWithPhp54TraitAndAdaptation
  2. ConflictedSimpleTrait::foo insteadof SimpleTrait; foo as protected fooBar; → handled by the Precedence + Alias paths, tested via ClassWithPhp54TraitAndConflict

The markTestIncomplete guard in testGetMethodCount that was blocking these classes is also removed in this PR.

@lisachenko lisachenko modified the milestone: 4.0.0 Apr 27, 2026
@lisachenko lisachenko marked this pull request as ready for review April 27, 2026 17:31
@lisachenko lisachenko changed the title Implement complete trait adaptation support (insteadof + alias) in getMethods() [Feature] Implement complete trait adaptation support (insteadof + alias) in getMethods() Apr 27, 2026
@lisachenko lisachenko merged commit a2f518b into master Apr 27, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Incomplete trait support Trait adoptation and method aliasing is not supported

2 participants