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
|
98 |
|
public static function isPure(): bool |
22
|
|
|
{ |
23
|
98 |
|
return !self::isBacked(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Determine whether the enum is backed. |
28
|
|
|
*/ |
29
|
98 |
|
public static function isBacked(): bool |
30
|
|
|
{ |
31
|
98 |
|
return is_subclass_of(self::class, BackedEnum::class); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Determine whether the enum is backed by integer. |
36
|
|
|
*/ |
37
|
1 |
|
public static function isBackedByInteger(): bool |
38
|
|
|
{ |
39
|
1 |
|
return (new ReflectionEnum(self::class))->getBackingType()?->getName() === 'int'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Determine whether the enum is backed by string. |
44
|
|
|
*/ |
45
|
1 |
|
public static function isBackedByString(): bool |
46
|
|
|
{ |
47
|
1 |
|
return (new ReflectionEnum(self::class))->getBackingType()?->getName() === 'string'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Retrieve all the meta names of the enum. |
52
|
|
|
* |
53
|
|
|
* @return list<string> |
|
|
|
|
54
|
|
|
*/ |
55
|
3 |
|
public static function metaNames(): array |
56
|
|
|
{ |
57
|
3 |
|
$meta = self::metaAttributeNames(); |
58
|
2 |
|
$enum = new ReflectionEnum(self::class); |
59
|
|
|
|
60
|
2 |
|
foreach ($enum->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
61
|
2 |
|
if (! $method->isStatic() && $method->getFileName() == $enum->getFileName()) { |
62
|
2 |
|
$meta[] = $method->getShortName(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
return array_values(array_unique($meta)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Retrieve all the meta attribute names of the enum. |
71
|
|
|
* |
72
|
|
|
* @return list<string> |
73
|
|
|
*/ |
74
|
5 |
|
public static function metaAttributeNames(): array |
75
|
|
|
{ |
76
|
5 |
|
$meta = []; |
77
|
5 |
|
$enum = new ReflectionEnum(self::class); |
78
|
|
|
|
79
|
5 |
|
foreach ($enum->getAttributes(Meta::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) { |
80
|
5 |
|
array_push($meta, ...$attribute->newInstance()->names()); |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
foreach ($enum->getCases() as $case) { |
84
|
4 |
|
foreach ($case->getAttributes(Meta::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) { |
85
|
4 |
|
array_push($meta, ...$attribute->newInstance()->names()); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
return array_values(array_unique($meta)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Retrieve the given item of this case. |
94
|
|
|
* |
95
|
|
|
* @template TItemValue |
96
|
|
|
* |
97
|
|
|
* @param (callable(self): TItemValue)|string $item |
|
|
|
|
98
|
|
|
* @return TItemValue |
99
|
|
|
* @throws ValueError |
100
|
|
|
*/ |
101
|
44 |
|
public function resolveItem(callable|string $item): mixed |
102
|
|
|
{ |
103
|
|
|
return match (true) { |
104
|
44 |
|
is_callable($item) => $item($this), |
105
|
34 |
|
property_exists($this, $item) => $this->$item, |
|
|
|
|
106
|
44 |
|
default => $this->resolveMeta($item), |
|
|
|
|
107
|
|
|
}; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Retrieve the given meta of this case. |
112
|
|
|
* |
113
|
|
|
* @throws ValueError |
114
|
|
|
*/ |
115
|
35 |
|
public function resolveMeta(string $meta): mixed |
116
|
|
|
{ |
117
|
35 |
|
$enum = new ReflectionEnum($this); |
118
|
35 |
|
$enumFileName = $enum->getFileName(); |
119
|
|
|
|
120
|
35 |
|
foreach ($enum->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
121
|
35 |
|
if (! $method->isStatic() && $method->getFileName() == $enumFileName && $method->getShortName() == $meta) { |
122
|
7 |
|
return $this->$meta(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
28 |
|
return $this->resolveMetaAttribute($meta); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Retrieve the given meta from the attributes. |
131
|
|
|
* |
132
|
|
|
* @throws ValueError |
133
|
|
|
*/ |
134
|
40 |
|
public function resolveMetaAttribute(string $meta): mixed |
135
|
|
|
{ |
136
|
40 |
|
$case = new ReflectionEnumUnitCase($this, $this->name); |
137
|
|
|
|
138
|
40 |
|
foreach ($case->getAttributes(Meta::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) { |
139
|
40 |
|
if (($metadata = $attribute->newInstance())->has($meta)) { |
140
|
36 |
|
return $metadata->get($meta); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
36 |
|
foreach ($case->getEnum()->getAttributes(Meta::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) { |
145
|
36 |
|
if (($metadata = $attribute->newInstance())->has($meta)) { |
146
|
32 |
|
return $metadata->get($meta); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
4 |
|
throw new ValueError(sprintf('"%s" is not a valid meta for enum "%s"', $meta, self::class)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Retrieve the value of a backed case or the name of a pure case. |
155
|
|
|
*/ |
156
|
2 |
|
public function value(): string|int |
157
|
|
|
{ |
158
|
2 |
|
return $this->value ?? $this->name; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
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