Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## 6.12.2

### Changed

- Simplify implementation of `Enum::fromKey` to native enum with dynamic class const fetch

## 6.12.1

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

.PHONY: setup
setup: vendor ## Set up the project

.PHONY: fix
fix: vendor ## Apply automatic code fixes
# 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
Expand All @@ -24,5 +27,5 @@ docs: ## Generate documentation

vendor: composer.json
composer validate --strict
composer install
composer update
composer normalize
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"phpstan/phpstan": "^1.12.19 || ^2.1.6",
"phpstan/phpstan-mockery": "^1.1.3 || ^2",
"phpstan/phpstan-phpunit": "^1.4.2 || ^2.0.4",
"phpunit/phpunit": "^9.5.21 || ^10.5.45 || ^11.5.10 || ^12.0.5",
"phpunit/phpunit": "^9.5.21 || ^10.5.45 || ^11.5.10",
"rector/rector": "^1.2.10 || ^2.0.9",
"symplify/rule-doc-generator": "^11.2 || ^12.2.5"
},
Expand Down
8 changes: 6 additions & 2 deletions src/Rector/ToNativeUsagesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ protected function refactorFromKey(StaticCall $call): ?Node
$class = $call->class;
if ($class instanceof Name) {
$makeFromKey = function (Expr $key) use ($class): Expr {
if (version_compare(PHP_VERSION, '8.3.0', '>=')) {
return $this->createEnumCaseAccess($class, $key);
}

$paramName = lcfirst($class->getLast());
$paramVariable = new Variable($paramName);

Expand Down Expand Up @@ -1061,11 +1065,11 @@ protected function refactorArrowFunction(ArrowFunction $arrowFunction): ?ArrowFu
return null;
}

protected function createEnumCaseAccess(Name $class, string $constName): ClassConstFetch
protected function createEnumCaseAccess(Name $class, Expr|string $name): ClassConstFetch
{
return new ClassConstFetch(
$class,
$constName,
$name,
[self::CONVERTED_INSTANTIATION => true],
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Rector/ToNativeImplementationRectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function test(string $filePath): void
$this->doTestFile($filePath);
}

/** @return iterable<string> */
/** @return iterable<array{string}> */
public static function provideData(): iterable
{
return self::yieldFilesFromDirectory(__DIR__ . '/Implementation');
Expand Down
19 changes: 17 additions & 2 deletions tests/Rector/ToNativeUsagesRectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,25 @@ public function test(string $filePath): void
$this->doTestFile($filePath);
}

/** @return iterable<string> */
/** @return iterable<array{string}> */
public static function provideData(): iterable
{
return self::yieldFilesFromDirectory(__DIR__ . '/Usages');
foreach (self::yieldFilesFromDirectory(__DIR__ . '/Usages') as $fileArray) {
[$file] = $fileArray;

// See https://wiki.php.net/rfc/dynamic_class_constant_fetch
if (version_compare(PHP_VERSION, '8.3.0', '<')) {
if ($file === __DIR__ . '/Usages/fromKey.dynamic_class_constant_fetch.php.inc') {
continue;
}
} else {
if ($file === __DIR__ . '/Usages/fromKey.php.inc') {
continue;
}
}

yield $fileArray;
}
}

public function provideConfigFilePath(): string
Expand Down
15 changes: 15 additions & 0 deletions tests/Rector/Usages/fromKey.dynamic_class_constant_fetch.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use BenSampo\Enum\Tests\Enums\UserType;

UserType::fromKey('foo');
UserType::fromKey($key);
UserType::fromKey(...);
-----
<?php

use BenSampo\Enum\Tests\Enums\UserType;

UserType::{'foo'};
UserType::{$key};
static fn(string $key): UserType => UserType::{$key};
2 changes: 2 additions & 0 deletions tests/Rector/Usages/fromKey.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
use BenSampo\Enum\Tests\Enums\UserType;

UserType::fromKey('foo');
UserType::fromKey($key);
UserType::fromKey(...);
-----
<?php

use BenSampo\Enum\Tests\Enums\UserType;

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