Passed
Push — master ( 1fb040...5fddea )
by Satoshi
02:25
created

Namespace_   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
B include() 0 19 7
A getType() 0 3 1
A getFullyQualifiedClassNameAsArray() 0 3 1
A getFullyQualifiedNamespaceNameAsArray() 0 7 2
A isNamespace() 0 3 1
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