Passed
Push — master ( b2175e...d070b7 )
by Julius
02:17
created

InterfaceBuilder::callExtensions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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 JuliusHaertl\PHPDocToRst\Extension\Extension;
27
use phpDocumentor\Reflection\DocBlock\Tags\Param;
28
use phpDocumentor\Reflection\Php\Interface_;
29
30
31
class InterfaceBuilder extends Builder {
32
33
    const SECTION_BEFORE_DESCRIPTION = self::class . '::SECTION_BEFORE_DESCRIPTION';
34
    const SECTION_AFTER_DESCRIPTION = self::class . '::SECTION_AFTER_DESCRIPTION';
35
36
    protected function render() {
37
        /** @var Interface_ $interface */
38
        $interface = $this->element;
39
40
        $namespace = str_replace('\\' . $interface->getName(), '', $interface->getFqsen());
41
42
        $docBlock = $interface->getDocBlock();
43
        $this->addH1(self::escape($interface->getFqsen()))->addLine();
44
        if($namespace !== '') {
45
            $this->addLine('.. php:namespace:: ' . substr($namespace, 1));
46
        }
47
        $this->addLine('.. php:interface:: ' . $interface->getName())->addLine();
48
49
        $this->callExtensions(self::SECTION_BEFORE_DESCRIPTION);
50
51
        if ($docBlock) {
52
            $this
53
                ->addIndentMultiline(1, $docBlock->getDescription())
54
                ->addLine();
55
        }
56
57
        // Add class details
58
        $parents = '';
59
        foreach ($interface->getParents() as $parent) {
60
            $parents .= $this->getLink('interface', $parent) . ' ';
61
        }
62
        $this->addFieldList('Parent', $parents);
63
64
        $this->callExtensions(self::SECTION_AFTER_DESCRIPTION);
65
66
        foreach ($interface->getMethods() as $method) {
67
            /* Render method */
68
            $docBlock = $method->getDocBlock();
69
            $params = [];
70 View Code Duplication
            if ($docBlock !== null) {
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...
71
                /** @var Param $param */
72
                foreach ($docBlock->getTagsByName('param') as $param) {
73
                    $params[$param->getVariableName()] = $param;
0 ignored issues
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

73
                    $params[$param->/** @scrutinizer ignore-call */ getVariableName()] = $param;
Loading history...
74
                }
75
            }
76
            $args = '';
77
            foreach ($method->getArguments() as $argument) {
78
                $args .= '$' . $argument->getName() . ', ';
79
            }
80
            $args = substr($args, 0, -2);
81
            $modifiers = $method->getVisibility();
82
            $modifiers .= $method->isAbstract() ? ' abstract' : '';
83
            $modifiers .= $method->isFinal() ? ' final' : '';
84
            $modifiers .= $method->isStatic() ? ' static' : '';
85
            $this->addIndentLine(1, '.. rst-class:: ' . $modifiers)->addLine();
86
87
            $this->addIndentLine(2, '.. php:method:: ' . $method->getName() . '(' . $args . ')');
88
            $this->addLine();
89
            if ($docBlock) {
90
                $this->addIndentMultiline(3, RstBuilder::escape($docBlock->getDescription()));
91
                $this->addLine();
92
            }
93
94
            if (!empty($params)) {
95
                foreach ($method->getArguments() as $argument) {
96
                    /** @var Param $param */
97
                    $param = $params[$argument->getName()];
98
                    if ($param !== null) $this->addIndentMultiline(3, ':param ' . RstBuilder::escape($param->getType()) . ' $' . $argument->getName() . ': ' . RstBuilder::escape($param->getDescription()), true);
99
                }
100
                foreach ($docBlock->getTags() as $tag) {
101
                    $this->addDocblockTag($tag->getName(), $docBlock);
102
                }
103
                $this->addLine();
104
            }
105
        }
106
    }
107
108
    /**
109
     * Render extension custom views before the descriptioj
110
     */
111
    private function callExtensions($type) {
112
113
        foreach ($this->extensions as $extension) {
114
            $addition = $extension->render($type, $this);
115
            $this->addIndentMultiline(1, $addition, true);
116
            $this->addLine();
117
        }
118
    }
119
}