Skip to content

Handle NeverType when converting to native enums #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 14, 2025
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.1

### Fixed

- Avoid false-positive addition of `->value` in `enum:to-native`

## 6.12.0

### Added
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/FlaggedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/Rector/ToNativeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,6 +37,14 @@ public function configure(array $configuration): void

protected function inConfiguredClasses(Node $node): bool
{
// When `get_class(<non-object>)` 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;
Expand Down
21 changes: 21 additions & 0 deletions tests/Rector/Usages/never.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

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) . '';
-----
<?php

namespace BenSampo\Enum\Tests\Rector\Usages;

// Put something here that changes to avoid warnings
use BenSampo\Enum\Tests\Enums\UserType;
UserType::Administrator;

// False-positive never type
get_class(0) . '';
Loading