diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index 098b2101bc..ea21710247 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -977,7 +977,7 @@ public function hasTraitUse(string $traitName): bool private function getTraitNames(): array { $class = $this->reflection; - $traitNames = $class->getTraitNames(); + $traitNames = array_map(static fn (ReflectionClass $class) => $class->getName(), $this->collectTraits($class)); while ($class->getParentClass() !== false) { $traitNames = array_values(array_unique(array_merge($traitNames, $class->getParentClass()->getTraitNames()))); $class = $class->getParentClass(); diff --git a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php index 5fe483b857..827c65e649 100644 --- a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php +++ b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php @@ -272,4 +272,11 @@ public function testBug8204(): void $this->analyse([__DIR__ . '/data/bug-8204.php'], []); } + public function testBug8850(): void + { + $this->alwaysWrittenTags = []; + $this->alwaysReadTags = []; + $this->analyse([__DIR__ . '/data/bug-8850.php'], []); + } + } diff --git a/tests/PHPStan/Rules/DeadCode/data/bug-8850.php b/tests/PHPStan/Rules/DeadCode/data/bug-8850.php new file mode 100644 index 0000000000..96ab4f318a --- /dev/null +++ b/tests/PHPStan/Rules/DeadCode/data/bug-8850.php @@ -0,0 +1,50 @@ +security = $security; + } +} + +trait QueryBuilderHelperTrait +{ + use OrganisationExtensionHelperTrait; +} + +trait OrganisationExtensionHelperTrait +{ + use UserHelperTrait; + + public function getOrganisationIds(): void + { + $user = $this->getUser(); + } +} + +trait UserHelperTrait +{ + public function getUser(): string + { + $user = $this->security->getUser(); + + return 'foo'; + } +} diff --git a/tests/PHPStan/Type/ObjectTypeTest.php b/tests/PHPStan/Type/ObjectTypeTest.php index be6c66eeb1..849125b80d 100644 --- a/tests/PHPStan/Type/ObjectTypeTest.php +++ b/tests/PHPStan/Type/ObjectTypeTest.php @@ -4,6 +4,8 @@ use ArrayAccess; use ArrayObject; +use Bug8850\UserHelperTrait; +use Bug8850\UserInSessionInRoleEndpointExtension; use Closure; use Countable; use DateInterval; @@ -432,6 +434,11 @@ public function dataIsSuperTypeOf(): array new ObjectType(DateTime::class), TrinaryLogic::createNo(), ], + 61 => [ + new ObjectType(UserInSessionInRoleEndpointExtension::class), + new ThisType($reflectionProvider->getClass(UserInSessionInRoleEndpointExtension::class), null, $reflectionProvider->getClass(UserHelperTrait::class)), + TrinaryLogic::createMaybe(), + ], ]; }