1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\Resource; |
6
|
|
|
|
7
|
|
|
use phpDocumentor\Reflection\DocBlock\Tags\Param; |
|
|
|
|
8
|
|
|
use phpDocumentor\Reflection\DocBlockFactory; |
9
|
|
|
use ReflectionMethod; |
10
|
|
|
|
11
|
|
|
use function is_string; |
12
|
|
|
|
13
|
|
|
/** @psalm-import-type OptionsDocBlock from Types */ |
14
|
|
|
final class OptionsMethodDocBolck |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Return docBloc and parameter metas of method |
18
|
|
|
* |
19
|
|
|
* @return OptionsDocBlock |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
public function __invoke(ReflectionMethod $method): array |
22
|
|
|
{ |
23
|
|
|
$docComment = $method->getDocComment(); |
24
|
|
|
$doc = $paramDoc = []; |
25
|
|
|
if (is_string($docComment)) { |
|
|
|
|
26
|
|
|
[$doc, $paramDoc] = $this->docBlock($docComment); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return [$doc, $paramDoc]; |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return (string|string[])[][] |
34
|
|
|
* @psalm-return OptionsDocBlock |
35
|
|
|
*/ |
36
|
|
|
private function docBlock(string $docComment): array |
37
|
|
|
{ |
38
|
|
|
$factory = DocBlockFactory::createInstance(); |
39
|
|
|
$docblock = $factory->create($docComment); |
40
|
|
|
$summary = $docblock->getSummary(); |
41
|
|
|
$docs = $params = []; |
42
|
|
|
if ($summary) { |
43
|
|
|
$docs['summary'] = $summary; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$description = (string) $docblock->getDescription(); |
47
|
|
|
if ($description) { |
48
|
|
|
$docs['description'] = $description; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @var Param[] $tags */ |
52
|
|
|
$tags = $docblock->getTagsByName('param'); |
53
|
|
|
$params = $this->docBlogTags($tags, $params); |
54
|
|
|
|
55
|
|
|
return [$docs, $params]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Param[] $tags |
60
|
|
|
* @param array<string, array{type: string, description?: string}> $params |
61
|
|
|
* |
62
|
|
|
* @return array<string, array{type: string, description?: string}> |
63
|
|
|
*/ |
64
|
|
|
private function docBlogTags(array $tags, array $params): array |
65
|
|
|
{ |
66
|
|
|
foreach ($tags as $tag) { |
67
|
|
|
$varName = (string) $tag->getVariableName(); |
68
|
|
|
$tagType = (string) $tag->getType(); |
69
|
|
|
$type = $tagType === 'int' ? 'integer' : $tagType; |
70
|
|
|
$params[$varName] = ['type' => $type]; |
71
|
|
|
$description = (string) $tag->getDescription(); |
72
|
|
|
if (! $description) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$params[$varName]['description'] = $description; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $params; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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: