|
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> |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
55
|
|
|
$this->namespaceNames = $namespaceNames; |
|
|
|
|
|
|
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
|
|
|
|
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