|
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; |
|
16
|
|
|
|
|
17
|
|
|
use Countable; |
|
18
|
|
|
use InvalidArgumentException; |
|
19
|
|
|
use function array_map; |
|
20
|
|
|
use function array_merge; |
|
21
|
|
|
use function array_unique; |
|
22
|
|
|
use function count; |
|
23
|
|
|
use function in_array; |
|
24
|
|
|
use function sprintf; |
|
25
|
|
|
use function substr; |
|
26
|
|
|
use function trim; |
|
27
|
|
|
|
|
28
|
|
|
final class Whitelist implements Countable |
|
29
|
|
|
{ |
|
30
|
|
|
private $classes; |
|
31
|
|
|
private $namespaces; |
|
32
|
|
|
private $whitelistGlobalConstants; |
|
33
|
|
|
|
|
34
|
|
|
public static function create(bool $whitelistGlobalConstants, string ...$elements): self |
|
35
|
|
|
{ |
|
36
|
|
|
$classes = []; |
|
37
|
|
|
$namespaces = []; |
|
38
|
|
|
|
|
39
|
|
|
foreach ($elements as $element) { |
|
40
|
|
|
if (isset($element[0]) && '\\' === $element[0]) { |
|
41
|
|
|
$element = substr($element, 1); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ('' === trim($element)) { |
|
45
|
|
|
throw new InvalidArgumentException( |
|
46
|
|
|
sprintf( |
|
47
|
|
|
'Invalid whitelist element "%s": cannot accept an empty string', |
|
48
|
|
|
$element |
|
49
|
|
|
) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ('\*' === substr($element, -2)) { |
|
54
|
|
|
$namespaces[] = substr($element, 0, -2); |
|
55
|
|
|
} elseif ('*' === $element) { |
|
56
|
|
|
$namespaces[] = ''; |
|
57
|
|
|
} else { |
|
58
|
|
|
$classes[] = $element; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return new self( |
|
63
|
|
|
$whitelistGlobalConstants, |
|
64
|
|
|
array_unique($classes), |
|
65
|
|
|
array_unique($namespaces) |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string[] $classes |
|
71
|
|
|
* @param string[] $namespaces |
|
72
|
|
|
*/ |
|
73
|
|
|
private function __construct(bool $whitelistGlobalConstants, array $classes, array $namespaces) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->whitelistGlobalConstants = $whitelistGlobalConstants; |
|
76
|
|
|
$this->classes = $classes; |
|
77
|
|
|
$this->namespaces = $namespaces; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function whitelistGlobalConstants(): bool |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->whitelistGlobalConstants; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function isClassWhitelisted(string $name): bool |
|
86
|
|
|
{ |
|
87
|
|
|
return in_array($name, $this->classes, true); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return string[] |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getClassWhitelistArray(): array |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->classes; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function isNamespaceWhitelisted(string $name): bool |
|
99
|
|
|
{ |
|
100
|
|
|
foreach ($this->namespaces as $namespace) { |
|
101
|
|
|
if ('' === $namespace || 0 === strpos($name, $namespace)) { |
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
|
|
public function count(): int |
|
113
|
|
|
{ |
|
114
|
|
|
return count($this->classes) + count($this->namespaces); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function toArray(): array |
|
118
|
|
|
{ |
|
119
|
|
|
$namespaces = array_map( |
|
120
|
|
|
function (string $namespace): string { |
|
121
|
|
|
return '' === $namespace ? '*' : $namespace.'\*'; |
|
122
|
|
|
}, |
|
123
|
|
|
$this->namespaces |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
return array_merge($this->classes, $namespaces); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|