Skip to content

Commit

Permalink
Simplify the collector implementation (#1611)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann authored Jun 27, 2024
1 parent 1c7330d commit 009c74f
Showing 1 changed file with 20 additions and 39 deletions.
59 changes: 20 additions & 39 deletions src/Processors/Concerns/CollectorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,38 @@
trait CollectorTrait
{
/**
* Collects a complete list of all nested/referenced annotations.
* Collects a (complete) list of all nested/referenced annotations starting from the given root.
*/
public function collect(iterable $annotations): \SplObjectStorage
public function collect(iterable $root): \SplObjectStorage
{
$storage = new \SplObjectStorage();

foreach ($annotations as $annotation) {
if ($annotation instanceof OA\AbstractAnnotation) {
$storage->addAll($this->traverse($annotation));
}
}

return $storage;
}

public function traverse(OA\AbstractAnnotation $annotation): \SplObjectStorage
{
$storage = new \SplObjectStorage();

if ($storage->contains($annotation)) {
return $storage;
}

$storage->attach($annotation);

foreach (array_merge($annotation::$_nested, ['allOf', 'anyOf', 'oneOf', 'callbacks']) as $properties) {
foreach ((array) $properties as $property) {
if (isset($annotation->{$property})) {
$storage->addAll($this->traverseNested($annotation->{$property}));
}
}
}
$this->traverse($root, function (OA\AbstractAnnotation $annotation) use (&$storage) {
$storage->attach($annotation);
});

return $storage;
}

/**
* @param string|array|OA\AbstractAnnotation $nested
* @param string|array|OA\AbstractAnnotation $root
*/
protected function traverseNested($nested): \SplObjectStorage
public function traverse($root, callable $callable): void
{
$storage = new \SplObjectStorage();

if (is_array($nested)) {
foreach ($nested as $value) {
$storage->addAll($this->traverseNested($value));
if (is_iterable($root)) {
foreach ($root as $value) {
$this->traverse($value, $callable);
}
} elseif ($root instanceof OA\AbstractAnnotation) {
$callable($root);

foreach (array_merge($root::$_nested, ['allOf', 'anyOf', 'oneOf', 'callbacks']) as $properties) {
foreach ((array) $properties as $property) {
if (isset($root->{$property})) {
$this->traverse($root->{$property}, $callable);
}
}
}
} elseif ($nested instanceof OA\AbstractAnnotation) {
$storage->addAll($this->traverse($nested));
}

return $storage;
}
}

0 comments on commit 009c74f

Please sign in to comment.