From 8db39dfbfc1f648046871f9bc5a608f104d1a1d8 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Thu, 29 Oct 2026 14:42:24 -0700 Subject: [PATCH 1/7] Test edges PHP rejects are no edges Co-Authored-By: Claude Opus 5.5 --- tests/Repository/MemberResolverTest.php | 73 +++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/Repository/MemberResolverTest.php b/tests/Repository/MemberResolverTest.php index c4858238..3de7c3aa 100644 --- a/tests/Repository/MemberResolverTest.php +++ b/tests/Repository/MemberResolverTest.php @@ -2280,6 +2280,79 @@ private function resolverOverClasses(ClassInfo ...$classes): MemberResolver return new MemberResolver($repo); } + /** + * @return iterable + */ + public static function edgesPhpRejects(): iterable + { + // php-src zend_inheritance.c rejects each of these when linking the class. + yield 'a class using a class as a trait' => [ClassKind::Class_, 'traits', ClassKind::Class_]; + yield 'a class using an interface as a trait' => [ClassKind::Class_, 'traits', ClassKind::Interface_]; + yield 'an interface using a trait' => [ClassKind::Interface_, 'traits', ClassKind::Trait_]; + yield 'a class extending an interface' => [ClassKind::Class_, 'parent', ClassKind::Interface_]; + yield 'a class extending a trait' => [ClassKind::Class_, 'parent', ClassKind::Trait_]; + yield 'a class extending an enum' => [ClassKind::Class_, 'parent', ClassKind::Enum_]; + yield 'a class implementing a class' => [ClassKind::Class_, 'interfaces', ClassKind::Class_]; + yield 'a class implementing a trait' => [ClassKind::Class_, 'interfaces', ClassKind::Trait_]; + yield 'an interface extending a class' => [ClassKind::Interface_, 'interfaces', ClassKind::Class_]; + } + + /** + * Code being edited often names the wrong kind of class-like; such an edge + * brings in no members and makes no subclass. + */ + #[DataProvider('edgesPhpRejects')] + public function testAnEdgePhpRejectsIsNoEdge(ClassKind $kind, string $edge, ClassKind $targetKind): void + { + $target = ClasslikeName::fromFullyQualified(self::fakeClass()); + $origin = ClasslikeName::fromFullyQualified(self::fakeClass()); + $resolver = new MemberResolver($this->sourceOf( + $this->createClassInfo( + $target, + kind: $targetKind, + methods: ['run' => $this->createMethodInfo('run', Visibility::Public, $target)], + ), + $this->createClassInfo( + $origin, + kind: $kind, + parent: $edge === 'parent' ? $target : null, + traits: $edge === 'traits' ? [$target] : [], + interfaces: $edge === 'interfaces' ? [$target] : [], + ), + )); + + self::assertNull($resolver->findMethod($origin, 'run', $origin), 'the named class-like brings in nothing'); + self::assertSame( + [], + $resolver->getMembersOfKind($origin, MemberKind::Method, $origin), + 'nothing is listed through it', + ); + self::assertFalse($resolver->isSubclassOf($origin, $target), 'it is not a supertype'); + } + + /** + * PHP rejects an alias naming a trait the class does not use ("Required + * Trait %s wasn't added to %s", zend_inheritance.c). + */ + public function testAnAliasOfATraitTheClassDoesNotUseIsInvisible(): void + { + $trait = ClasslikeName::fromFullyQualified(self::fakeClass()); + $origin = ClasslikeName::fromFullyQualified(self::fakeClass()); + $resolver = new MemberResolver($this->sourceOf( + $this->createClassInfo( + $trait, + kind: ClassKind::Trait_, + methods: ['run' => $this->createMethodInfo('run', Visibility::Public, $trait)], + ), + $this->createClassInfo( + $origin, + traitAliases: [new TraitAlias(trait: $trait, method: 'run', newName: 'go', newVisibility: null)], + ), + )); + + self::assertNull($resolver->findMethod($origin, 'go', $origin), 'an alias needs the trait to be used'); + } + /** * @return class-string */ From 3f18cd2ae10efb0b951a1b1b47edb6a746693788 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Thu, 29 Oct 2026 14:44:21 -0700 Subject: [PATCH 2/7] Follow only the edges PHP accepts Co-Authored-By: Claude Opus 5.5 --- src/Repository/MemberResolver.php | 73 +++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/src/Repository/MemberResolver.php b/src/Repository/MemberResolver.php index ac887843..67834f20 100644 --- a/src/Repository/MemberResolver.php +++ b/src/Repository/MemberResolver.php @@ -362,7 +362,10 @@ private function passes(MemberInfoInterface $member, MemberFilter $filter): bool * The types to search after a class's own members, in PHP's resolution order: * used traits, then the parent chain, then interfaces. A trait edge may carry * the method names excluded from that trait by an `insteadof` clause on this - * class. Unresolvable types are skipped. + * class. Unresolvable types are skipped, and so is an edge naming a kind of + * class-like PHP rejects there: a class extends only a class, and + * `implements` or an interface's `extends` names only interfaces + * (`zend_do_inheritance_ex` and `zend_do_implement_interfaces` in php-src). * * Every member lookup walks the type graph through this one method, so all * member kinds see the same hierarchy. @@ -373,27 +376,46 @@ private function passes(MemberInfoInterface $member, MemberFilter $filter): bool */ private function supertypes(ClassInfo $classInfo): array { - $names = []; - foreach ($classInfo->traits as $trait) { - $key = NameKind::ClassLike->normalize($trait->qualifiedName); - $names[] = [$trait, true, $classInfo->traitExclusions[$key] ?? []]; + $supertypes = []; + foreach ($this->usedTraits($classInfo) as [$trait, $exclusions]) { + $supertypes[] = [$trait, true, $exclusions]; } - if ($classInfo->parent !== null) { - $names[] = [$classInfo->parent, false, []]; + $parent = $classInfo->parent === null ? null : $this->source->lookupClassLike($classInfo->parent); + if ($parent?->isClass() === true) { + $supertypes[] = [$parent, false, []]; } - foreach ($classInfo->interfaces as $interface) { - $names[] = [$interface, false, []]; + foreach ($classInfo->interfaces as $name) { + $interface = $this->source->lookupClassLike($name); + if ($interface?->isInterface() === true) { + $supertypes[] = [$interface, false, []]; + } } - $supertypes = []; - foreach ($names as [$name, $isOriginClass, $exclusions]) { - $info = $this->source->lookupClassLike($name); - if ($info !== null) { - $supertypes[] = [$info, $isOriginClass, $exclusions]; + return $supertypes; + } + + /** + * The traits a class-like uses, each with the method names an `insteadof` + * clause excludes from it. PHP rejects `use` naming anything but a trait, + * and an interface using traits (`zend_do_bind_traits` in php-src). + * + * @return list}> + */ + private function usedTraits(ClassInfo $classInfo): array + { + if ($classInfo->isInterface()) { + return []; + } + $traits = []; + foreach ($classInfo->traits as $name) { + $trait = $this->source->lookupClassLike($name); + if ($trait?->isTrait() === true) { + $key = NameKind::ClassLike->normalize($name->qualifiedName); + $traits[] = [$trait, $classInfo->traitExclusions[$key] ?? []]; } } - return $supertypes; + return $traits; } /** @@ -507,14 +529,20 @@ private function resolveAlias(ClassInfo $origin, TraitAlias $alias): ?MethodInfo ); } + /** + * The trait method an alias names, from the traits $origin uses: the one + * the alias names, which PHP requires to be used ("Required Trait %s + * wasn't added to %s", zend_inheritance.c), else the first that declares + * the method. + */ private function findAliasSource(ClassInfo $origin, TraitAlias $alias): ?MethodInfo { - if ($alias->trait !== null) { - return $this->traitMethod($alias->trait, $alias->method); - } - foreach ($origin->traits as $trait) { + foreach ($this->usedTraits($origin) as [$trait]) { + if ($alias->trait !== null && !$alias->trait->equals($trait->name)) { + continue; + } $method = $this->traitMethod($trait, $alias->method); - if ($method !== null) { + if ($method !== null || $alias->trait !== null) { return $method; } } @@ -526,10 +554,9 @@ private function findAliasSource(ClassInfo $origin, TraitAlias $alias): ?MethodI * A trait's method as declared, so an alias of it binds `self` to the * class using the trait. */ - private function traitMethod(ClasslikeName $trait, string $name): ?MethodInfo + private function traitMethod(ClassInfo $trait, string $name): ?MethodInfo { - $origin = $this->source->lookupClassLike($trait); - return $origin === null ? null : ($this->declaredMethod($origin, $name, $trait)[0] ?? null); + return $this->declaredMethod($trait, $name, $trait->name)[0] ?? null; } /** From ec824e96ccc8a73ef2897b9b69fe6b0724daadf4 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Thu, 29 Oct 2026 14:51:02 -0700 Subject: [PATCH 3/7] Cite where PHP rejects a non-trait in use Co-Authored-By: Claude Opus 5.5 --- src/Repository/MemberResolver.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Repository/MemberResolver.php b/src/Repository/MemberResolver.php index 67834f20..5d482737 100644 --- a/src/Repository/MemberResolver.php +++ b/src/Repository/MemberResolver.php @@ -396,8 +396,9 @@ private function supertypes(ClassInfo $classInfo): array /** * The traits a class-like uses, each with the method names an `insteadof` - * clause excludes from it. PHP rejects `use` naming anything but a trait, - * and an interface using traits (`zend_do_bind_traits` in php-src). + * clause excludes from it. PHP rejects `use` naming anything but a trait + * (`zend_do_link_class` in php-src), and an interface using traits + * (`zend_compile.c`). * * @return list}> */ From cecaa67328e90e9a45b7c5da80a2d9437e5e077a Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Thu, 29 Oct 2026 14:55:41 -0700 Subject: [PATCH 4/7] Test only that use follows traits Co-Authored-By: Claude Opus 5.5 --- tests/Repository/MemberResolverTest.php | 70 +++++++++++++++---------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/tests/Repository/MemberResolverTest.php b/tests/Repository/MemberResolverTest.php index 3de7c3aa..258e15dc 100644 --- a/tests/Repository/MemberResolverTest.php +++ b/tests/Repository/MemberResolverTest.php @@ -2281,44 +2281,34 @@ private function resolverOverClasses(ClassInfo ...$classes): MemberResolver } /** - * @return iterable + * @return iterable */ - public static function edgesPhpRejects(): iterable + public static function nonTraitsInUse(): iterable { - // php-src zend_inheritance.c rejects each of these when linking the class. - yield 'a class using a class as a trait' => [ClassKind::Class_, 'traits', ClassKind::Class_]; - yield 'a class using an interface as a trait' => [ClassKind::Class_, 'traits', ClassKind::Interface_]; - yield 'an interface using a trait' => [ClassKind::Interface_, 'traits', ClassKind::Trait_]; - yield 'a class extending an interface' => [ClassKind::Class_, 'parent', ClassKind::Interface_]; - yield 'a class extending a trait' => [ClassKind::Class_, 'parent', ClassKind::Trait_]; - yield 'a class extending an enum' => [ClassKind::Class_, 'parent', ClassKind::Enum_]; - yield 'a class implementing a class' => [ClassKind::Class_, 'interfaces', ClassKind::Class_]; - yield 'a class implementing a trait' => [ClassKind::Class_, 'interfaces', ClassKind::Trait_]; - yield 'an interface extending a class' => [ClassKind::Interface_, 'interfaces', ClassKind::Class_]; + yield 'a class using a class' => [ClassKind::Class_, ClassKind::Class_]; + yield 'a class using an interface' => [ClassKind::Class_, ClassKind::Interface_]; + yield 'a class using an enum' => [ClassKind::Class_, ClassKind::Enum_]; + yield 'an enum using a class' => [ClassKind::Enum_, ClassKind::Class_]; + yield 'a trait using an interface' => [ClassKind::Trait_, ClassKind::Interface_]; } /** - * Code being edited often names the wrong kind of class-like; such an edge - * brings in no members and makes no subclass. + * PHP rejects `use` naming anything but a trait ("%s cannot use %s - it + * is not a trait", zend_inheritance.c). Code being edited often does, and + * the named class-like's members must not become the class's own. */ - #[DataProvider('edgesPhpRejects')] - public function testAnEdgePhpRejectsIsNoEdge(ClassKind $kind, string $edge, ClassKind $targetKind): void + #[DataProvider('nonTraitsInUse')] + public function testUseNamingANonTraitBringsInNothing(ClassKind $kind, ClassKind $usedKind): void { - $target = ClasslikeName::fromFullyQualified(self::fakeClass()); + $used = ClasslikeName::fromFullyQualified(self::fakeClass()); $origin = ClasslikeName::fromFullyQualified(self::fakeClass()); $resolver = new MemberResolver($this->sourceOf( $this->createClassInfo( - $target, - kind: $targetKind, - methods: ['run' => $this->createMethodInfo('run', Visibility::Public, $target)], - ), - $this->createClassInfo( - $origin, - kind: $kind, - parent: $edge === 'parent' ? $target : null, - traits: $edge === 'traits' ? [$target] : [], - interfaces: $edge === 'interfaces' ? [$target] : [], + $used, + kind: $usedKind, + methods: ['run' => $this->createMethodInfo('run', Visibility::Private, $used)], ), + $this->createClassInfo($origin, kind: $kind, traits: [$used]), )); self::assertNull($resolver->findMethod($origin, 'run', $origin), 'the named class-like brings in nothing'); @@ -2327,7 +2317,31 @@ interfaces: $edge === 'interfaces' ? [$target] : [], $resolver->getMembersOfKind($origin, MemberKind::Method, $origin), 'nothing is listed through it', ); - self::assertFalse($resolver->isSubclassOf($origin, $target), 'it is not a supertype'); + } + + /** + * PHP matches class names case-insensitively, so an alias naming its + * trait in another case still draws from it. + */ + public function testAnAliasNamingItsTraitInAnotherCaseDrawsFromIt(): void + { + $trait = ClasslikeName::fromFullyQualified(self::fakeClass()); + $written = ClasslikeName::fromFullyQualified(strtolower($trait->qualifiedName->fullyQualifiedName())); + $origin = ClasslikeName::fromFullyQualified(self::fakeClass()); + $resolver = new MemberResolver($this->sourceOf( + $this->createClassInfo( + $trait, + kind: ClassKind::Trait_, + methods: ['run' => $this->createMethodInfo('run', Visibility::Public, $trait)], + ), + $this->createClassInfo( + $origin, + traits: [$trait], + traitAliases: [new TraitAlias(trait: $written, method: 'run', newName: 'go', newVisibility: null)], + ), + )); + + self::assertSame('go', $resolver->findMethod($origin, 'go', $origin)?->getName()->name, 'the alias resolves'); } /** From efbf2235c3116b57a103814ec6906509f97d06a2 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Thu, 29 Oct 2026 14:57:18 -0700 Subject: [PATCH 5/7] Check only that use names a trait Co-Authored-By: Claude Opus 5.5 --- src/Repository/MemberResolver.php | 46 ++++++++++++++----------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/src/Repository/MemberResolver.php b/src/Repository/MemberResolver.php index 5d482737..af154ef9 100644 --- a/src/Repository/MemberResolver.php +++ b/src/Repository/MemberResolver.php @@ -362,10 +362,7 @@ private function passes(MemberInfoInterface $member, MemberFilter $filter): bool * The types to search after a class's own members, in PHP's resolution order: * used traits, then the parent chain, then interfaces. A trait edge may carry * the method names excluded from that trait by an `insteadof` clause on this - * class. Unresolvable types are skipped, and so is an edge naming a kind of - * class-like PHP rejects there: a class extends only a class, and - * `implements` or an interface's `extends` names only interfaces - * (`zend_do_inheritance_ex` and `zend_do_implement_interfaces` in php-src). + * class. Unresolvable types are skipped. * * Every member lookup walks the type graph through this one method, so all * member kinds see the same hierarchy. @@ -380,14 +377,14 @@ private function supertypes(ClassInfo $classInfo): array foreach ($this->usedTraits($classInfo) as [$trait, $exclusions]) { $supertypes[] = [$trait, true, $exclusions]; } - $parent = $classInfo->parent === null ? null : $this->source->lookupClassLike($classInfo->parent); - if ($parent?->isClass() === true) { - $supertypes[] = [$parent, false, []]; + $names = $classInfo->interfaces; + if ($classInfo->parent !== null) { + array_unshift($names, $classInfo->parent); } - foreach ($classInfo->interfaces as $name) { - $interface = $this->source->lookupClassLike($name); - if ($interface?->isInterface() === true) { - $supertypes[] = [$interface, false, []]; + foreach ($names as $name) { + $info = $this->source->lookupClassLike($name); + if ($info !== null) { + $supertypes[] = [$info, false, []]; } } @@ -397,16 +394,13 @@ private function supertypes(ClassInfo $classInfo): array /** * The traits a class-like uses, each with the method names an `insteadof` * clause excludes from it. PHP rejects `use` naming anything but a trait - * (`zend_do_link_class` in php-src), and an interface using traits - * (`zend_compile.c`). + * (`zend_do_link_class` in php-src), so a name that is not one brings in + * nothing. * * @return list}> */ private function usedTraits(ClassInfo $classInfo): array { - if ($classInfo->isInterface()) { - return []; - } $traits = []; foreach ($classInfo->traits as $name) { $trait = $this->source->lookupClassLike($name); @@ -531,20 +525,20 @@ private function resolveAlias(ClassInfo $origin, TraitAlias $alias): ?MethodInfo } /** - * The trait method an alias names, from the traits $origin uses: the one - * the alias names, which PHP requires to be used ("Required Trait %s - * wasn't added to %s", zend_inheritance.c), else the first that declares - * the method. + * An alias naming its trait draws only from that trait, which PHP requires + * $origin to use ("Required Trait %s wasn't added to %s", + * zend_inheritance.c). */ private function findAliasSource(ClassInfo $origin, TraitAlias $alias): ?MethodInfo { foreach ($this->usedTraits($origin) as [$trait]) { - if ($alias->trait !== null && !$alias->trait->equals($trait->name)) { - continue; - } - $method = $this->traitMethod($trait, $alias->method); - if ($method !== null || $alias->trait !== null) { - return $method; + if ($alias->trait === null) { + $method = $this->traitMethod($trait, $alias->method); + if ($method !== null) { + return $method; + } + } elseif ($alias->trait->equals($trait->name)) { + return $this->traitMethod($trait, $alias->method); } } From 0c2ebfe9350dd17b3cf1123fba8724cda1332b1f Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Thu, 29 Oct 2026 15:10:08 -0700 Subject: [PATCH 6/7] Keep the trait alias cycle as a known defect Co-Authored-By: Claude Opus 5.5 --- .../defects/889-trait-alias-cycle.php | 17 +++++++++++++++ .../EdgeCases/SelfUsingTraitAlias.php | 21 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/EndToEnd/defects/889-trait-alias-cycle.php create mode 100644 tests/Fixtures/EdgeCases/SelfUsingTraitAlias.php diff --git a/tests/EndToEnd/defects/889-trait-alias-cycle.php b/tests/EndToEnd/defects/889-trait-alias-cycle.php new file mode 100644 index 00000000..3dc72e8d --- /dev/null +++ b/tests/EndToEnd/defects/889-trait-alias-cycle.php @@ -0,0 +1,17 @@ +run(); //hover:self_alias_call + } +} From 5edd19eeca55dc1844a34327509af6151b2604ca Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Thu, 29 Oct 2026 15:10:09 -0700 Subject: [PATCH 7/7] Test a class aliasing from itself resolves its own method Co-Authored-By: Claude Opus 5.5 --- tests/Repository/MemberResolverTest.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/Repository/MemberResolverTest.php b/tests/Repository/MemberResolverTest.php index 258e15dc..8bdb0710 100644 --- a/tests/Repository/MemberResolverTest.php +++ b/tests/Repository/MemberResolverTest.php @@ -2319,6 +2319,29 @@ public function testUseNamingANonTraitBringsInNothing(ClassKind $kind, ClassKind ); } + /** + * A class naming itself in `use` uses no trait, so an alias adapting its + * own method finds no source rather than looking itself up again. + */ + public function testAClassAliasingFromItselfResolvesItsOwnMethod(): void + { + $origin = ClasslikeName::fromFullyQualified(self::fakeClass()); + $resolver = new MemberResolver($this->sourceOf($this->createClassInfo( + $origin, + methods: ['run' => $this->createMethodInfo('run', Visibility::Public, $origin)], + traits: [$origin], + traitAliases: [ + new TraitAlias(trait: null, method: 'run', newName: null, newVisibility: Visibility::Protected), + ], + ))); + + self::assertSame( + Visibility::Public, + $resolver->findMethod($origin, 'run', $origin)?->visibility, + 'the class\'s own method, unadapted', + ); + } + /** * PHP matches class names case-insensitively, so an alias naming its * trait in another case still draws from it.