Skip to content

Commit 29862f4

Browse files
committed
Refactor + Naming
1 parent 49ae19c commit 29862f4

24 files changed

+81
-77
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ if (PHP_VERSION_ID < 80100) {
1818
```
1919

2020
**Vendor\MyEnum-polyfill.php**
21+
2122
```php
2223
<?php declare(strict_types=1);
2324

2425
namespace Vendor;
2526

26-
use Mabe\Enum\Cl\IntEnumPolyfill;
27+
use Mabe\Enum\Cl\IntBackedEnum;
2728

28-
final class MyEnum extends IntEnumPolyfill
29+
final class MyEnum extends IntBackedEnum
2930
{
3031
const ZERO = 0;
3132
const ONE = 1;
@@ -106,4 +107,3 @@ namespace Vendor;
106107
MyEnum::ZERO; // 1 on PHP<8.1
107108
// enum(MyEnum::ZERO) on PHP>=8.1
108109
```
109-

examples/doctrine/composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
"homepage": "https://mabe.berlin/",
1010
"role": "Lead"
1111
}],
12+
"repositories": [{
13+
"type": "path",
14+
"url": "../.."
15+
}],
1216
"license": "BSD-3-Clause",
1317
"require": {
1418
"php": "^7.1 | ^8.0",

examples/doctrine/src/UserStatus-polyfill.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace Example;
44

5-
use Mabe\Enum\Cl\StringEnumPolyfill;
5+
use Mabe\Enum\Cl\StringBackedEnum;
66

77
/**
88
* @method static UserStatus ACTIVE()
99
* @method static UserStatus BANNED()
1010
* @method static UserStatus DELETED()
1111
*/
12-
final class UserStatus extends StringEnumPolyfill
12+
final class UserStatus extends StringBackedEnum
1313
{
1414
/** @internal */
1515
const ACTIVE = 'active';

src/Mabe/Enum/Cl/BackedEnumPolyfill.php renamed to src/Mabe/Enum/Cl/EmulatedBackedEnum.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
* @psalm-immutable
2323
*/
24-
abstract class BackedEnumPolyfill implements BackedEnum
24+
abstract class EmulatedBackedEnum implements BackedEnum
2525
{
2626
/**
2727
* The name of the current case
@@ -42,14 +42,14 @@ abstract class BackedEnumPolyfill implements BackedEnum
4242
/**
4343
* A map of case names and values by enumeration class
4444
*
45-
* @var array<class-string<BackedEnumPolyfill>, array<string, int|string>>
45+
* @var array<class-string<EmulatedBackedEnum>, array<string, int|string>>
4646
*/
4747
private static $caseConstants = [];
4848

4949
/**
5050
* A map of case names and instances by enumeration class
5151
*
52-
* @var array<class-string<BackedEnumPolyfill>, array<string, BackedEnumPolyfill>>
52+
* @var array<class-string<EmulatedBackedEnum>, array<string, EmulatedBackedEnum>>
5353
*/
5454
private static $cases = [];
5555

@@ -129,13 +129,13 @@ final public static function from($value): BackedEnum
129129

130130
$name = \array_search($value, self::$caseConstants[static::class], true);
131131
if ($name === false) {
132-
if (\is_subclass_of(static::class, IntEnumPolyfill::class)) {
132+
if (\is_subclass_of(static::class, IntBackedEnum::class)) {
133133
if (!\is_int($value)) {
134134
throw new TypeError(static::class . '::from(): Argument #1 ($value) must be of type int, ' . \get_debug_type($value) . ' given');
135135
}
136136

137137
throw new ValueError("{$value} is not a valid backing value for enum \"" . static::class . '"');
138-
} elseif (\is_subclass_of(static::class, StringEnumPolyfill::class)) {
138+
} elseif (\is_subclass_of(static::class, StringBackedEnum::class)) {
139139
if (!\is_string($value)) {
140140
throw new TypeError(static::class . '::from(): Argument #1 ($value) must be of type string, ' . \get_debug_type($value) . ' given');
141141
}
@@ -214,8 +214,8 @@ private static function init(string $enumClass)
214214
$cases = [];
215215
foreach ($caseConstants as $name => $value) {
216216
assert(
217-
(\is_subclass_of($enumClass, IntEnumPolyfill::class) && \is_int($value))
218-
|| (\is_subclass_of($enumClass, StringEnumPolyfill::class) && \is_string($value)),
217+
(\is_subclass_of($enumClass, IntBackedEnum::class) && \is_int($value))
218+
|| (\is_subclass_of($enumClass, StringBackedEnum::class) && \is_string($value)),
219219
"Enum case constant \"{$enumClass}::{$name}\" does not match enum backing type"
220220
);
221221

src/Mabe/Enum/Cl/UnitEnumPolyfill.php renamed to src/Mabe/Enum/Cl/EmulatedUnitEnum.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @psalm-immutable
2222
*/
23-
abstract class UnitEnumPolyfill implements UnitEnum
23+
abstract class EmulatedUnitEnum implements UnitEnum
2424
{
2525
/**
2626
* The name of the current case
@@ -33,7 +33,7 @@ abstract class UnitEnumPolyfill implements UnitEnum
3333
/**
3434
* A map of case names and instances by enumeration class
3535
*
36-
* @var array<class-string<BackedEnumPolyfill>, array<string, BackedEnumPolyfill>>
36+
* @var array<class-string<EmulatedBackedEnum>, array<string, EmulatedBackedEnum>>
3737
*/
3838
private static $cases = [];
3939

src/Mabe/Enum/Cl/IntEnumPolyfill.php renamed to src/Mabe/Enum/Cl/IntBackedEnum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* @psalm-immutable
1313
*/
14-
abstract class IntEnumPolyfill extends BackedEnumPolyfill
14+
abstract class IntBackedEnum extends EmulatedBackedEnum
1515
{
1616
/**
1717
* The value of the current case

src/Mabe/Enum/Cl/StringEnumPolyfill.php renamed to src/Mabe/Enum/Cl/StringBackedEnum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* @psalm-immutable
1313
*/
14-
abstract class StringEnumPolyfill extends BackedEnumPolyfill
14+
abstract class StringBackedEnum extends EmulatedBackedEnum
1515
{
1616
/**
1717
* The value of the current case

src/functions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ function enum_exists(string $enum, bool $autoload = true) : bool
7070
}
7171

7272
return \class_exists($enum, $autoload) && (
73-
\is_a($enum, __NAMESPACE__ . '\\IntEnumPolyfill', true)
74-
|| \is_a($enum, __NAMESPACE__ . '\\StringEnumPolyfill', true)
73+
\is_a($enum, __NAMESPACE__ . '\\IntBackedEnum', true)
74+
|| \is_a($enum, __NAMESPACE__ . '\\StringBackedEnum', true)
7575
);
7676
}
7777
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
final class AmbiguousStringValuesEnumEmulated extends Mabe\Enum\Cl\StringBackedEnum
4+
{
5+
const TEST1 = 'test';
6+
const TEST2 = 'test';
7+
}
8+
9+
final class AmbiguousIntValuesEnumEmulated extends Mabe\Enum\Cl\IntBackedEnum
10+
{
11+
const TEST1 = 1;
12+
const TEST2 = 1;
13+
}

tests/AmbiguousBackingValuesEnumPolyfillTest.php renamed to tests/AmbiguousBackingValuesEnumEmulatedTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
use PHPUnit\Framework\TestCase;
44

55
if (PHP_VERSION_ID < 80100) {
6-
require_once __DIR__ . '/AmbiguousBackingValuesEnumPolyfill.php';
6+
require_once __DIR__ . '/AmbiguousBackingValuesEnumEmulated.php';
77
}
88

9-
class AmbiguousBackingValuesEnumPolyfillTest extends TestCase
9+
class AmbiguousBackingValuesEnumEmulatedTest extends TestCase
1010
{
1111
public function setUp(): void
1212
{
@@ -18,14 +18,14 @@ public function setUp(): void
1818
public function testAmbiguousBackingValuesForIntBackedEnum()
1919
{
2020
$this->expectException('AssertionError');
21-
$this->expectExceptionMessage('Enum case value for AmbiguousIntValuesEnumPolyfill::TEST1 is ambiguous');
22-
AmbiguousIntValuesEnumPolyfill::cases();
21+
$this->expectExceptionMessage('Enum case value for AmbiguousIntValuesEnumEmulated::TEST1 is ambiguous');
22+
AmbiguousIntValuesEnumEmulated::cases();
2323
}
2424

2525
public function testAmbiguousBackingValuesEnumPolyfillForStringBackedEnum()
2626
{
2727
$this->expectException('AssertionError');
28-
$this->expectExceptionMessage('Enum case value for AmbiguousStringValuesEnumPolyfill::TEST1 is ambiguous');
29-
AmbiguousStringValuesEnumPolyfill::cases();
28+
$this->expectExceptionMessage('Enum case value for AmbiguousStringValuesEnumEmulated::TEST1 is ambiguous');
29+
AmbiguousStringValuesEnumEmulated::cases();
3030
}
3131
}

0 commit comments

Comments
 (0)