Completed
Push — code201 ( 677523...68154a )
by Akihito
05:57
created

OptionsMethodDocBolck::docBlock()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 1
1
<?php
2
/**
3
 * This file is part of the BEAR.Resource package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Resource;
8
9
use phpDocumentor\Reflection\DocBlockFactory;
10
11
final class OptionsMethodDocBolck
12
{
13
    /**
14
     * Return docBloc and parameter metas of method
15
     */
16
    public function __invoke(\ReflectionMethod $method) : array
17
    {
18
        $docComment = $method->getDocComment();
19
        $doc = $paramDoc = [];
20
        if ($docComment) {
21
            list($doc, $paramDoc) = $this->docBlock($docComment);
22
        }
23
24
        return [$doc, $paramDoc];
25
    }
26
27
    /**
28
     * @return array [$docs, $params]
29
     */
30
    private function docBlock(string $docComment) : array
31
    {
32
        $factory = DocBlockFactory::createInstance();
33
        $docblock = $factory->create($docComment);
34
        $summary = $docblock->getSummary();
35
        $docs = $params = [];
36
        if ($summary) {
37
            $docs['summary'] = $summary;
38
        }
39
        $description = (string) $docblock->getDescription();
40
        if ($description) {
41
            $docs['description'] = $description;
42
        }
43
        $tags = $docblock->getTagsByName('param');
44
        $params = $this->docBlogTags($tags, $params);
45
46
        return [$docs, $params];
47
    }
48
49
    private function docBlogTags(array $tags, array $params) : array
50
    {
51
        foreach ($tags as $tag) {
52
            /* @var $tag \phpDocumentor\Reflection\DocBlock\Tags\Param */
53
            $varName = $tag->getVariableName();
54
            $tagType = (string) $tag->getType();
55
            $type = $tagType === 'int' ? 'integer' : $tagType;
56
            $params[$varName] = [
57
                'type' => $type
58
            ];
59
            $description = (string) $tag->getDescription();
60
            if ($description) {
61
                $params[$varName]['description'] = $description;
62
            }
63
        }
64
65
        return $params;
66
    }
67
}
68