|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cerbero\Enum\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use BackedEnum; |
|
|
|
|
|
|
6
|
|
|
use Cerbero\Enum\Attributes\Meta; |
|
7
|
|
|
use ReflectionAttribute; |
|
8
|
|
|
use ReflectionEnum; |
|
|
|
|
|
|
9
|
|
|
use ReflectionEnumUnitCase; |
|
|
|
|
|
|
10
|
|
|
use ReflectionMethod; |
|
11
|
|
|
use ValueError; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The trait to make an enum self-aware. |
|
15
|
|
|
*/ |
|
16
|
|
|
trait SelfAware |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Determine whether the enum is pure. |
|
20
|
|
|
*/ |
|
21
|
96 |
|
public static function isPure(): bool |
|
22
|
|
|
{ |
|
23
|
96 |
|
return !self::isBacked(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Determine whether the enum is backed. |
|
28
|
|
|
*/ |
|
29
|
96 |
|
public static function isBacked(): bool |
|
30
|
|
|
{ |
|
31
|
96 |
|
return is_subclass_of(self::class, BackedEnum::class); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Retrieve all the meta names of the enum. |
|
36
|
|
|
* |
|
37
|
|
|
* @return string[] |
|
38
|
|
|
*/ |
|
39
|
3 |
|
public static function metaNames(): array |
|
40
|
|
|
{ |
|
41
|
3 |
|
$meta = []; |
|
42
|
3 |
|
$enum = new ReflectionEnum(self::class); |
|
43
|
|
|
|
|
44
|
3 |
|
foreach ($enum->getAttributes(Meta::class) as $attribute) { |
|
45
|
3 |
|
array_push($meta, ...$attribute->newInstance()->names()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
foreach ($enum->getCases() as $case) { |
|
49
|
2 |
|
foreach ($case->getAttributes(Meta::class) as $attribute) { |
|
50
|
2 |
|
array_push($meta, ...$attribute->newInstance()->names()); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
foreach ($enum->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
|
55
|
2 |
|
if (! $method->isStatic() && $method->getFileName() == $enum->getFileName()) { |
|
56
|
2 |
|
$meta[] = $method->getShortName(); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
return array_values(array_unique($meta)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Retrieve the given item of this case. |
|
65
|
|
|
* |
|
66
|
|
|
* @template TItemValue |
|
67
|
|
|
* |
|
68
|
|
|
* @param (callable(self): TItemValue)|string $item |
|
|
|
|
|
|
69
|
|
|
* @return TItemValue |
|
70
|
|
|
* @throws ValueError |
|
71
|
|
|
*/ |
|
72
|
44 |
|
public function resolveItem(callable|string $item): mixed |
|
73
|
|
|
{ |
|
74
|
|
|
return match (true) { |
|
75
|
44 |
|
is_callable($item) => $item($this), |
|
76
|
34 |
|
property_exists($this, $item) => $this->$item, |
|
|
|
|
|
|
77
|
44 |
|
default => $this->resolveMeta($item), |
|
|
|
|
|
|
78
|
|
|
}; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Retrieve the given meta of this case. |
|
83
|
|
|
* |
|
84
|
|
|
* @throws ValueError |
|
85
|
|
|
*/ |
|
86
|
35 |
|
public function resolveMeta(string $meta): mixed |
|
87
|
|
|
{ |
|
88
|
35 |
|
$enum = new ReflectionEnum($this); |
|
89
|
35 |
|
$enumFileName = $enum->getFileName(); |
|
90
|
|
|
|
|
91
|
35 |
|
foreach ($enum->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
|
92
|
35 |
|
if (! $method->isStatic() && $method->getFileName() == $enumFileName && $method->getShortName() == $meta) { |
|
93
|
7 |
|
return $this->$meta(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
28 |
|
return $this->resolveMetaAttribute($meta); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Retrieve the given meta from the attributes. |
|
102
|
|
|
* |
|
103
|
|
|
* @throws ValueError |
|
104
|
|
|
*/ |
|
105
|
40 |
|
public function resolveMetaAttribute(string $meta): mixed |
|
106
|
|
|
{ |
|
107
|
40 |
|
$case = new ReflectionEnumUnitCase($this, $this->name); |
|
108
|
|
|
|
|
109
|
40 |
|
foreach ($case->getAttributes(Meta::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) { |
|
110
|
40 |
|
if (($metadata = $attribute->newInstance())->has($meta)) { |
|
111
|
36 |
|
return $metadata->get($meta); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
36 |
|
foreach ($case->getEnum()->getAttributes(Meta::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) { |
|
116
|
36 |
|
if (($metadata = $attribute->newInstance())->has($meta)) { |
|
117
|
32 |
|
return $metadata->get($meta); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
4 |
|
throw new ValueError(sprintf('"%s" is not a valid meta for enum "%s"', $meta, self::class)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Retrieve the value of a backed case or the name of a pure case. |
|
126
|
|
|
*/ |
|
127
|
2 |
|
public function value(): string|int |
|
128
|
|
|
{ |
|
129
|
2 |
|
return $this->value ?? $this->name; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
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