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

TraitBuilder::render()   F

Complexity

Conditions 16
Paths 2064

Size

Total Lines 79
Code Lines 51

Duplication

Lines 39
Ratio 49.37 %

Importance

Changes 0
Metric Value
cc 16
eloc 51
c 0
b 0
f 0
nc 2064
nop 0
dl 39
loc 79
rs 2.1424

How to fix   Long Method    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
 * @copyright Copyright (c) 2017 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace JuliusHaertl\PHPDocToRst\Builder;
25
26
use phpDocumentor\Reflection\DocBlock\Tags\Param;
27
use phpDocumentor\Reflection\Php\Argument;
28
use phpDocumentor\Reflection\Php\Class_;
29
use phpDocumentor\Reflection\Php\Trait_;
30
31
class TraitBuilder extends Builder {
32
33
    protected function render() {
34
        /** @var Trait_ $trait */
35
        $trait = $this->element;
36
37
        $docBlock = $trait->getDocBlock();
38
39
        $this->addH1(self::escape($trait->getFqsen()));
40
41
        $namespace = str_replace('\\' . $trait->getName(), '', $trait->getFqsen());
42
        if($namespace !== '') {
43
            $this->beginPhpDomain('namespace', substr($namespace, 1), false);
44
        }
45
46
        $this->beginPhpDomain('trait', $trait->getName(), false);
47
48
        $this->indent();
49
        if ($docBlock) {
50
            $this
51
                ->addLine($docBlock->getDescription())
52
                ->addLine();
53
        }
54
55
        $usedTraits = '';
56
        foreach ($trait->getUsedTraits() as $trait) {
57
            $usedTraits .= $this->getLink('trait', $trait) . ' ';
58
        }
59
        $this->addFieldList('Traits', $usedTraits);
60
61
        $this->unindent();
62
        $this->addLine();
63
        $this->addLine();
64
65
        $this->addH2('Properties');
66
        foreach ($trait->getProperties() as $property) {
0 ignored issues
show
Bug introduced by
The method getProperties() does not exist on phpDocumentor\Reflection\Fqsen. ( Ignorable by Annotation )

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

66
        foreach ($trait->/** @scrutinizer ignore-call */ getProperties() as $property) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
            $this->addProperty($property);
68
        }
69
70
        $this->addH2('Methods');
71
        /* Render methods of a trait */
72 View Code Duplication
        foreach ($trait->getMethods() as $method) {
0 ignored issues
show
Bug introduced by
The method getMethods() does not exist on phpDocumentor\Reflection\Fqsen. ( Ignorable by Annotation )

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

72
        foreach ($trait->/** @scrutinizer ignore-call */ getMethods() as $method) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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...
73
            $docBlock = $method->getDocBlock();
74
            $params = [];
75
            if($docBlock !== null) {
76
                /** @var Param $param */
77
                foreach ($docBlock->getTagsByName('param') as $param) {
78
                    $params[$param->getVariableName()] = $param;
1 ignored issue
show
Bug introduced by
The method getVariableName() does not exist on phpDocumentor\Reflection\DocBlock\Tag. It seems like you code against a sub-type of phpDocumentor\Reflection\DocBlock\Tag such as phpDocumentor\Reflection\DocBlock\Tags\Property or phpDocumentor\Reflection\DocBlock\Tags\Var_ or phpDocumentor\Reflection\DocBlock\Tags\Param or phpDocumentor\Reflection...lock\Tags\PropertyWrite or phpDocumentor\Reflection...Block\Tags\PropertyRead. ( Ignorable by Annotation )

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

78
                    $params[$param->/** @scrutinizer ignore-call */ getVariableName()] = $param;
Loading history...
79
                }
80
            }
81
            $args = '';
82
            /** @var Argument $argument */
83
            foreach ($method->getArguments() as $argument) {
84
                // TODO: defaults, types
85
                $args .=  ' $' . $argument->getName() . ', ';
86
            }
87
            $args = substr($args, 0, -2);
88
89
            $modifiers = $method->getVisibility();
90
            $modifiers .= $method->isAbstract() ? ' abstract' : '';
91
            $modifiers .= $method->isFinal() ? ' final' : '';
92
            $modifiers .= $method->isStatic() ? ' static' : '';
93
            $this->addLine('.. rst-class:: ' . $modifiers)->addLine();
94
            $this->indent();
95
            $this->beginPhpDomain('method', $method->getName().'('.$args.')');
96
            if ($docBlock)
97
                $this->addMultiline($docBlock->getDescription());
98
            $this->addLine();
99
            if (!empty($params)) {
100
                foreach ($method->getArguments() as $argument) {
101
                    /** @var Param $param */
102
                    $param = $params[$argument->getName()];
103
                    if ($param !== null)
104
                        $this->addMultiline(':param ' . $param->getType() . ' $' . $argument->getName() . ': ' . $param->getDescription(), true);
105
                }
106
            }
107
            $this->endPhpDomain('method');
108
            $this->unindent();
109
110
        }
111
        $this->endPhpDomain(); //trait
112
    }
113
114
}