Skip to content

Commit c4c3c1e

Browse files
committed
Simplify implementation of Enum::fromKey to native enum with dynamic class const fetch
1 parent db2ffc4 commit c4c3c1e

File tree

8 files changed

+52
-7
lines changed

8 files changed

+52
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
## 6.12.2
11+
12+
### Changed
13+
14+
- Simplify implementation of `Enum::fromKey` to native enum with dynamic class const fetch
15+
1016
## 6.12.1
1117

1218
### Fixed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ it: fix stan test docs ## Run the commonly used targets
55
help: ## Displays this list of targets with descriptions
66
@grep --extended-regexp '^[a-zA-Z0-9_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}'
77

8+
.PHONY: setup
9+
setup: vendor ## Set up the project
10+
811
.PHONY: fix
912
fix: vendor ## Apply automatic code fixes
1013
# TODO fix PHP Fatal error: Class PhpCsFixer\Fixer\Operator\AssignNullCoalescingToCoalesceEqualFixer contains 4 abstract methods and must therefore be declared abstract or implement the remaining methods (PhpCsFixer\Fixer\FixerInterface::isRisky, PhpCsFixer\Fixer\FixerInterface::fix, PhpCsFixer\Fixer\FixerInterface::getName, ...) in /home/bfranke/projects/laravel-enum/vendor/friendsofphp/php-cs-fixer/src/Fixer/Operator/AssignNullCoalescingToCoalesceEqualFixer.php on line 24
@@ -24,5 +27,5 @@ docs: ## Generate documentation
2427

2528
vendor: composer.json
2629
composer validate --strict
27-
composer install
30+
composer update
2831
composer normalize

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"phpstan/phpstan": "^1.12.19 || ^2.1.6",
4343
"phpstan/phpstan-mockery": "^1.1.3 || ^2",
4444
"phpstan/phpstan-phpunit": "^1.4.2 || ^2.0.4",
45-
"phpunit/phpunit": "^9.5.21 || ^10.5.45 || ^11.5.10 || ^12.0.5",
45+
"phpunit/phpunit": "^9.5.21 || ^10.5.45 || ^11.5.10",
4646
"rector/rector": "^1.2.10 || ^2.0.9",
4747
"symplify/rule-doc-generator": "^11.2 || ^12.2.5"
4848
},

src/Rector/ToNativeUsagesRector.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ protected function refactorFromKey(StaticCall $call): ?Node
277277
$class = $call->class;
278278
if ($class instanceof Name) {
279279
$makeFromKey = function (Expr $key) use ($class): Expr {
280+
if (version_compare(PHP_VERSION, '8.3.0', '>=')) {
281+
return $this->createEnumCaseAccess($class, $key);
282+
}
283+
280284
$paramName = lcfirst($class->getLast());
281285
$paramVariable = new Variable($paramName);
282286

@@ -1061,11 +1065,11 @@ protected function refactorArrowFunction(ArrowFunction $arrowFunction): ?ArrowFu
10611065
return null;
10621066
}
10631067

1064-
protected function createEnumCaseAccess(Name $class, string $constName): ClassConstFetch
1068+
protected function createEnumCaseAccess(Name $class, Expr|string $name): ClassConstFetch
10651069
{
10661070
return new ClassConstFetch(
10671071
$class,
1068-
$constName,
1072+
$name,
10691073
[self::CONVERTED_INSTANTIATION => true],
10701074
);
10711075
}

tests/Rector/ToNativeImplementationRectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function test(string $filePath): void
1515
$this->doTestFile($filePath);
1616
}
1717

18-
/** @return iterable<string> */
18+
/** @return iterable<array{string}> */
1919
public static function provideData(): iterable
2020
{
2121
return self::yieldFilesFromDirectory(__DIR__ . '/Implementation');

tests/Rector/ToNativeUsagesRectorTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,25 @@ public function test(string $filePath): void
1515
$this->doTestFile($filePath);
1616
}
1717

18-
/** @return iterable<string> */
18+
/** @return iterable<array{string}> */
1919
public static function provideData(): iterable
2020
{
21-
return self::yieldFilesFromDirectory(__DIR__ . '/Usages');
21+
foreach (self::yieldFilesFromDirectory(__DIR__ . '/Usages') as $fileArray) {
22+
[$file] = $fileArray;
23+
24+
// See https://wiki.php.net/rfc/dynamic_class_constant_fetch
25+
if (version_compare(PHP_VERSION, '8.3.0', '<')) {
26+
if ($file === __DIR__ . '/Usages/fromKey.dynamic_class_constant_fetch.php.inc') {
27+
continue;
28+
}
29+
} else {
30+
if ($file === __DIR__ . '/Usages/fromKey.php.inc') {
31+
continue;
32+
}
33+
}
34+
35+
yield $fileArray;
36+
}
2237
}
2338

2439
public function provideConfigFilePath(): string
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use BenSampo\Enum\Tests\Enums\UserType;
4+
5+
UserType::fromKey('foo');
6+
UserType::fromKey($key);
7+
UserType::fromKey(...);
8+
-----
9+
<?php
10+
11+
use BenSampo\Enum\Tests\Enums\UserType;
12+
13+
UserType::{'foo'};
14+
UserType::{$key};
15+
static fn(string $key): UserType => UserType::{$key};

tests/Rector/Usages/fromKey.php.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
use BenSampo\Enum\Tests\Enums\UserType;
44

55
UserType::fromKey('foo');
6+
UserType::fromKey($key);
67
UserType::fromKey(...);
78
-----
89
<?php
910

1011
use BenSampo\Enum\Tests\Enums\UserType;
1112

1213
Illuminate\Support\Arr::first(array_filter(UserType::cases(), fn(UserType $userType): bool => $userType->name === 'foo'));
14+
Illuminate\Support\Arr::first(array_filter(UserType::cases(), fn(UserType $userType): bool => $userType->name === $key));
1315
static fn(string $key): UserType => Illuminate\Support\Arr::first(array_filter(UserType::cases(), fn(UserType $userType): bool => $userType->name === $key));

0 commit comments

Comments
 (0)