Completed
Push — master ( 1a3dd2...a893e4 )
by Julius
02:37
created

PhpDomainBuilder::addDocblockTag()   C

Complexity

Conditions 14
Paths 14

Size

Total Lines 45
Code Lines 36

Duplication

Lines 36
Ratio 80 %

Importance

Changes 0
Metric Value
cc 14
eloc 36
c 0
b 0
f 0
nc 14
nop 2
dl 36
loc 45
rs 5.0864

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jus
5
 * Date: 07.10.17
6
 * Time: 23:25
7
 */
8
9
namespace JuliusHaertl\PHPDocToRst\Builder;
10
11
use phpDocumentor\Reflection\DocBlock;
12
use phpDocumentor\Reflection\DocBlock\Tags\Return_;
13
use phpDocumentor\Reflection\DocBlock\Tags\See;
14
use phpDocumentor\Reflection\DocBlock\Tags\Since;
15
use phpDocumentor\Reflection\DocBlock\Tags\Throws;
16
use phpDocumentor\Reflection\Php\Constant;
17
use phpDocumentor\Reflection\Php\Property;
18
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated;
19
20
class PhpDomainBuilder extends RstBuilder {
21
22
    protected function addConstant(Constant $constant) {
23
        $this->beginPhpDomain('const', $constant->getName() . ' = ' . $constant->getValue());
24
        $docBlock = $constant->getDocBlock();
25
        if ($docBlock) {
26
            foreach ($docBlock->getTags() as $tag) {
27
                $this->addDocblockTag( $tag->getName(), $docBlock);
28
            }
29
        }
30
        $this->endPhpDomain();
31
    }
32
33
    protected function addProperty(Property $property) {
34
        $this->beginPhpDomain('attr', $property->getName());
35
        $this->endPhpDomain();
36
    }
37
    public function getLink($type, $fqsen) {
38
        return ':php:' . $type . ':`' . RstBuilder::escape(substr($fqsen, 1)) . '`';
39
    }
40
41
    public function beginPhpDomain($type, $name, $indent=true) {
42
        // FIXME: Add checks if it is properly ended
43
        $this->addLine('.. php:' . $type . ':: '. $name)->addLine();
44
        if ($indent === true) {
45
            $this->indent();
46
        }
47
    }
48
49
    public function endPhpDomain($type='') {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

49
    public function endPhpDomain(/** @scrutinizer ignore-unused */ $type='') {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
        $this->unindent();
51
        $this->addLine();
52
    }
53
54
    /**
55
     * @param string $tag Name of the tag to parse
56
     * @param DocBlock $docBlock
57
     */
58
    protected function addDocblockTag($tag, DocBlock $docBlock) {
59
        $tags = $docBlock->getTagsByName($tag);
60
        switch ($tag) {
61 View Code Duplication
            case 'return':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
                if (count($tags) === 0) continue;
63
                /** @var Return_ $return */
64
                $return = $tags[0];
65
                $this->addMultiline(':returns: ' . $return->getType() . ' ' . RstBuilder::escape($return->getDescription()), true);
66
                break;
67 View Code Duplication
            case 'throws':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
                if (count($tags) === 0) continue;
69
                /** @var Throws $return */
70
                $return = $tags[0];
71
                $this->addMultiline(':throws: ' . $return->getType() . ' ' . RstBuilder::escape($return->getDescription()), true);
72
                break;
73 View Code Duplication
            case 'since':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
                if (count($tags) === 0) continue;
75
                /** @var Since $return */
76
                $return = $tags[0];
77
                $this->addMultiline(':since: ' . $return->getVersion() . ' ' . RstBuilder::escape($return->getDescription()), true);
78
                break;
79 View Code Duplication
            case 'deprecated':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
                if (count($tags) === 0) continue;
81
                /** @var Deprecated $return */
82
                $return = $tags[0];
83
                $this->addMultiline(':deprecated: ' . $return->getVersion() . ' ' . RstBuilder::escape($return->getDescription()), true);
84
                break;
85 View Code Duplication
            case 'see':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
                if (count($tags) === 0) continue;
87
                /** @var See $return */
88
                $return = $tags[0];
89
                $this->addMultiline(':see: ' . $return->getReference() . ' ' . RstBuilder::escape($return->getDescription()), true);
90
                break;
91 View Code Duplication
            case 'license':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
                if (count($tags) === 0) continue;
93
                /** @var DocBlock\Tags\BaseTag $return */
94
                $return = $tags[0];
95
                $this->addMultiline(':license: ' . RstBuilder::escape($return->getDescription()), true);
96
                break;
97
            case 'param':
98
                // param handling is done by subclasses since it is more that docbook parsing
99
                break;
100
            default:
101
                //echo 'Tag handling not defined for: ' . $tag . PHP_EOL;
102
                break;
103
        }
104
105
    }
106
107
}