diff --git a/CHANGELOG.md b/CHANGELOG.md index 214dc83..48bea85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ 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` + ## 6.12.0 ### Added 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 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); diff --git a/src/Rector/ToNativeRector.php b/src/Rector/ToNativeRector.php index 6c19524..2ca0850 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\Constant\ConstantBooleanType; use PHPStan\Type\ObjectType; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\PhpParser\Node\Value\ValueResolver; @@ -36,6 +37,14 @@ public function configure(array $configuration): void protected function inConfiguredClasses(Node $node): bool { + // 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; + } + foreach ($this->classes as $class) { if ($this->isObjectType($node, $class)) { return true; diff --git a/tests/Rector/Usages/never.php.inc b/tests/Rector/Usages/never.php.inc new file mode 100644 index 0000000..0972d4f --- /dev/null +++ b/tests/Rector/Usages/never.php.inc @@ -0,0 +1,21 @@ +