Skip to content

Commit 1fb04d1

Browse files
committed
- Fixed not recursing up traits for used traits (inherited traits)
1 parent a9b8c58 commit 1fb04d1

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/Trait/ComparableTrait.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,41 @@ final public static function hasComparableTrait(object $object): bool
9292
$reflectionClass = $object instanceof \ReflectionClass ?
9393
$object :
9494
(new \ReflectionClass($object));
95-
$traits = $reflectionClass->getTraits();
9695

97-
while ($ancestor = $reflectionClass->getParentClass()) {
98-
$traits = array_merge($traits, $ancestor->getTraits());
99-
}
96+
/**
97+
* @return array<int,\ReflectionClass>
98+
*/
99+
$getAncestryAndSelf = function (\ReflectionClass $reflection): array {
100+
$ancestorAndSelf = [$reflection];
101+
while ($ancestor = $reflection->getParentClass()) {
102+
$ancestorAndSelf[$ancestor];
103+
}
104+
105+
return $ancestorAndSelf;
106+
};
107+
108+
/**
109+
* @return array<string,array<int,string>>
110+
*/
111+
$getTraitsRecursive = function (
112+
\ReflectionClass $reflection,
113+
) use (&$getTraitsRecursive): array {
114+
$traits = $reflection->getTraits();
115+
if ($parent = $reflection->getParentClass()) {
116+
$traits = array_merge($traits, $getTraitsRecursive($parent));
117+
}
100118

101-
$traits = array_unique($traits);
119+
return $traits;
120+
};
121+
122+
$ancestorsAndSelf = $getAncestryAndSelf($object);
123+
$traits = [];
124+
125+
foreach ($ancestorsAndSelf as $reflection) {
126+
$traits = array_merge($traits, $getTraitsRecursive($reflection));
127+
}
102128

103-
return isset($reflectionClass->getTraits()[ComparableTrait::class]);
129+
return isset($traits[ComparableTrait::class]);
104130
}
105131

106132
/**

0 commit comments

Comments
 (0)