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

NamespaceIndexBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jus
5
 * Date: 06.10.17
6
 * Time: 03:05
7
 */
8
9
namespace JuliusHaertl\PHPDocToRst\Builder;
10
11
12
use phpDocumentor\Reflection\Fqsen;
13
use phpDocumentor\Reflection\Php\Namespace_;
14
15
class NamespaceIndexBuilder extends RstBuilder {
16
17
    /** @var Namespace_ */
18
    private $currentNamespace;
19
    /** @var Namespace_[] */
20
    private $namespaces;
21
    public function __construct($namespaces, Namespace_ $current) {
22
        $this->namespaces = $namespaces;
23
        $this->currentNamespace = $current;
24
    }
25
26
    public function render() {
27
        $currentNamespaceFqsen = (string)$this->currentNamespace->getFqsen();
28
29
        /**
30
         * Find child namespaces for current namespace
31
         */
32
        $childNamespaces = [];
33
        /** @var Namespace_ $namespace */
34
        foreach ($this->namespaces as $namespace) {
35
            // check if not root and doesn't start with current namespace
36
            if ($currentNamespaceFqsen !== '\\' && strpos((string)$namespace->getFqsen(), $currentNamespaceFqsen . '\\') !== 0) {
37
                continue;
38
            }
39
            if (
40
                strpos((string)$namespace->getFqsen(), $currentNamespaceFqsen) === 0
41
                && (string)$namespace->getFqsen() !== (string)$currentNamespaceFqsen
42
            ) {
43
                // only keep first level children
44
                $childrenPath = substr((string)$namespace->getFqsen(), strlen((string)$this->currentNamespace->getFqsen())+1);
45
                if (strpos($childrenPath, '\\') === false) {
46
                    $childNamespaces[] = $namespace;
47
                }
48
            }
49
        }
50
51
        if ($currentNamespaceFqsen !== '\\') {
52
            $label = str_replace('\\', '-', $currentNamespaceFqsen);
53
            $this->addLine('.. _namespace' . $label . '');
54
            $this->addH1(RstBuilder::escape($currentNamespaceFqsen));
55
        } else {
56
            $label = 'root-namespace';
57
            $this->addLine('.. _namespace-' . $label . '');
58
            $this->addH1(RstBuilder::escape('\\'));
59
        }
60
        $this->addLine();
61
62
        $this->addH2('Namespaces');
63
        $this->addLine('.. toctree::');
64
        $this->addIndentLine(1, ':maxdepth: 1')->addLine();
65
        /** @var Namespace_ $namespace */
66
        foreach ($childNamespaces as $namespace) {
67
            $this->addIndentLine(1, $namespace->getName() . ' <' . $namespace->getName() . '/index>');
68
        }
69
        $this->addLine()->addLine();
70
71
        $this->addH2('Interfaces');
72
        $this->addLine('.. toctree::');
73
        $this->addIndentLine(1, ':maxdepth: 1')->addLine();
74
        /** @var Fqsen $entry */
75 View Code Duplication
        foreach ($this->currentNamespace->getInterfaces() as $entry) {
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...
76
            $subPath = $entry;
77
            if ($currentNamespaceFqsen !== '\\' && substr($entry, 0, strlen($currentNamespaceFqsen)) === $currentNamespaceFqsen) {
78
                $subPath = substr($entry, strlen($currentNamespaceFqsen));
79
            }
80
            $path = substr(str_replace("\\", "/", $subPath), 1);
81
            $this->addIndentLine(1, $entry->getName() . ' <' . $path . '>');
82
        }
83
        $this->addLine()->addLine();
84
85
        $this->addH2('Classes');
86
        $this->addLine('.. toctree::');
87
        $this->addIndentLine(1, ':maxdepth: 1')->addLine();
88
        /** @var Fqsen $entry */
89 View Code Duplication
        foreach ($this->currentNamespace->getClasses() as $entry) {
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...
90
            $subPath = $entry;
91
            if ($currentNamespaceFqsen !== '\\' && substr($entry, 0, strlen($currentNamespaceFqsen)) === $currentNamespaceFqsen) {
92
                $subPath = substr($entry, strlen($currentNamespaceFqsen));
93
            }
94
            $path = substr(str_replace("\\", "/", $subPath), 1);
95
            $this->addIndentLine(1,$entry->getName() . ' <' . $path . '>');
96
        }
97
        $this->addLine()->addLine();
98
99
        // FIXME: add functions / constants
100
101
102
    }
103
104
}