Issues (17)

src/Extension/InterfaceImplementors.php (1 issue)

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