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

ClassFileBuilder   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 113
Duplicated Lines 6.19 %

Importance

Changes 0
Metric Value
dl 7
loc 113
rs 10
c 0
b 0
f 0
wmc 28

1 Method

Rating   Name   Duplication   Size   Complexity  
F render() 7 111 28

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
30
class ClassFileBuilder extends FileBuilder {
31
32
    protected function render() {
33
34
        /** @var Class_ $class */
35
        $class = $this->element;
36
37
        if (!$this->shouldRenderElement($class)) {
38
            return;
39
        }
40
41
        $docBlock = $class->getDocBlock();
42
43
        $this->addH1(self::escape($class->getName()));
44
45
        $namespace = substr($class->getFqsen(), 0, strlen($class->getFqsen())-strlen('\\' . $class->getName()));
46
        if($namespace !== '') {
47
            $this->beginPhpDomain('namespace', substr($namespace, 1), false);
48
        }
49
50
        $modifiers = $class->isAbstract() ? ' abstract' : '';
51
        $modifiers = $class->isFinal() ? ' final' : $modifiers;
52
        if ($modifiers !== '') {
53
            $this->addLine('.. rst-class:: ' . $modifiers)->addLine();
54
        }
55
        $this->beginPhpDomain('class', $class->getName(), false);
56
57
        $this->indent();
58
        $this->addDocBlockDescription($docBlock);
59
60
        if($class instanceof Class_) {
61
            // Add class details
62
            $parent = $class->getParent();
63
            if ($parent !== null) {
64
                $this->addFieldList('Extends', $parent !== null ? $this->getLink('class', $parent) : '');
65
            }
66
        }
67
        $implementedInterfaces = '';
68
        foreach ($class->getInterfaces() as $int) {
69
            $implementedInterfaces .= $this->getLink('interface', $int) . ' ';
70
        }
71
        if ($implementedInterfaces !== '') {
72
            $this->addFieldList('Implements', $implementedInterfaces);
73
        }
74
75
        $usedTraits = '';
76
        foreach ($class->getUsedTraits() as $trait) {
77
            $usedTraits .= $this->getLink('trait', $trait) . ' ';
78
        }
79
        if ($usedTraits !== '') {
80
            $this->addFieldList('Used traits', $usedTraits);
81
        }
82
83
        $this->unindent();
84
        $this->addLine();
85
        $this->addLine();
86
87
        // Add class constants
88
        if (count($class->getConstants()) > 0) {
89
            $this->addH2('Constants');
90
            foreach ($class->getConstants() as $constant) {
91
                if ($this->shouldRenderElement($constant)) {
92
                    $this->addConstant($constant);
93
                }
94
            }
95
        }
96
97
        $this->addProperties($class->getProperties());
98
99
        if (count($class->getMethods()) > 0) {
100
            $this->addH2('Methods');
101
            /* Render methods of a class */
102
            foreach ($class->getMethods() as $method) {
103
                if (!$this->shouldRenderElement($method)) {
104
                    continue;
105
                }
106
                $docBlock = $method->getDocBlock();
107
                $params = [];
108
                if ($docBlock !== null) {
109
                    /** @var Param $param */
110
                    foreach ($docBlock->getTagsByName('param') as $param) {
111
                        $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

111
                        $params[$param->/** @scrutinizer ignore-call */ getVariableName()] = $param;
Loading history...
112
                    }
113
                }
114
                $args = '';
115
                /** @var Argument $argument */
116
                foreach ($method->getArguments() as $argument) {
117
                    // TODO: defaults, types
118
                    $args .= ' $' . $argument->getName() . ', ';
119
                }
120
                $args = substr($args, 0, -2);
121
122
                $modifiers = $method->getVisibility();
123
                $modifiers .= $method->isAbstract() ? ' abstract' : '';
124
                $modifiers .= $method->isFinal() ? ' final' : '';
125
                $modifiers .= $method->isStatic() ? ' static' : '';
126
                $this->addLine('.. rst-class:: ' . $modifiers)->addLine();
127
                $this->indent();
128
                $this->beginPhpDomain('method', $method->getName() . '(' . $args . ')');
129
                $this->addDocBlockDescription($docBlock);
130
                $this->addLine();
131 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...
132
                    foreach ($method->getArguments() as $argument) {
133
                        /** @var Param $param */
134
                        $param = $params[$argument->getName()];
135
                        if ($param !== null) $this->addMultiline(':param ' . self::escape($param->getType()) . ' $' . $argument->getName() . ': ' . $param->getDescription(), true);
136
                    }
137
                }
138
                $this->endPhpDomain('method');
139
                $this->unindent();
140
            }
141
        }
142
        $this->endPhpDomain(); //class
143
    }
144
145
}