InterfaceImplementors   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
eloc 22
c 5
b 2
f 0
dl 0
loc 44
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A prepare() 0 9 5
B render() 0 21 8
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 JuliusHaertl\PHPDocToRst\Builder\RstBuilder;
26
use phpDocumentor\Reflection\Element;
27
use phpDocumentor\Reflection\Php\Interface_;
28
29
/**
30
 * Class InterfaceImplementors.
31
 */
32
class InterfaceImplementors extends Extension
33
{
34
    private $implementors = [];
35
36
    public function prepare()
37
    {
38
        foreach ($this->project->getFiles() as $file) {
39
            foreach ($file->getClasses() as $class) {
40
                foreach ($class->getInterfaces() as $interface) {
41
                    if (!array_key_exists((string) $interface, $this->implementors)) {
42
                        $this->implementors[(string) $interface] = [];
43
                    }
44
                    $this->implementors[(string) $interface][] = $class->getFqsen();
45
                }
46
            }
47
        }
48
    }
49
50
    /**
51
     * @param string      $type
52
     * @param FileBuilder $builder
53
     * @param Element     $element
54
     */
55
    public function render($type, &$builder, $element)
56
    {
57
        if (!$builder instanceof FileBuilder || !$element instanceof Interface_) {
0 ignored issues
show
introduced by
$builder is always a sub-type of JuliusHaertl\PHPDocToRst\Builder\FileBuilder.
Loading history...
58
            return;
59
        }
60
        if ($type === PhpDomainBuilder::SECTION_AFTER_DESCRIPTION && $builder->getElement() instanceof Interface_) {
61
            /** @var Interface_ $interface */
62
            $interface = $builder->getElement();
63
            $content = '';
64
            if (!array_key_exists((string) $interface->getFqsen(), $this->implementors)) {
65
                return;
66
            }
67
            $implementors = $this->implementors[(string) $interface->getFqsen()];
68
            if (count($implementors) === 0) {
69
                return;
70
            }
71
            foreach ($implementors as $implementor) {
72
                $content .= ':php:class:`'.RstBuilder::escape(substr($implementor, 1)).'` ';
73
            }
74
            $builder->addFieldList('Implemented by', $content);
75
            $builder->addLine();
76
        }
77
    }
78
}
79