Passed
Push — master ( 4b6191...118b18 )
by Julius
01:48
created

InterfaceImplementors::render()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 4
nop 3
dl 0
loc 13
rs 8.8571
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\Extension;
25
26
use JuliusHaertl\PHPDocToRst\Builder\ExtensionBuilder;
27
use JuliusHaertl\PHPDocToRst\Builder\FileBuilder;
28
use JuliusHaertl\PHPDocToRst\Builder\InterfaceFileBuilder;
29
use JuliusHaertl\PHPDocToRst\Builder\PhpDomainBuilder;
30
use JuliusHaertl\PHPDocToRst\Builder\RstBuilder;
31
use phpDocumentor\Reflection\Php\Interface_;
32
use PhpParser\Builder\Class_;
33
34
/**
35
 * Class InterfaceImplementors
36
 * @package JuliusHaertl\PHPDocToRst\Extension
37
 *
38
 * This extension parses all classes and interface relations.
39
 * A link to all classes implementing a specific interface
40
 * is added to the interface documentation.
41
 */
42
43
class InterfaceImplementors extends Extension {
44
45
    private $implementors = [];
46
47
    public function prepare() {
48
        foreach ($this->project->getFiles() as $file) {
49
            foreach ($file->getClasses() as $class) {
50
                foreach ($class->getInterfaces() as $interface) {
51
                    if (!array_key_exists((string)$interface, $this->implementors)) {
52
                        $this->implementors[(string)$interface] = [];
53
                    }
54
                    $this->implementors[(string)$interface][] = $class->getFqsen();
55
                }
56
            }
57
        }
58
    }
59
60
    /**
61
     * @param string $type
62
     * @param FileBuilder $builder
63
     * @param Element $element
0 ignored issues
show
Bug introduced by
The type JuliusHaertl\PHPDocToRst\Extension\Element was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
64
     */
65
    public function render($type, &$builder, $element) {
66
        if (!$builder instanceof FileBuilder && !$element instanceof Interface_) {
67
            return;
68
        }
69
        if ($type === PhpDomainBuilder::SECTION_AFTER_DESCRIPTION && $builder->getElement() instanceof Interface_) {
70
            /** @var Interface_ $interface */
71
            $interface = $builder->getElement();
72
            $content = '';
73
            foreach ($this->implementors[(string)$interface->getFqsen()] as $implementor) {
74
                $content .= ':php:class:`' . RstBuilder::escape(substr($implementor, 1)) . '` ';
75
            }
76
            $builder->addFieldList('Implemented by', $content);
77
            $builder->addLine();
78
        }
79
    }
80
81
}