|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Eclipxe\Enum\Internal; |
|
6
|
|
|
|
|
7
|
|
|
use Eclipxe\Enum\Exceptions\IndexOverrideException; |
|
8
|
|
|
use Eclipxe\Enum\Exceptions\ValueOverrideException; |
|
9
|
|
|
use ReflectionClass; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This is a helper that perform discovery |
|
13
|
|
|
* |
|
14
|
|
|
* This is an internal class, do not use it by your own. Changes on this class are not library breaking changes. |
|
15
|
|
|
* @internal |
|
16
|
|
|
*/ |
|
17
|
|
|
class EntriesPopulator |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
private $className; |
|
21
|
|
|
|
|
22
|
|
|
/** @var array */ |
|
23
|
|
|
private $overrideValues; |
|
24
|
|
|
|
|
25
|
|
|
/** @var array */ |
|
26
|
|
|
private $overrideIndices; |
|
27
|
|
|
|
|
28
|
|
|
/** @var Entries */ |
|
29
|
|
|
private $parentEntries; |
|
30
|
|
|
|
|
31
|
5 |
|
public function __construct( |
|
32
|
|
|
string $className, |
|
33
|
|
|
array $overrideValues, |
|
34
|
|
|
array $overrideIndices, |
|
35
|
|
|
Entries $parentEntries |
|
36
|
|
|
) { |
|
37
|
5 |
|
$this->className = $className; |
|
38
|
5 |
|
$this->overrideValues = $overrideValues; |
|
39
|
5 |
|
$this->overrideIndices = $overrideIndices; |
|
40
|
5 |
|
$this->parentEntries = $parentEntries; |
|
41
|
5 |
|
} |
|
42
|
|
|
|
|
43
|
5 |
|
public function getClassName(): string |
|
44
|
|
|
{ |
|
45
|
5 |
|
return $this->className; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
5 |
|
public function populate(Entries $entries): void |
|
49
|
|
|
{ |
|
50
|
|
|
// populate with parents first |
|
51
|
5 |
|
$entries->append($this->parentEntries); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
// populate with discovered |
|
54
|
5 |
|
$names = array_filter( |
|
55
|
5 |
|
static::resolveNamesFromDocBlocks(), |
|
56
|
|
|
function (string $name) use ($entries) { |
|
57
|
5 |
|
return ! $entries->hasName($name); |
|
58
|
5 |
|
} |
|
59
|
|
|
); |
|
60
|
5 |
|
foreach ($names as $name) { |
|
61
|
5 |
|
$newValue = $this->overrideValue($name) ?? $name; |
|
62
|
5 |
|
if (null !== $entries->findEntryByValue($newValue)) { |
|
63
|
1 |
|
throw ValueOverrideException::create($this->getClassName(), $newValue); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
5 |
|
$newIndex = $this->overrideIndex($name) ?? $entries->nextIndex(); |
|
67
|
5 |
|
if (null !== $entries->findEntryByIndex($newIndex)) { |
|
68
|
1 |
|
throw IndexOverrideException::create($this->getClassName(), strval($newIndex)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
5 |
|
$entries->put($name, new Entry($newValue, $newIndex)); |
|
72
|
|
|
} |
|
73
|
3 |
|
} |
|
74
|
|
|
|
|
75
|
5 |
|
public function overrideValue(string $name): ?string |
|
76
|
|
|
{ |
|
77
|
5 |
|
$value = $this->overrideValues[$name] ?? null; |
|
78
|
5 |
|
if (null === $value || ! is_string($value)) { |
|
79
|
4 |
|
return null; |
|
80
|
|
|
} |
|
81
|
2 |
|
return $value; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
5 |
|
public function overrideIndex(string $name): ?int |
|
85
|
|
|
{ |
|
86
|
5 |
|
$index = $this->overrideIndices[$name] ?? null; |
|
87
|
5 |
|
if (null === $index || ! is_int($index)) { |
|
88
|
5 |
|
return null; |
|
89
|
|
|
} |
|
90
|
2 |
|
return $index; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
5 |
|
public function resolveNamesFromDocBlocks(): array |
|
94
|
|
|
{ |
|
95
|
|
|
// get comments |
|
96
|
5 |
|
$reflectionClass = new ReflectionClass($this->getClassName()); |
|
97
|
5 |
|
$docComment = strval($reflectionClass->getDocComment()); |
|
98
|
|
|
|
|
99
|
|
|
// read declarations @method static self WORD() |
|
100
|
|
|
// [\w]+: any word letters, numbers and underscore |
|
101
|
5 |
|
preg_match_all('/\@method static self ([\w]+)\(\)/', $docComment, $matches); |
|
102
|
5 |
|
$values = $matches[1] ?? []; |
|
103
|
|
|
|
|
104
|
5 |
|
return $values; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: