|
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
|
|
|
/** |
|
14
|
|
|
* @psalm-import-type OptionsDocBlock from Types |
|
15
|
|
|
* @psalm-import-type DocBlockParams from Types |
|
16
|
|
|
*/ |
|
17
|
|
|
final class OptionsMethodDocBolck |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Return docBloc and parameter metas of method |
|
21
|
|
|
* |
|
22
|
|
|
* @return OptionsDocBlock |
|
|
|
|
|
|
23
|
|
|
*/ |
|
24
|
|
|
public function __invoke(ReflectionMethod $method): array |
|
25
|
|
|
{ |
|
26
|
|
|
$docComment = $method->getDocComment(); |
|
27
|
|
|
$doc = $paramDoc = []; |
|
28
|
|
|
if (is_string($docComment)) { |
|
|
|
|
|
|
29
|
|
|
[$doc, $paramDoc] = $this->docBlock($docComment); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return [$doc, $paramDoc]; |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return (string|string[])[][] |
|
37
|
|
|
* @psalm-return OptionsDocBlock |
|
38
|
|
|
*/ |
|
39
|
|
|
private function docBlock(string $docComment): array |
|
40
|
|
|
{ |
|
41
|
|
|
$factory = DocBlockFactory::createInstance(); |
|
42
|
|
|
$docblock = $factory->create($docComment); |
|
43
|
|
|
$summary = $docblock->getSummary(); |
|
44
|
|
|
$docs = $params = []; |
|
45
|
|
|
if ($summary) { |
|
46
|
|
|
$docs['summary'] = $summary; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$description = (string) $docblock->getDescription(); |
|
50
|
|
|
if ($description) { |
|
51
|
|
|
$docs['description'] = $description; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** @var Param[] $tags */ |
|
55
|
|
|
$tags = $docblock->getTagsByName('param'); |
|
56
|
|
|
$params = $this->docBlogTags($tags, $params); |
|
57
|
|
|
|
|
58
|
|
|
return [$docs, $params]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param Param[] $tags |
|
63
|
|
|
* @param DocBlockParams $params |
|
|
|
|
|
|
64
|
|
|
* |
|
65
|
|
|
* @return DocBlockParams |
|
66
|
|
|
*/ |
|
67
|
|
|
private function docBlogTags(array $tags, array $params): array |
|
68
|
|
|
{ |
|
69
|
|
|
foreach ($tags as $tag) { |
|
70
|
|
|
$varName = (string) $tag->getVariableName(); |
|
71
|
|
|
$tagType = (string) $tag->getType(); |
|
72
|
|
|
$type = $tagType === 'int' ? 'integer' : $tagType; |
|
73
|
|
|
$params[$varName] = ['type' => $type]; |
|
74
|
|
|
$description = (string) $tag->getDescription(); |
|
75
|
|
|
if (! $description) { |
|
76
|
|
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$params[$varName]['description'] = $description; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $params; |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: