Skip to content

Commit

Permalink
Add wildcard argument to AugmentTags whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Johnston committed Aug 28, 2024
1 parent 0904ea0 commit 8035bcb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
25 changes: 18 additions & 7 deletions src/Processors/AugmentTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ class AugmentTags implements ProcessorInterface
{

/** @var array<string> */
protected $unusedTagsToKeepWhitelist = [];
protected $whitelist;

public function __construct(array $unusedTagsToKeepWhitelist = [])
public function __construct(array $whitelist = [])
{
$this->unusedTagsToKeepWhitelist = $unusedTagsToKeepWhitelist;
$this->whitelist = $whitelist;
}

public function setUnusedTagsToKeepWhitelist(array $unusedTagsToKeepWhitelist): AugmentTags
/**
* Whitelist tags to keep even if not used. <code>*</code> may be used to keep all unused.
*/
public function setWhitelist(array $whitelist): AugmentTags
{
$this->unusedTagsToKeepWhitelist = $unusedTagsToKeepWhitelist;
$this->whitelist = $whitelist;

return $this;
}
Expand Down Expand Up @@ -65,8 +68,16 @@ public function __invoke(Analysis $analysis)
}
}

// remove tags that we don't want to keep (defaults to all unused tags)
$tagsToKeep = array_merge($usedTagNames, $this->unusedTagsToKeepWhitelist);
$this->removeUnusedTags($usedTagNames, $declaredTags, $analysis);
}

private function removeUnusedTags(array $usedTagNames, array $declaredTags, Analysis $analysis)
{
if (in_array('*', $this->whitelist)) {
return;
}

$tagsToKeep = array_merge($usedTagNames, $this->whitelist);
foreach ($declaredTags as $tag) {
if (!in_array($tag->name, $tagsToKeep)) {
if (false !== $index = array_search($tag, $analysis->openapi->tags, true)) {
Expand Down
21 changes: 20 additions & 1 deletion tests/Processors/AugmentTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,29 @@ public function testAllowUnusedTags(): void
static::processors(),
null,
[
'augmentTags' => ['unusedTagsToKeepWhitelist' => ['fancy']],
'augmentTags' => ['whitelist' => ['fancy']],
]
);

$this->assertCount(2, $analysis->openapi->tags, 'Expecting fancy tag to be preserved');
}

/**
* @requires PHP 8.1
*/
public function testAllowUnusedTagsWildcard(): void
{
$this->skipLegacy();

$analysis = $this->analysisFromFixtures(
['UnusedTags.php'],
static::processors(),
null,
[
'augmentTags' => ['whitelist' => ['*']],
]
);

$this->assertCount(3, $analysis->openapi->tags, 'Expecting all tags to be preserved');
}
}

0 comments on commit 8035bcb

Please sign in to comment.