From 33960e0c273632af319f6867469cb96308cb4db4 Mon Sep 17 00:00:00 2001 From: Martin Rademacher Date: Wed, 8 Mar 2023 15:25:26 +1300 Subject: [PATCH] Use `AbstractAnnotation::isRoot()` as strict check in `Analysis::getAnnotationsOfType()` (#1425) --- src/Analysis.php | 26 ++++------ .../Fixtures/Scratch/AttributeInheritance.php | 48 +++++++++++++++++++ .../Scratch/AttributeInheritance.yaml | 36 ++++++++++++++ 3 files changed, 94 insertions(+), 16 deletions(-) create mode 100644 tests/Fixtures/Scratch/AttributeInheritance.php create mode 100644 tests/Fixtures/Scratch/AttributeInheritance.yaml diff --git a/src/Analysis.php b/src/Analysis.php index f20b5292a..dc78b8f3c 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 000000000..68147299c --- /dev/null +++ b/tests/Fixtures/Scratch/AttributeInheritance.php @@ -0,0 +1,48 @@ +