@@ -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