Issues (108)

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
/** @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];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($doc, $paramDoc) returns the type array<integer,array|array<string,array>> which is incompatible with the documented return type BEAR\Resource\OptionsDocBlock.
Loading history...
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