From 204a2827951de3bd0a65f99fa14f02224a6f19be Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Tue, 5 Mar 2024 19:19:48 +0100 Subject: [PATCH 01/10] Handle NeverType https://github.com/BenSampo/laravel-enum/issues/355 --- src/Rector/ToNativeRector.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Rector/ToNativeRector.php b/src/Rector/ToNativeRector.php index 6c19524..941fe0e 100644 --- a/src/Rector/ToNativeRector.php +++ b/src/Rector/ToNativeRector.php @@ -4,6 +4,7 @@ use Illuminate\Support\Arr; use PhpParser\Node; +use PHPStan\Type\NeverType; use PHPStan\Type\ObjectType; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\PhpParser\Node\Value\ValueResolver; @@ -36,6 +37,10 @@ public function configure(array $configuration): void protected function inConfiguredClasses(Node $node): bool { + if ($this->getType($node) instanceof NeverType) { + return false; + } + foreach ($this->classes as $class) { if ($this->isObjectType($node, $class)) { return true; From 567529d01a132e6cf3a26570ed68ccf55be9e30b Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Fri, 8 Mar 2024 15:32:14 +0100 Subject: [PATCH 02/10] never --- src/Rector/ToNativeRector.php | 6 ++++-- tests/Rector/ToNativeUsagesRectorTest.php | 1 + tests/Rector/Usages/never.php.inc | 11 +++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 tests/Rector/Usages/never.php.inc diff --git a/src/Rector/ToNativeRector.php b/src/Rector/ToNativeRector.php index 941fe0e..750fc49 100644 --- a/src/Rector/ToNativeRector.php +++ b/src/Rector/ToNativeRector.php @@ -4,7 +4,7 @@ use Illuminate\Support\Arr; use PhpParser\Node; -use PHPStan\Type\NeverType; +use PHPStan\Type\Constant\ConstantBooleanType; use PHPStan\Type\ObjectType; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\PhpParser\Node\Value\ValueResolver; @@ -37,7 +37,9 @@ public function configure(array $configuration): void protected function inConfiguredClasses(Node $node): bool { - if ($this->getType($node) instanceof NeverType) { + // I don't understand why, but get_class() is used in concat: '' . get_class(0) + // Somehow isObjectType produces true - thus leading rector to make this: '' . get_class(ß)->value + if ($this->getType($node) instanceof ConstantBooleanType) { return false; } diff --git a/tests/Rector/ToNativeUsagesRectorTest.php b/tests/Rector/ToNativeUsagesRectorTest.php index 325f463..7b62e78 100644 --- a/tests/Rector/ToNativeUsagesRectorTest.php +++ b/tests/Rector/ToNativeUsagesRectorTest.php @@ -16,6 +16,7 @@ public function test(string $filePath): void /** @return iterable */ public static function provideData(): iterable { + yield [__DIR__. '/Usages/never.php.inc']; return self::yieldFilesFromDirectory(__DIR__ . '/Usages'); } diff --git a/tests/Rector/Usages/never.php.inc b/tests/Rector/Usages/never.php.inc new file mode 100644 index 0000000..6804d16 --- /dev/null +++ b/tests/Rector/Usages/never.php.inc @@ -0,0 +1,11 @@ + Date: Fri, 8 Mar 2024 15:32:23 +0100 Subject: [PATCH 03/10] format --- tests/Rector/ToNativeUsagesRectorTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Rector/ToNativeUsagesRectorTest.php b/tests/Rector/ToNativeUsagesRectorTest.php index 7b62e78..0f889cb 100644 --- a/tests/Rector/ToNativeUsagesRectorTest.php +++ b/tests/Rector/ToNativeUsagesRectorTest.php @@ -16,7 +16,8 @@ public function test(string $filePath): void /** @return iterable */ public static function provideData(): iterable { - yield [__DIR__. '/Usages/never.php.inc']; + yield [__DIR__ . '/Usages/never.php.inc']; + return self::yieldFilesFromDirectory(__DIR__ . '/Usages'); } From a5b2e53f8679d26a07438a54b865eca06822e220 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Fri, 8 Mar 2024 15:32:39 +0100 Subject: [PATCH 04/10] fix --- tests/Rector/ToNativeUsagesRectorTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Rector/ToNativeUsagesRectorTest.php b/tests/Rector/ToNativeUsagesRectorTest.php index 0f889cb..325f463 100644 --- a/tests/Rector/ToNativeUsagesRectorTest.php +++ b/tests/Rector/ToNativeUsagesRectorTest.php @@ -16,8 +16,6 @@ public function test(string $filePath): void /** @return iterable */ public static function provideData(): iterable { - yield [__DIR__ . '/Usages/never.php.inc']; - return self::yieldFilesFromDirectory(__DIR__ . '/Usages'); } From 3c6437a15b6d3b8f7046ae993df32412c31786b0 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Fri, 8 Mar 2024 15:37:11 +0100 Subject: [PATCH 05/10] cl --- CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7128c00..a673c1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## 6.10.1 + +### Fixed + +- Avoid false-positive addition of `->value` in `enum:to-native` + +## 6.10.0 + +### Added + +- Allow installation alongside PHPUnit 11 + ## 6.9.1 ### Fixed From 714665b8087d9fd9d4319e30cf9410ad15140784 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Wed, 14 May 2025 16:01:03 +0200 Subject: [PATCH 06/10] ignore php-cs-fixer --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e65c5c5..a127196 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,8 @@ help: ## Displays this list of targets with descriptions .PHONY: fix fix: vendor ## Apply automatic code fixes - vendor/bin/php-cs-fixer fix + # 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 + #vendor/bin/php-cs-fixer fix .PHONY: stan stan: vendor ## Runs a static analysis with phpstan From 05d161f7785c42987ce9a0015b2a66b443f0f498 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Wed, 14 May 2025 16:01:16 +0200 Subject: [PATCH 07/10] fix test and stan warnings --- src/Rector/ToNativeRector.php | 3 ++- tests/Rector/Usages/never.php.inc | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Rector/ToNativeRector.php b/src/Rector/ToNativeRector.php index 750fc49..903f24a 100644 --- a/src/Rector/ToNativeRector.php +++ b/src/Rector/ToNativeRector.php @@ -39,7 +39,8 @@ protected function inConfiguredClasses(Node $node): bool { // I don't understand why, but get_class() is used in concat: '' . get_class(0) // Somehow isObjectType produces true - thus leading rector to make this: '' . get_class(ß)->value - if ($this->getType($node) instanceof ConstantBooleanType) { + $nodeType = $this->getType($node); + if ($nodeType->isTrue()->yes() || $nodeType->isFalse()->yes()) { return false; } diff --git a/tests/Rector/Usages/never.php.inc b/tests/Rector/Usages/never.php.inc index 6804d16..0972d4f 100644 --- a/tests/Rector/Usages/never.php.inc +++ b/tests/Rector/Usages/never.php.inc @@ -2,10 +2,20 @@ namespace BenSampo\Enum\Tests\Rector\Usages; +// Put something here that changes to avoid warnings +use BenSampo\Enum\Tests\Enums\UserType; +new UserType(UserType::Administrator); + +// False-positive never type get_class(0) . ''; ----- Date: Wed, 14 May 2025 16:04:01 +0200 Subject: [PATCH 08/10] comment --- src/Rector/ToNativeRector.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Rector/ToNativeRector.php b/src/Rector/ToNativeRector.php index 903f24a..2ca0850 100644 --- a/src/Rector/ToNativeRector.php +++ b/src/Rector/ToNativeRector.php @@ -37,8 +37,9 @@ public function configure(array $configuration): void protected function inConfiguredClasses(Node $node): bool { - // I don't understand why, but get_class() is used in concat: '' . get_class(0) - // Somehow isObjectType produces true - thus leading rector to make this: '' . get_class(ß)->value + // When `get_class()` is used as a string, e.g. `get_class(0) . ''`, + // isObjectType produces true - thus triggering a refactor: `get_class(0)->value . ''`. + // To avoid this, we check if the node is constant a boolean type (true or false). $nodeType = $this->getType($node); if ($nodeType->isTrue()->yes() || $nodeType->isFalse()->yes()) { return false; From 8ff89762d1ba207aaa0168eb5f12030b8be42e8d Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Wed, 14 May 2025 16:07:56 +0200 Subject: [PATCH 09/10] 6.12.1 --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b77d59..48bea85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## 6.12.1 + ### Fixed - Avoid false-positive addition of `->value` in `enum:to-native` From aa2a3e63cbc66f28ab27de4279384cf50e6fcf2b Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Wed, 14 May 2025 16:19:10 +0200 Subject: [PATCH 10/10] PHPStan --- src/FlaggedEnum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FlaggedEnum.php b/src/FlaggedEnum.php index 7f7855f..9273dfe 100644 --- a/src/FlaggedEnum.php +++ b/src/FlaggedEnum.php @@ -33,7 +33,7 @@ abstract class FlaggedEnum extends Enum */ public function __construct(mixed $flags = []) { - unset($this->key, $this->description); + unset($this->key, $this->description); // @phpstan-ignore unset.possiblyHookedProperty,unset.possiblyHookedProperty (latest PHPStan on PHP 8.4) if (is_array($flags)) { $this->setFlags($flags);