Skip to content

Commit

Permalink
Remove unused tags (zircote#1625)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann authored Jul 10, 2024
1 parent ad3f913 commit 88895af
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
34 changes: 23 additions & 11 deletions src/Processors/AugmentTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,36 @@ public function __invoke(Analysis $analysis)
/** @var OA\Operation[] $operations */
$operations = $analysis->getAnnotationsOfType(OA\Operation::class);

$usedTags = [];
$usedTagNames = [];
foreach ($operations as $operation) {
if (!Generator::isDefault($operation->tags)) {
$usedTags = array_merge($usedTags, $operation->tags);
$usedTagNames = array_merge($usedTagNames, $operation->tags);
}
}
$usedTagNames = array_unique($usedTagNames);

if ($usedTags) {
$usedTags = array_unique($usedTags);
$declaredTags = [];
if (!Generator::isDefault($analysis->openapi->tags)) {
foreach ($analysis->openapi->tags as $tag) {
$declaredTags[] = $tag->name;
$declaredTags = [];
if (!Generator::isDefault($analysis->openapi->tags)) {
foreach ($analysis->openapi->tags as $tag) {
$declaredTags[$tag->name] = $tag;
}
}

if ($usedTagNames) {
$declatedTagNames = array_keys($declaredTags);
foreach ($usedTagNames as $tagName) {
if (!in_array($tagName, $declatedTagNames)) {
$analysis->openapi->merge([new OA\Tag(['name' => $tagName, 'description' => $tagName])]);
}
}
foreach ($usedTags as $tag) {
if (!in_array($tag, $declaredTags)) {
$analysis->openapi->merge([new OA\Tag(['name' => $tag, 'description' => $tag])]);
}

foreach ($declaredTags as $tag) {
if (!in_array($tag->name, $usedTagNames)) {
if (false !== $index = array_search($tag, $analysis->openapi->tags)) {
$analysis->annotations->detach($tag);
unset($analysis->openapi->tags[$index]);
$analysis->openapi->tags = array_values($analysis->openapi->tags);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion tests/Fixtures/Scratch/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

use OpenApi\Attributes as OAT;

#[OAT\Tag(name: 'sandbox', description: 'Sandbox tag')]
#[OAT\Tag(name: 'unused', description: 'Not used')]
#[OAT\Info(
title: 'Tags',
description: 'Tag Scratch',
Expand All @@ -18,7 +20,7 @@
#[OAT\Get(
path: '/endpoint',
description: 'Sandbox endpoint',
tags: ['sandbox'],
tags: ['sandbox', 'other'],
responses: [
new OAT\Response(
response: 200,
Expand Down
6 changes: 5 additions & 1 deletion tests/Fixtures/Scratch/Tags3.0.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ paths:
get:
tags:
- sandbox
- other
description: 'Sandbox endpoint'
operationId: f1c7f5a59b9708596c2bc955017002e6
responses:
Expand All @@ -19,4 +20,7 @@ paths:
tags:
-
name: sandbox
description: sandbox
description: 'Sandbox tag'
-
name: other
description: other
6 changes: 5 additions & 1 deletion tests/Fixtures/Scratch/Tags3.1.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ paths:
get:
tags:
- sandbox
- other
description: 'Sandbox endpoint'
operationId: f1c7f5a59b9708596c2bc955017002e6
responses:
Expand All @@ -19,4 +20,7 @@ paths:
tags:
-
name: sandbox
description: sandbox
description: 'Sandbox tag'
-
name: other
description: other

0 comments on commit 88895af

Please sign in to comment.