TocExtension::render()   C
last analyzed

Complexity

Conditions 12
Paths 21

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 2 Features 1
Metric Value
cc 12
eloc 21
c 6
b 2
f 1
nc 21
nop 3
dl 0
loc 34
rs 6.9666

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
 * @copyright Copyright (c) 2017 Julius Härtl <[email protected]>
4
 * @author    Julius Härtl <[email protected]>
5
 * @license   GNU AGPL version 3 or any later version
6
 *
7
 *  This program is free software: you can redistribute it and/or modify
8
 *  it under the terms of the GNU Affero General Public License as
9
 *  published by the Free Software Foundation, either version 3 of the
10
 *  License, or (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU Affero General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU Affero General Public License
18
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
namespace JuliusHaertl\PHPDocToRst\Extension;
22
23
use JuliusHaertl\PHPDocToRst\Builder\FileBuilder;
24
use JuliusHaertl\PHPDocToRst\Builder\PhpDomainBuilder;
25
use phpDocumentor\Reflection\Php\Argument;
26
use phpDocumentor\Reflection\Php\Class_;
27
use phpDocumentor\Reflection\Php\Interface_;
28
use PhpParser\Builder\Trait_;
29
30
/**
31
 * This extension will render a list of methods  for easy access
32
 * at the beginning of classes, interfaces and traits.
33
 */
34
class TocExtension extends Extension
35
{
36
    /**
37
     * @param string      $type
38
     * @param FileBuilder $builder
39
     */
40
    public function render($type, &$builder, $element)
41
    {
42
        if ($type === PhpDomainBuilder::SECTION_AFTER_INTRODUCTION) {
43
            if ($element instanceof Class_ || $element instanceof Interface_ || $element instanceof Trait_) {
44
                $builder->addLine();
45
                $builder->addH2('Summary');
46
47
                /** @var Interface_ $interface */
48
                $interface = $builder->getElement();
49
50
                if (count($interface->getMethods()) > 0) {
51
                    $builder->addH3('Methods');
52
                    foreach ($interface->getMethods() as $method) {
53
                        //We don't want add an no displayed method to Summary
54
                        if (!$builder->shouldRenderElement($method)) {
55
                            continue;
56
                        }
57
58
                        $args = '';
59
                        /** @var Argument $argument */
60
                        foreach ($method->getArguments() as $argument) {
61
                            // TODO: defaults, types
62
                            $args .= '$'.$argument->getName().', ';
63
                        }
64
                        $args = substr($args, 0, -2);
65
                        $modifiers = $method->getVisibility();
66
                        $modifiers .= $method->isAbstract() ? ' abstract' : '';
67
                        $modifiers .= $method->isFinal() ? ' final' : '';
68
                        $modifiers .= $method->isStatic() ? ' static' : '';
69
                        $signature = $modifiers.' '.$method->getName().'('.$args.')';
70
71
                        $builder->addLine('* '.PhpDomainBuilder::getLink('meth', $method->getFqsen(), $signature));
72
                    }
73
                    $builder->addLine()->addLine();
74
                }
75
            }
76
        }
77
    }
78
}
79