|
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> |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
57
|
|
|
$this->regexes = $namespaceRegexes; |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @internal |
|
118
|
|
|
* |
|
119
|
|
|
* @return list<string> |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getRegexes(): array |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->regexes; |
|
|
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths