Skip to content

Commit db2ffc4

Browse files
authored
Handle NeverType when converting to native enums (#356)
1 parent af2b84b commit db2ffc4

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
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.12.1
11+
12+
### Fixed
13+
14+
- Avoid false-positive addition of `->value` in `enum:to-native`
15+
1016
## 6.12.0
1117

1218
### Added

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ help: ## Displays this list of targets with descriptions
77

88
.PHONY: fix
99
fix: vendor ## Apply automatic code fixes
10-
vendor/bin/php-cs-fixer fix
10+
# 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
11+
#vendor/bin/php-cs-fixer fix
1112

1213
.PHONY: stan
1314
stan: vendor ## Runs a static analysis with phpstan

src/FlaggedEnum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ abstract class FlaggedEnum extends Enum
3333
*/
3434
public function __construct(mixed $flags = [])
3535
{
36-
unset($this->key, $this->description);
36+
unset($this->key, $this->description); // @phpstan-ignore unset.possiblyHookedProperty,unset.possiblyHookedProperty (latest PHPStan on PHP 8.4)
3737

3838
if (is_array($flags)) {
3939
$this->setFlags($flags);

src/Rector/ToNativeRector.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Arr;
66
use PhpParser\Node;
7+
use PHPStan\Type\Constant\ConstantBooleanType;
78
use PHPStan\Type\ObjectType;
89
use Rector\Contract\Rector\ConfigurableRectorInterface;
910
use Rector\PhpParser\Node\Value\ValueResolver;
@@ -36,6 +37,14 @@ public function configure(array $configuration): void
3637

3738
protected function inConfiguredClasses(Node $node): bool
3839
{
40+
// When `get_class(<non-object>)` is used as a string, e.g. `get_class(0) . ''`,
41+
// isObjectType produces true - thus triggering a refactor: `get_class(0)->value . ''`.
42+
// To avoid this, we check if the node is constant a boolean type (true or false).
43+
$nodeType = $this->getType($node);
44+
if ($nodeType->isTrue()->yes() || $nodeType->isFalse()->yes()) {
45+
return false;
46+
}
47+
3948
foreach ($this->classes as $class) {
4049
if ($this->isObjectType($node, $class)) {
4150
return true;

tests/Rector/Usages/never.php.inc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace BenSampo\Enum\Tests\Rector\Usages;
4+
5+
// Put something here that changes to avoid warnings
6+
use BenSampo\Enum\Tests\Enums\UserType;
7+
new UserType(UserType::Administrator);
8+
9+
// False-positive never type
10+
get_class(0) . '';
11+
-----
12+
<?php
13+
14+
namespace BenSampo\Enum\Tests\Rector\Usages;
15+
16+
// Put something here that changes to avoid warnings
17+
use BenSampo\Enum\Tests\Enums\UserType;
18+
UserType::Administrator;
19+
20+
// False-positive never type
21+
get_class(0) . '';

0 commit comments

Comments
 (0)