Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify the collector implementation #1611

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}