humbug /
box
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of the box project. |
||
| 7 | * |
||
| 8 | * (c) Kevin Herrera <[email protected]> |
||
| 9 | * Théo Fidry <[email protected]> |
||
| 10 | * |
||
| 11 | * This source file is subject to the MIT license that is bundled |
||
| 12 | * with this source code in the file LICENSE. |
||
| 13 | */ |
||
| 14 | |||
| 15 | namespace KevinGH\Box\Annotation; |
||
| 16 | |||
| 17 | use InvalidArgumentException; |
||
| 18 | use phpDocumentor\Reflection\DocBlock; |
||
| 19 | use phpDocumentor\Reflection\DocBlock\Tag; |
||
| 20 | use phpDocumentor\Reflection\DocBlock\Tags\Formatter; |
||
| 21 | use phpDocumentor\Reflection\DocBlockFactoryInterface; |
||
| 22 | use function array_filter; |
||
| 23 | use function array_flip; |
||
| 24 | use function array_key_exists; |
||
| 25 | use function array_map; |
||
| 26 | use function array_values; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @private |
||
| 30 | */ |
||
| 31 | final class DocblockAnnotationParser |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var array<string, mixed> |
||
| 35 | */ |
||
| 36 | private readonly array $ignoredAnnotationsAsKeys; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param string[] $ignoredAnnotations |
||
| 40 | */ |
||
| 41 | public function __construct( |
||
| 42 | private readonly DocBlockFactoryInterface $factory, |
||
| 43 | private readonly Formatter $tagsFormatter, |
||
| 44 | array $ignoredAnnotations, |
||
| 45 | ) { |
||
| 46 | $this->ignoredAnnotationsAsKeys = array_flip($ignoredAnnotations); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @return string[] Parsed compacted annotations parsed from the docblock |
||
| 51 | */ |
||
| 52 | public function parse(string $docblock): array |
||
| 53 | { |
||
| 54 | $doc = $this->createDocBlock($docblock); |
||
| 55 | |||
| 56 | $tags = self::extractTags($doc, $this->ignoredAnnotationsAsKeys); |
||
| 57 | |||
| 58 | return array_map( |
||
| 59 | fn (Tag $tag) => $tag->render($this->tagsFormatter), |
||
| 60 | $tags, |
||
| 61 | ); |
||
| 62 | } |
||
| 63 | |||
| 64 | private function createDocBlock(string $docblock): DocBlock |
||
| 65 | { |
||
| 66 | try { |
||
| 67 | return $this->factory->create($docblock); |
||
| 68 | } catch (InvalidArgumentException $invalidDocBlock) { |
||
| 69 | throw new MalformedTagException( |
||
| 70 | 'The annotations could not be parsed.', |
||
| 71 | 0, |
||
| 72 | $invalidDocBlock, |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param array<string, mixed> $ignoredAnnotations |
||
| 79 | * |
||
| 80 | * @return list<string> |
||
|
0 ignored issues
–
show
The type
KevinGH\Box\Annotation\list was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 81 | */ |
||
| 82 | private static function extractTags(DocBlock $docBlock, array $ignoredAnnotations): array |
||
| 83 | { |
||
| 84 | return array_values( |
||
|
0 ignored issues
–
show
|
|||
| 85 | array_filter( |
||
| 86 | $docBlock->getTags(), |
||
| 87 | static fn (Tag $tag) => !array_key_exists( |
||
| 88 | mb_strtolower($tag->getName()), |
||
| 89 | $ignoredAnnotations, |
||
| 90 | ), |
||
| 91 | ), |
||
| 92 | ); |
||
| 93 | } |
||
| 94 | } |
||
| 95 |