Issues (182)

src/OptionsMethodDocBolck.php (1 issue)

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)) {
0 ignored issues
show
The condition is_string($docComment) is always true.
Loading history...
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