1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the humbug/php-scoper package. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2017 Théo FIDRY <[email protected]>, |
9
|
|
|
* Pádraic Brady <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Humbug\PhpScoper\Symbol; |
16
|
|
|
|
17
|
|
|
use function array_filter; |
18
|
|
|
use function array_map; |
19
|
|
|
use function array_pop; |
20
|
|
|
use function array_unique; |
21
|
|
|
use function count; |
22
|
|
|
use function explode; |
23
|
|
|
use function implode; |
24
|
|
|
use function ltrim; |
25
|
|
|
use function Safe\preg_match; |
26
|
|
|
use function str_contains; |
27
|
|
|
use function strtolower; |
28
|
|
|
use function trim; |
29
|
|
|
use const SORT_STRING; |
30
|
|
|
|
31
|
|
|
final class NamespaceRegistry |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var list<string> |
|
|
|
|
35
|
|
|
*/ |
36
|
|
|
private array $names; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var list<string> |
40
|
|
|
*/ |
41
|
|
|
private array $regexes; |
42
|
|
|
|
43
|
|
|
private bool $containsGlobalNamespace; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string[] $namespaceNames |
47
|
|
|
* @param string[] $namespaceRegexes |
48
|
|
|
*/ |
49
|
|
|
public static function create( |
50
|
|
|
array $namespaceNames = [], |
51
|
|
|
array $namespaceRegexes = [] |
52
|
|
|
): self { |
53
|
|
|
return new self( |
54
|
|
|
array_unique( |
55
|
|
|
array_map( |
56
|
|
|
static fn (string $namespaceName) => strtolower(trim($namespaceName, '\\')), |
57
|
|
|
$namespaceNames, |
58
|
|
|
), |
59
|
|
|
SORT_STRING, |
60
|
|
|
), |
61
|
|
|
array_unique($namespaceRegexes, SORT_STRING), |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param list<string> $namespaceNames |
67
|
|
|
* @param list<string> $namespaceRegexes |
68
|
|
|
*/ |
69
|
|
|
private function __construct( |
70
|
|
|
array $namespaceNames, |
71
|
|
|
array $namespaceRegexes |
72
|
|
|
) { |
73
|
|
|
$this->names = $namespaceNames; |
|
|
|
|
74
|
|
|
$this->regexes = $namespaceRegexes; |
|
|
|
|
75
|
|
|
|
76
|
|
|
$this->containsGlobalNamespace = count( |
77
|
|
|
array_filter( |
78
|
|
|
$namespaceNames, |
79
|
|
|
static fn (string $name) => '' === $name, |
80
|
|
|
), |
81
|
|
|
) !== 0; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function belongsToRegisteredNamespace(string $symbolName): bool |
85
|
|
|
{ |
86
|
|
|
return $this->isRegisteredNamespace( |
87
|
|
|
self::extractNameNamespace($symbolName), |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Checks if the given namespace matches one of the registered namespace |
93
|
|
|
* names, is a sub-namespace of a registered namespace name or matches any |
94
|
|
|
* regex provided. |
95
|
|
|
*/ |
96
|
|
|
public function isRegisteredNamespace(string $namespaceName): bool |
97
|
|
|
{ |
98
|
|
|
if ($this->containsGlobalNamespace) { |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$originalNamespaceName = ltrim($namespaceName, '\\'); |
103
|
|
|
$normalizedNamespaceName = strtolower($originalNamespaceName); |
104
|
|
|
|
105
|
|
|
foreach ($this->names as $excludedNamespaceName) { |
106
|
|
|
if ('' === $excludedNamespaceName |
107
|
|
|
|| str_contains($normalizedNamespaceName, $excludedNamespaceName) |
108
|
|
|
) { |
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
foreach ($this->regexes as $excludedNamespace) { |
114
|
|
|
if (preg_match($excludedNamespace, $originalNamespaceName)) { |
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @internal |
124
|
|
|
* |
125
|
|
|
* @return list<string> |
126
|
|
|
*/ |
127
|
|
|
public function getNames(): array |
128
|
|
|
{ |
129
|
|
|
return $this->names; |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @internal |
134
|
|
|
* |
135
|
|
|
* @return list<string> |
136
|
|
|
*/ |
137
|
|
|
public function getRegexes(): array |
138
|
|
|
{ |
139
|
|
|
return $this->regexes; |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private static function extractNameNamespace(string $name): string |
143
|
|
|
{ |
144
|
|
|
$nameParts = explode('\\', $name); |
145
|
|
|
|
146
|
|
|
array_pop($nameParts); |
147
|
|
|
|
148
|
|
|
return [] === $nameParts ? '' : implode('\\', $nameParts); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
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