Passed
Push — master ( 8b4604...b089da )
by Théo
05:38 queued 03:31
created

NamespaceRegistry::getNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
use const SORT_STRING;
18
19
final class NamespaceRegistry
20
{
21
    /**
22
     * @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...
23
     */
24
    private array $names;
25
26
    /**
27
     * @var list<string>
28
     */
29
    private array $regexes;
30
31
    /**
32
     * @param string[] $namespaceNames
33
     * @param string[] $namespaceRegexes
34
     */
35
    public static function create(
36
        array $namespaceNames = [],
37
        array $namespaceRegexes = []
38
    ): self {
39
        return new self(
40
            array_unique(
41
                array_map('strtolower', $namespaceNames),
42
                SORT_STRING,
43
            ),
44
            array_unique($namespaceRegexes, SORT_STRING),
45
        );
46
    }
47
48
    /**
49
     * @param list<string> $namespaceNames
50
     * @param list<string> $namespaceRegexes
51
     */
52
    private function __construct(
53
        array $namespaceNames,
54
        array $namespaceRegexes
55
    ) {
56
        $this->names = $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 $names.

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...
57
        $this->regexes = $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 $regexes.

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...
58
    }
59
60
    public function belongsToRegisteredNamespace(string $symbolName): bool
61
    {
62
        return $this->isRegisteredNamespace(
63
            self::extractNameNamespace($symbolName),
64
        );
65
    }
66
67
    /**
68
     * Checks if the given namespace matches one of the registered namespace
69
     * names, is a sub-namespace of a registered namespace name or matches any
70
     * regex provided.
71
     */
72
    public function isRegisteredNamespace(string $namespaceName): bool
73
    {
74
        $originalNamespaceName = ltrim($namespaceName, '\\');
75
        $normalizedNamespaceName = strtolower($originalNamespaceName);
76
77
        foreach ($this->names as $excludedNamespaceName) {
78
            if ('' === $excludedNamespaceName) {
79
                return true;
80
            }
81
82
            if (0 !== strpos($normalizedNamespaceName, $excludedNamespaceName)) {
83
                continue;
84
            }
85
86
            $nameParts = explode('\\', $normalizedNamespaceName);
87
88
            foreach (explode('\\', $excludedNamespaceName) as $index => $excludedNamespacePart) {
89
                if ($nameParts[$index] !== $excludedNamespacePart) {
90
                    return false;
91
                }
92
            }
93
94
            return true;
95
        }
96
97
        foreach ($this->regexes as $excludedNamespace) {
98
            if (preg_match($excludedNamespace, $originalNamespaceName)) {
99
                return true;
100
            }
101
        }
102
103
        return false;
104
    }
105
106
    /**
107
     * @internal
108
     *
109
     * @return list<string>
110
     */
111
    public function getNames(): array
112
    {
113
        return $this->names;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->names returns the type array which is incompatible with the documented return type Humbug\PhpScoper\Symbol\list.
Loading history...
114
    }
115
116
    /**
117
     * @internal
118
     *
119
     * @return list<string>
120
     */
121
    public function getRegexes(): array
122
    {
123
        return $this->regexes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->regexes returns the type array which is incompatible with the documented return type Humbug\PhpScoper\Symbol\list.
Loading history...
124
    }
125
126
    private static function extractNameNamespace(string $name): string
127
    {
128
        if (0 === strpos($name, '\\')) {
129
            $name = substr($name, 1);
130
        }
131
132
        $nameParts = explode('\\', $name);
133
134
        array_pop($nameParts);
135
136
        return [] === $nameParts ? '' : implode('\\', $nameParts);
137
    }
138
}
139