Skip to content

Commit caa3ebd

Browse files
committed
Fix conversion of Enum::fromKey() to native enum
1 parent cab2e22 commit caa3ebd

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

CHANGELOG.md

+6
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.6.4
11+
12+
### Fixed
13+
14+
- Fix conversion of `Enum::fromKey()` to native enum
15+
1016
## 6.6.3
1117

1218
### Fixed

src/Rector/ToNativeUsagesRector.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BenSampo\Enum\Enum;
66
use BenSampo\Enum\Tests\Enums\UserType;
7+
use Illuminate\Support\Arr;
78
use PhpParser\Node;
89
use PhpParser\Node\Arg;
910
use PhpParser\Node\Expr;
@@ -41,7 +42,6 @@
4142
use PhpParser\Node\Param;
4243
use PhpParser\Node\Scalar\Encapsed;
4344
use PhpParser\Node\Scalar\EncapsedStringPart;
44-
use PhpParser\Node\Scalar\LNumber;
4545
use PhpParser\Node\Stmt\Case_;
4646
use PhpParser\Node\Stmt\Return_;
4747
use PhpParser\Node\Stmt\Switch_;
@@ -291,7 +291,13 @@ protected function refactorFromKey(StaticCall $call): ?Node
291291
],
292292
);
293293

294-
return new ArrayDimFetch($arrayMatching, new LNumber(0));
294+
return new StaticCall(
295+
new Name(Arr::class),
296+
'first',
297+
[
298+
new Arg($arrayMatching),
299+
],
300+
);
295301
};
296302

297303
if ($call->isFirstClassCallable()) {

tests/Rector/Usages/fromKey.php.inc

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ UserType::fromKey(...);
99

1010
use BenSampo\Enum\Tests\Enums\UserType;
1111

12-
array_filter(UserType::cases(), fn(UserType $userType): bool => $userType->name === 'foo')[0];
13-
static fn(string $key): UserType => array_filter(UserType::cases(), fn(UserType $userType): bool => $userType->name === $key)[0];
12+
Illuminate\Support\Arr::first(array_filter(UserType::cases(), fn(UserType $userType): bool => $userType->name === 'foo'));
13+
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)