Skip to content

Commit d82cb88

Browse files
committed
Merge branch 'feature/initial-enums' into feature/unit-tests
2 parents 79dc957 + 006baa1 commit d82cb88

File tree

3 files changed

+32
-101
lines changed

3 files changed

+32
-101
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,16 @@ vendor/
1212
[Dd]esktop.ini
1313
*.DS_store
1414
.DS_store?
15+
16+
############
17+
## AI Tools
18+
############
19+
1520
.claude/
21+
CLAUDE.md
22+
23+
############
24+
## PHPUnit
25+
############
26+
1627
.phpunit.cache/

CLAUDE.md

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/Common/AbstractEnum.php

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,25 @@ abstract class AbstractEnum
4040
/**
4141
* @var string The name of the enum constant
4242
*/
43-
private $name;
43+
private string $name;
4444

4545
/**
4646
* @var array<string, array<string, string|int>> Cache for reflection data
4747
*/
48-
private static $cache = [];
48+
private static array $cache = [];
4949

5050
/**
5151
* @var array<string, array<string, self>> Cache for enum instances
5252
*/
53-
private static $instances = [];
53+
private static array $instances = [];
5454

5555
/**
5656
* Constructor is private to ensure instances are created through static methods
5757
*
5858
* @param string|int $value The enum value
5959
* @param string $name The constant name
6060
*/
61-
private function __construct($value, string $name)
61+
final private function __construct($value, string $name)
6262
{
6363
$this->value = $value;
6464
$this->name = $name;
@@ -71,7 +71,7 @@ private function __construct($value, string $name)
7171
* @return mixed
7272
* @throws BadMethodCallException If property doesn't exist
7373
*/
74-
public function __get(string $property)
74+
final public function __get(string $property)
7575
{
7676
if ($property === 'value' || $property === 'name') {
7777
return $this->$property;
@@ -89,7 +89,7 @@ public function __get(string $property)
8989
* @param mixed $value The value to set
9090
* @throws BadMethodCallException Always, as enum properties are read-only
9191
*/
92-
public function __set(string $property, $value): void
92+
final public function __set(string $property, $value): void
9393
{
9494
throw new BadMethodCallException(
9595
sprintf('Cannot modify property %s::%s - enum properties are read-only', static::class, $property)
@@ -103,7 +103,7 @@ public function __set(string $property, $value): void
103103
* @return static
104104
* @throws InvalidArgumentException If the value is not valid
105105
*/
106-
public static function from($value): self
106+
final public static function from($value): self
107107
{
108108
$instance = self::tryFrom($value);
109109
if ($instance === null) {
@@ -120,7 +120,7 @@ public static function from($value): self
120120
* @param string|int $value The enum value
121121
* @return static|null
122122
*/
123-
public static function tryFrom($value): ?self
123+
final public static function tryFrom($value): ?self
124124
{
125125
$constants = self::getConstants();
126126
foreach ($constants as $name => $constantValue) {
@@ -136,7 +136,7 @@ public static function tryFrom($value): ?self
136136
*
137137
* @return static[]
138138
*/
139-
public static function cases(): array
139+
final public static function cases(): array
140140
{
141141
$cases = [];
142142
$constants = self::getConstants();
@@ -152,7 +152,7 @@ public static function cases(): array
152152
* @param string|int|self $other The value or enum to compare
153153
* @return bool
154154
*/
155-
public function equals($other): bool
155+
final public function equals($other): bool
156156
{
157157
if ($other instanceof self) {
158158
return $this->is($other);
@@ -167,7 +167,7 @@ public function equals($other): bool
167167
* @param self $other The other enum to compare
168168
* @return bool
169169
*/
170-
public function is(self $other): bool
170+
final public function is(self $other): bool
171171
{
172172
return $this === $other; // Since we're using singletons, we can use identity comparison
173173
}
@@ -177,7 +177,7 @@ public function is(self $other): bool
177177
*
178178
* @return array<string, string|int>
179179
*/
180-
public static function getValues(): array
180+
final public static function getValues(): array
181181
{
182182
return self::getConstants();
183183
}
@@ -188,48 +188,33 @@ public static function getValues(): array
188188
* @param string|int $value The value to check
189189
* @return bool
190190
*/
191-
public static function isValidValue($value): bool
191+
final public static function isValidValue($value): bool
192192
{
193193
return in_array($value, self::getValues(), true);
194194
}
195195

196-
/**
197-
* Create an enum instance from a value (deprecated, use from() instead)
198-
*
199-
* @param string|int $value The enum value
200-
* @return static
201-
* @throws InvalidArgumentException If the value is not valid
202-
* @deprecated Use from() method instead
203-
*/
204-
public static function fromValue($value): self
205-
{
206-
return self::from($value);
207-
}
208-
209196
/**
210197
* Get or create a singleton instance for the given value and name
211198
*
212199
* @param string|int $value The enum value
213200
* @param string $name The constant name
214201
* @return static
215-
* @phpstan-return static
216202
*/
217203
private static function getInstance($value, string $name): self
218204
{
219205
$className = static::class;
220-
$key = $name;
221206

222207
if (!isset(self::$instances[$className])) {
223208
self::$instances[$className] = [];
224209
}
225210

226-
if (!isset(self::$instances[$className][$key])) {
211+
if (!isset(self::$instances[$className][$name])) {
227212
$instance = new $className($value, $name);
228-
self::$instances[$className][$key] = $instance;
213+
self::$instances[$className][$name] = $instance;
229214
}
230215

231216
/** @var static */
232-
return self::$instances[$className][$key];
217+
return self::$instances[$className][$name];
233218
}
234219

235220
/**
@@ -238,7 +223,7 @@ private static function getInstance($value, string $name): self
238223
* @return array<string, string|int>
239224
* @throws \RuntimeException If invalid constant found
240225
*/
241-
protected static function getConstants(): array
226+
final protected static function getConstants(): array
242227
{
243228
$className = static::class;
244229

@@ -283,14 +268,14 @@ protected static function getConstants(): array
283268
}
284269

285270
/**
286-
* Handle dynamic method calls for enum creation and checking
271+
* Handle dynamic method calls for enum checking
287272
*
288273
* @param string $name The method name
289274
* @param array<mixed> $arguments The method arguments
290275
* @return bool
291276
* @throws BadMethodCallException If the method doesn't exist
292277
*/
293-
public function __call(string $name, array $arguments)
278+
final public function __call(string $name, array $arguments): bool
294279
{
295280
// Handle is* methods
296281
if (strpos($name, 'is') === 0) {
@@ -315,7 +300,7 @@ public function __call(string $name, array $arguments)
315300
* @return static
316301
* @throws BadMethodCallException If the method doesn't exist
317302
*/
318-
public static function __callStatic(string $name, array $arguments)
303+
final public static function __callStatic(string $name, array $arguments): self
319304
{
320305
$constantName = self::camelCaseToConstant($name);
321306
$constants = self::getConstants();
@@ -349,7 +334,7 @@ private static function camelCaseToConstant(string $camelCase): string
349334
*
350335
* @return string
351336
*/
352-
public function __toString(): string
337+
final public function __toString(): string
353338
{
354339
return (string) $this->value;
355340
}

0 commit comments

Comments
 (0)