|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yokai\EnumBundle; |
|
6
|
|
|
|
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
use ReflectionException; |
|
9
|
|
|
use Yokai\EnumBundle\Exception\CannotExtractConstantsException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @internal |
|
13
|
|
|
* |
|
14
|
|
|
* @author Yann Eugoné <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class ConstantExtractor |
|
17
|
|
|
{ |
|
18
|
|
|
public function extract(string $pattern): array |
|
19
|
|
|
{ |
|
20
|
|
|
[$class, $patternRegex] = $this->explode($pattern); |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
return $this->filter( |
|
23
|
|
|
$this->publicConstants($class), |
|
24
|
|
|
$patternRegex, |
|
|
|
|
|
|
25
|
|
|
$pattern |
|
26
|
|
|
); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
private function filter(array $constants, string $regexp, string $pattern): array |
|
30
|
|
|
{ |
|
31
|
|
|
$matchingNames = preg_grep($regexp, array_keys($constants)); |
|
32
|
|
|
|
|
33
|
|
|
if (count($matchingNames) === 0) { |
|
34
|
|
|
throw CannotExtractConstantsException::noConstantMatchingPattern($pattern); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return array_values(array_intersect_key($constants, array_flip($matchingNames))); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private function publicConstants(string $class): array |
|
41
|
|
|
{ |
|
42
|
|
|
try { |
|
43
|
|
|
$constants = (new ReflectionClass($class))->getReflectionConstants(); |
|
44
|
|
|
} catch (ReflectionException $exception) { |
|
45
|
|
|
throw CannotExtractConstantsException::classDoNotExists($class); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$list = []; |
|
49
|
|
|
foreach ($constants as $constant) { |
|
50
|
|
|
if (!$constant->isPublic()) { |
|
51
|
|
|
continue; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$list[$constant->getName()] = $constant->getValue(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (count($list) === 0) { |
|
58
|
|
|
throw CannotExtractConstantsException::classHasNoPublicConstant($class); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $list; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function explode(string $pattern): array |
|
65
|
|
|
{ |
|
66
|
|
|
if (substr_count($pattern, '::') !== 1) { |
|
67
|
|
|
throw CannotExtractConstantsException::invalidPattern($pattern); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
[$class, $constantsNamePattern] = explode('::', $pattern); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
if (substr_count($constantsNamePattern, '*') === 0) { |
|
|
|
|
|
|
73
|
|
|
throw CannotExtractConstantsException::invalidPattern($pattern); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$constantsNameRegexp = sprintf( |
|
77
|
|
|
'#^%s$#', |
|
78
|
|
|
str_replace('*', '[0-9a-zA-Z_]+', $constantsNamePattern) |
|
|
|
|
|
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
return [$class, $constantsNameRegexp]; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.