Passed
Pull Request — master (#582)
by Théo
02:12
created

NamespaceRegistry::extractNameNamespace()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Humbug\PhpScoper\Symbol;
6
7
use function array_map;
8
use function array_pop;
9
use function array_unique;
10
use function explode;
11
use function implode;
12
use function ltrim;
13
use function Safe\preg_match;
14
use function Safe\substr;
15
use function strpos;
16
use function strtolower;
17
18
final class NamespaceRegistry
19
{
20
    /**
21
     * @var list<string>
0 ignored issues
show
Bug introduced by
The type Humbug\PhpScoper\Symbol\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
     */
23
    private array $namespaceRegexes;
24
25
    /**
26
     * @var list<string>
27
     */
28
    private array $namespaceNames;
29
30
    /**
31
     * @param string[] $namespaceRegexes
32
     * @param string[] $namespaceNames
33
     */
34
    public static function create(
35
        array $namespaceRegexes = [],
36
        array $namespaceNames = []
37
    ): self {
38
        return new self(
39
            array_unique($namespaceRegexes),
40
            array_unique(
41
                array_map('strtolower', $namespaceNames),
42
            ),
43
        );
44
    }
45
46
    /**
47
     * @param list<string> $namespaceRegexes
48
     * @param list<string> $namespaceNames
49
     */
50
    private function __construct(
51
        array $namespaceRegexes,
52
        array $namespaceNames
53
    ) {
54
        $this->namespaceRegexes = $namespaceRegexes;
0 ignored issues
show
Documentation Bug introduced by
It seems like $namespaceRegexes of type array is incompatible with the declared type Humbug\PhpScoper\Symbol\list of property $namespaceRegexes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
        $this->namespaceNames = $namespaceNames;
0 ignored issues
show
Documentation Bug introduced by
It seems like $namespaceNames of type array is incompatible with the declared type Humbug\PhpScoper\Symbol\list of property $namespaceNames.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
    }
57
58
    public function belongsToRegisteredNamespace(string $symbolName): bool
59
    {
60
        return $this->isRegisteredNamespace(
61
            self::extractNameNamespace($symbolName),
62
        );
63
    }
64
65
    /**
66
     * Checks if the given namespace matches one of the registered namespace
67
     * names, is a sub-namespace of a registered namespace name or matches any
68
     * regex provided.
69
     */
70
    public function isRegisteredNamespace(string $namespaceName): bool
71
    {
72
        $originalNamespaceName = ltrim($namespaceName, '\\');
73
        $normalizedNamespaceName = strtolower($originalNamespaceName);
74
75
        foreach ($this->namespaceNames as $excludedNamespaceName) {
76
            if ('' === $excludedNamespaceName) {
77
                return true;
78
            }
79
80
            if (0 !== strpos($normalizedNamespaceName, $excludedNamespaceName)) {
81
                continue;
82
            }
83
84
            $nameParts = explode('\\', $normalizedNamespaceName);
85
86
            foreach (explode('\\', $excludedNamespaceName) as $index => $excludedNamespacePart) {
87
                if ($nameParts[$index] !== $excludedNamespacePart) {
88
                    return false;
89
                }
90
            }
91
92
            return true;
93
        }
94
95
        foreach ($this->namespaceRegexes as $excludedNamespace) {
96
            if (preg_match($excludedNamespace, $originalNamespaceName)) {
97
                return true;
98
            }
99
        }
100
101
        return false;
102
    }
103
104
    private static function extractNameNamespace(string $name): string
105
    {
106
        if (0 === strpos($name, '\\')) {
107
            $name = substr($name, 1);
108
        }
109
110
        $nameParts = explode('\\', $name);
111
112
        array_pop($nameParts);
113
114
        return [] === $nameParts ? '' : implode('\\', $nameParts);
115
    }
116
}
117