1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Humbug\PhpScoper\Symbol; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use function array_key_exists; |
9
|
|
|
use function array_keys; |
10
|
|
|
use function array_map; |
11
|
|
|
use function array_pop; |
12
|
|
|
use function array_unique; |
13
|
|
|
use function explode; |
14
|
|
|
use function implode; |
15
|
|
|
use function ltrim; |
16
|
|
|
use function Safe\array_flip; |
17
|
|
|
use function Safe\preg_match; |
18
|
|
|
use function strtolower; |
19
|
|
|
use function trim; |
20
|
|
|
use const SORT_STRING; |
21
|
|
|
|
22
|
|
|
final class SymbolRegistry |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array<string, mixed> |
26
|
|
|
*/ |
27
|
|
|
private array $names; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var list<string> |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
private array $regexes; |
33
|
|
|
|
34
|
|
|
private bool $constants; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string[] $names |
38
|
|
|
* @param string[] $regexes |
39
|
|
|
*/ |
40
|
|
|
public static function create( |
41
|
|
|
array $names = [], |
42
|
|
|
array $regexes = [] |
43
|
|
|
): self { |
44
|
|
|
return new self( |
45
|
|
|
array_map( |
46
|
|
|
static fn (string $name) => strtolower(trim($name, '\\')), |
47
|
|
|
$names, |
48
|
|
|
), |
49
|
|
|
array_unique($regexes, SORT_STRING), |
50
|
|
|
false, |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Unlike classes & functions, constants are not case-insensitive (although |
56
|
|
|
* the namespace part _is_). I.e. \Acme\FOO = \ACME\FOO but Acme\FOO ≠ Acme\Foo. |
57
|
|
|
* |
58
|
|
|
* @param string[] $names |
59
|
|
|
* @param string[] $regexes |
60
|
|
|
*/ |
61
|
|
|
public static function createForConstants( |
62
|
|
|
array $names = [], |
63
|
|
|
array $regexes = [] |
64
|
|
|
): self { |
65
|
|
|
return new self( |
66
|
|
|
array_map( |
67
|
|
|
static fn (string $name) => self::lowerCaseConstantName( |
68
|
|
|
trim($name, '\\'), |
69
|
|
|
), |
70
|
|
|
$names, |
71
|
|
|
), |
72
|
|
|
array_unique($regexes, SORT_STRING), |
73
|
|
|
true, |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param list<string> $names |
79
|
|
|
* @param list<string> $regexes |
80
|
|
|
*/ |
81
|
|
|
private function __construct( |
82
|
|
|
array $names, |
83
|
|
|
array $regexes, |
84
|
|
|
bool $constants |
85
|
|
|
) { |
86
|
|
|
$this->names = array_flip($names); |
87
|
|
|
$this->regexes = $regexes; |
|
|
|
|
88
|
|
|
$this->constants = $constants; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function matches(string $symbol): bool |
92
|
|
|
{ |
93
|
|
|
$originalSymbol = ltrim($symbol, '\\'); |
94
|
|
|
$symbol = $this->constants |
95
|
|
|
? self::lowerCaseConstantName($originalSymbol) |
96
|
|
|
: strtolower($originalSymbol); |
97
|
|
|
|
98
|
|
|
if (array_key_exists($symbol, $this->names)) { |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
foreach ($this->regexes as $regex) { |
103
|
|
|
if (preg_match($regex, $originalSymbol)) { |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function merge(SymbolRegistry $registry): self |
112
|
|
|
{ |
113
|
|
|
if ($this->constants !== $registry->constants) { |
114
|
|
|
throw new InvalidArgumentException('Cannot merge registries of different symbol types'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$args = [ |
118
|
|
|
[ |
119
|
|
|
...$this->getNames(), |
120
|
|
|
...$registry->getNames(), |
121
|
|
|
], |
122
|
|
|
[ |
123
|
|
|
...$this->getRegexes(), |
124
|
|
|
...$registry->getRegexes(), |
125
|
|
|
], |
126
|
|
|
]; |
127
|
|
|
|
128
|
|
|
return $this->constants |
129
|
|
|
? self::createForConstants(...$args) |
|
|
|
|
130
|
|
|
: self::create(...$args); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @internal |
135
|
|
|
* |
136
|
|
|
* @return list<string> |
137
|
|
|
*/ |
138
|
|
|
public function getNames(): array |
139
|
|
|
{ |
140
|
|
|
return array_keys($this->names); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @internal |
145
|
|
|
* |
146
|
|
|
* @erturn list<string> |
147
|
|
|
*/ |
148
|
|
|
public function getRegexes(): array |
149
|
|
|
{ |
150
|
|
|
return $this->regexes; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Transforms the constant FQ name "Acme\Foo\X" to "acme\foo\X" since the |
155
|
|
|
* namespace remains case-insensitive for constants regardless of whether |
156
|
|
|
* constants actually are case-insensitive. |
157
|
|
|
*/ |
158
|
|
|
private static function lowerCaseConstantName(string $name): string |
159
|
|
|
{ |
160
|
|
|
$parts = explode('\\', $name); |
161
|
|
|
|
162
|
|
|
$lastPart = array_pop($parts); |
163
|
|
|
|
164
|
|
|
$parts = array_map('strtolower', $parts); |
165
|
|
|
|
166
|
|
|
$parts[] = $lastPart; |
167
|
|
|
|
168
|
|
|
return implode('\\', $parts); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
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