Passed
Push — master ( a893e4...e78a9c )
by Julius
02:11
created

TraitFileBuilder::render()   F

Complexity

Conditions 14
Paths 520

Size

Total Lines 74
Code Lines 47

Duplication

Lines 8
Ratio 10.81 %

Importance

Changes 0
Metric Value
cc 14
eloc 47
nc 520
nop 0
dl 8
loc 74
rs 3.2637
c 0
b 0
f 0

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 TraitFileBuilder extends FileBuilder {
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->addProperties($trait->getProperties());
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

65
        $this->addProperties($trait->/** @scrutinizer ignore-call */ getProperties());

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...
66
67
        $this->addH2('Methods');
68
        /* Render methods of a trait */
69
        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

69
        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...
70
            $docBlock = $method->getDocBlock();
71
            $params = [];
72
            if($docBlock !== null) {
73
                /** @var Param $param */
74
                foreach ($docBlock->getTagsByName('param') as $param) {
75
                    $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

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