1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace BEAR\Resource; |
||
6 | |||
7 | use phpDocumentor\Reflection\DocBlock\Tags\Param; |
||
0 ignored issues
–
show
|
|||
8 | use phpDocumentor\Reflection\DocBlockFactory; |
||
9 | use ReflectionMethod; |
||
10 | |||
11 | final class OptionsMethodDocBolck |
||
12 | { |
||
13 | /** |
||
14 | * Return docBloc and parameter metas of method |
||
15 | * |
||
16 | * @return array{0: array{summary?: string, description?: string}, 1: array<string, array{type: string, description?: string}>} |
||
17 | */ |
||
18 | public function __invoke(ReflectionMethod $method): array |
||
19 | { |
||
20 | $docComment = $method->getDocComment(); |
||
21 | $doc = $paramDoc = []; |
||
22 | if ($docComment) { |
||
23 | [$doc, $paramDoc] = $this->docBlock($docComment); |
||
24 | } |
||
25 | |||
26 | return [$doc, $paramDoc]; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @return (string|string[])[][] |
||
31 | * @psalm-return array{0: array{summary?: string, description?: string}, 1: array<string, array{type: string, description?: string}>} |
||
32 | */ |
||
33 | private function docBlock(string $docComment): array |
||
34 | { |
||
35 | $factory = DocBlockFactory::createInstance(); |
||
36 | $docblock = $factory->create($docComment); |
||
37 | $summary = $docblock->getSummary(); |
||
38 | $docs = $params = []; |
||
39 | if ($summary) { |
||
40 | $docs['summary'] = $summary; |
||
41 | } |
||
42 | |||
43 | $description = (string) $docblock->getDescription(); |
||
44 | if ($description) { |
||
45 | $docs['description'] = $description; |
||
46 | } |
||
47 | |||
48 | /** @var Param[] $tags */ |
||
49 | $tags = $docblock->getTagsByName('param'); |
||
50 | $params = $this->docBlogTags($tags, $params); |
||
51 | |||
52 | return [$docs, $params]; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param Param[] $tags |
||
57 | * @param array<string, array{type: string, description?: string}> $params |
||
58 | * |
||
59 | * @return array<string, array{type: string, description?: string}> |
||
60 | */ |
||
61 | private function docBlogTags(array $tags, array $params): array |
||
62 | { |
||
63 | foreach ($tags as $tag) { |
||
64 | $varName = (string) $tag->getVariableName(); |
||
65 | $tagType = (string) $tag->getType(); |
||
66 | $type = $tagType === 'int' ? 'integer' : $tagType; |
||
67 | $params[$varName] = ['type' => $type]; |
||
68 | $description = (string) $tag->getDescription(); |
||
69 | if (! $description) { |
||
70 | continue; |
||
71 | } |
||
72 | |||
73 | $params[$varName]['description'] = $description; |
||
74 | } |
||
75 | |||
76 | return $params; |
||
77 | } |
||
78 | } |
||
79 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: