1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Cerbero\Enum\Services; |
||
6 | |||
7 | use Cerbero\Enum\Concerns\Enumerates; |
||
8 | use Cerbero\Enum\Data\MethodAnnotation; |
||
9 | use InvalidArgumentException; |
||
10 | use ReflectionEnum; |
||
11 | |||
12 | use function Cerbero\Enum\traitsUsedBy; |
||
13 | |||
14 | /** |
||
15 | * The enum inspector. |
||
16 | * |
||
17 | * @template TEnum of \UnitEnum |
||
18 | */ |
||
19 | class Inspector |
||
20 | { |
||
21 | /** |
||
22 | * The main trait to supercharge enums. |
||
23 | */ |
||
24 | protected string $mainTrait = Enumerates::class; |
||
25 | |||
26 | /** |
||
27 | * The enum reflection. |
||
28 | * |
||
29 | * @var ReflectionEnum<TEnum> |
||
30 | */ |
||
31 | protected ReflectionEnum $reflection; |
||
32 | |||
33 | /** |
||
34 | * The method annotations. |
||
35 | * |
||
36 | * @var array<string, MethodAnnotation> |
||
37 | */ |
||
38 | protected array $methodAnnotations; |
||
39 | |||
40 | /** |
||
41 | * The use statements. |
||
42 | * |
||
43 | * @var array<string, class-string> |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
44 | */ |
||
45 | protected array $useStatements; |
||
46 | |||
47 | /** |
||
48 | * Instantiate the class. |
||
49 | * |
||
50 | * @param class-string<TEnum> $enum |
||
0 ignored issues
–
show
|
|||
51 | */ |
||
52 | 6 | public function __construct(protected string $enum) |
|
53 | { |
||
54 | 6 | $this->reflection = new ReflectionEnum($enum); |
|
55 | |||
56 | 6 | $this->assertEnumUsesMainTrait(); |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * Assert that the enum uses the main trait. |
||
61 | */ |
||
62 | 6 | protected function assertEnumUsesMainTrait(): void |
|
63 | { |
||
64 | 6 | if (! $this->uses($this->mainTrait)) { |
|
65 | 1 | throw new InvalidArgumentException("The enum {$this->enum} must use the trait {$this->mainTrait}"); |
|
66 | } |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Retrieve the enum filename. |
||
71 | */ |
||
72 | 4 | public function filename(): string |
|
73 | { |
||
74 | 4 | return (string) $this->reflection->getFileName(); |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Retrieve the DocBlock of the enum. |
||
79 | */ |
||
80 | 5 | public function docBlock(): string |
|
81 | { |
||
82 | 5 | return $this->reflection->getDocComment() ?: ''; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Retrieve the enum cases. |
||
87 | * |
||
88 | * @return list<TEnum> |
||
89 | */ |
||
90 | 5 | public function cases(): array |
|
91 | { |
||
92 | /** @var list<TEnum> */ |
||
93 | 5 | return $this->enum::cases(); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Retrieve the meta attribute names of the enum. |
||
98 | * |
||
99 | * @return list<string> |
||
100 | */ |
||
101 | 5 | public function metaAttributeNames(): array |
|
102 | { |
||
103 | /** @var list<string> */ |
||
104 | 5 | return $this->enum::metaAttributeNames(); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Determine whether the enum uses the given trait. |
||
109 | */ |
||
110 | 6 | public function uses(string $trait): bool |
|
111 | { |
||
112 | 6 | return isset($this->traits()[$trait]); |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * Retrieve all the enum traits. |
||
117 | * |
||
118 | * @return array<class-string, class-string> |
||
0 ignored issues
–
show
|
|||
119 | */ |
||
120 | 6 | public function traits(): array |
|
121 | { |
||
122 | 6 | $traits = []; |
|
123 | |||
124 | 6 | foreach ($this->reflection->getTraitNames() as $trait) { |
|
125 | 5 | $traits += [$trait => $trait, ...traitsUsedBy($trait)]; |
|
126 | } |
||
127 | |||
128 | /** @var array<class-string, class-string> */ |
||
129 | 6 | return $traits; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Retrieve the use statements. |
||
134 | * |
||
135 | * @return array<string, class-string> |
||
0 ignored issues
–
show
|
|||
136 | */ |
||
137 | 4 | public function useStatements(bool $includeExisting = true): array |
|
138 | { |
||
139 | 4 | return $this->useStatements ??= [...new UseStatements($this, $includeExisting)]; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Retrieve the method annotations. |
||
144 | * |
||
145 | * @return array<string, MethodAnnotation> |
||
146 | */ |
||
147 | 5 | public function methodAnnotations(bool $includeExisting = true): array |
|
148 | { |
||
149 | 5 | return $this->methodAnnotations ??= [...new MethodAnnotations($this, $includeExisting)]; |
|
150 | } |
||
151 | } |
||
152 |