Completed
Push — master ( d582b4...acdf9f )
by Greg
02:27
created

CommandDocBlockParser3::processUsageTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
namespace Consolidation\AnnotatedCommand\Parser\Internal;
3
4
use phpDocumentor\Reflection\DocBlock\Tags\Param;
5
use phpDocumentor\Reflection\DocBlock\Tags\Return_;
6
use Consolidation\AnnotatedCommand\Parser\CommandInfo;
7
use Consolidation\AnnotatedCommand\Parser\DefaultsWithDescriptions;
8
9
/**
10
 * Given a class and method name, parse the annotations in the
11
 * DocBlock comment, and provide accessor methods for all of
12
 * the elements that are needed to create an annotated Command.
13
 */
14
class CommandDocBlockParser3 extends AbstractCommandDocBlockParser
15
{
16
    /**
17
     * Parse the docBlock comment for this command, and set the
18
     * fields of this class with the data thereby obtained.
19
     */
20
    public function parse()
21
    {
22
        // DocBlockFactory::create fails if the comment is empty.
23
        if (empty($this->reflection->getDocComment())) {
24
            return;
25
        }
26
        $phpdoc = $this->createDocBlock();
27
28
        // First set the description (synopsis) and help.
29
        $this->commandInfo->setDescription((string)$phpdoc->getSummary());
30
        $this->commandInfo->setHelp((string)$phpdoc->getDescription());
31
32
        $this->processAllTags($phpdoc);
33
    }
34
35
    public function createDocBlock()
36
    {
37
        $docBlockFactory = \phpDocumentor\Reflection\DocBlockFactory::createInstance();
38
        $contextFactory = new \phpDocumentor\Reflection\Types\ContextFactory();
39
40
        return $docBlockFactory->create(
41
            $this->reflection,
42
            $contextFactory->createFromReflector($this->reflection)
43
        );
44
    }
45
46
    protected function getTagContents($tag)
47
    {
48
        return (string)$tag;
49
    }
50
51
    /**
52
     * Store the data from a @param annotation in our argument descriptions.
53
     */
54
    protected function processParamTag($tag)
55
    {
56
        if (!$tag instanceof Param) {
57
            return;
58
        }
59
        return parent::processParamTag($tag);
60
    }
61
62
    /**
63
     * Store the data from a @return annotation in our argument descriptions.
64
     */
65
    protected function processReturnTag($tag)
66
    {
67
        if (!$tag instanceof Return_) {
68
            return;
69
        }
70
        // If there is a spurrious trailing space on the return type, remove it.
71
        $this->commandInfo->setReturnType(trim($this->getTagContents($tag)));
72
    }
73
}
74