diff --git a/src/Analysis.php b/src/Analysis.php index f20b5292..dc78b8f3 100644 --- a/src/Analysis.php +++ b/src/Analysis.php @@ -297,28 +297,22 @@ public function getTraitsOfClass(string $source, bool $direct = false): array } /** - * @param string|array $classes One ore more class names - * @param bool $strict in non-strict mode child classes are also detected + * @param class-string|array $classes one or more class names + * @param bool $strict in non-strict mode child classes are also detected * * @return OA\AbstractAnnotation[] */ public function getAnnotationsOfType($classes, bool $strict = false): array { + $unique = new \SplObjectStorage(); $annotations = []; - if ($strict) { - foreach ((array) $classes as $class) { - foreach ($this->annotations as $annotation) { - if (get_class($annotation) === $class) { - $annotations[] = $annotation; - } - } - } - } else { - foreach ((array) $classes as $class) { - foreach ($this->annotations as $annotation) { - if ($annotation instanceof $class) { - $annotations[] = $annotation; - } + + foreach ((array) $classes as $class) { + /** @var OA\AbstractAnnotation $annotation */ + foreach ($this->annotations as $annotation) { + if ($annotation instanceof $class && (!$strict || ($annotation->isRoot($class) && !$unique->contains($annotation)))) { + $unique->attach($annotation); + $annotations[] = $annotation; } } } diff --git a/tests/Fixtures/Scratch/AttributeInheritance.php b/tests/Fixtures/Scratch/AttributeInheritance.php new file mode 100644 index 00000000..68147299 --- /dev/null +++ b/tests/Fixtures/Scratch/AttributeInheritance.php @@ -0,0 +1,48 @@ +