Namespace_::include()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 12
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 19
rs 8.8333
1
<?php
2
declare(strict_types=1);
3
4
namespace DependencyAnalyzer\DependencyGraph\FullyQualifiedStructuralElementName;
5
6
use DependencyAnalyzer\DependencyGraph\FullyQualifiedStructuralElementName;
7
8
class Namespace_ extends Base
9
{
10
    /**
11
     * @param string $elementName
12
     * @inheritDoc
13
     */
14
    public function __construct(string $elementName)
15
    {
16
        if (substr($elementName, -1) !== '\\') {
17
            $elementName .= '\\';
18
        }
19
        parent::__construct($elementName);
20
    }
21
22
    public function getType(): string
23
    {
24
        return FullyQualifiedStructuralElementName::TYPE_NAMESPACE;
25
    }
26
27
    public function include(Base $that): bool
28
    {
29
        if ($this->toString() === '\\') {
30
            // Pattern likely '\\' will match with all className.
31
            return true;
32
        } elseif ($this->isSame($that)) {
33
            return true;
34
        } elseif (count($this->getFullyQualifiedNamespaceNameAsArray()) > count($this->getFullyQualifiedNamespaceNameAsArray())) {
35
            return false;
36
        }
37
38
        $namesOfThat = $that->getFullyQualifiedNamespaceNameAsArray();
39
        foreach ($this->getFullyQualifiedNamespaceNameAsArray() as $index => $name) {
40
            if (!isset($namesOfThat[$index]) || $namesOfThat[$index] !== $name) {
41
                return false;
42
            }
43
        }
44
45
        return true;
46
    }
47
48
    public function isNamespace(): bool
49
    {
50
        return true;
51
    }
52
53
    public function getFullyQualifiedNamespaceNameAsArray(): array
54
    {
55
        if ($this->toString() === '\\') {
56
            return [];
57
        }
58
59
        return explode('\\', trim($this->toString(), '\\'));
60
    }
61
62
    public function getFullyQualifiedClassNameAsArray(): ?array
63
    {
64
        return null;
65
    }
66
}
67