Skip to content

Commit 9abc4ab

Browse files
authored
[PHP 8.1] Refactor isEnum() downgrade to method_exists check (#186)
1 parent a6fc6e7 commit 9abc4ab

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"tomasvotruba/class-leak": "^0.1",
2525
"tomasvotruba/cognitive-complexity": "^0.1",
2626
"tomasvotruba/type-coverage": "^0.2",
27-
"tomasvotruba/unused-public": "^0.2"
27+
"tomasvotruba/unused-public": "^0.2",
28+
"tracy/tracy": "^2.10"
2829
},
2930
"autoload": {
3031
"psr-4": {

rules-tests/DowngradePhp81/Rector/MethodCall/DowngradeIsEnumRector/Fixture/some_class.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class SomeClass
2020
{
2121
public function run(\ReflectionClass $reflectionClass)
2222
{
23-
return false;
23+
return method_exists($reflectionClass, 'isEnum') ? $reflectionClass->isEnum() : false;
2424
}
2525
}
2626

rules/DowngradePhp81/Rector/MethodCall/DowngradeIsEnumRector.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
namespace Rector\DowngradePhp81\Rector\MethodCall;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
89
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Expr\Ternary;
11+
use PhpParser\Node\Scalar\String_;
912
use PHPStan\Type\ObjectType;
1013
use Rector\Core\Rector\AbstractRector;
1114
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -36,7 +39,7 @@ class SomeClass
3639
{
3740
public function run(ReflectionClass $reflectionClass)
3841
{
39-
return false;
42+
return method_exists($reflectionClass, 'isEnum') ? $reflectionClass->isEnum() : false;
4043
}
4144
}
4245
CODE_SAMPLE
@@ -65,6 +68,12 @@ public function refactor(Node $node): ?Node
6568
return null;
6669
}
6770

68-
return $this->nodeFactory->createFalse();
71+
$args = [new Arg($node->var), new Arg(new String_('isEnum'))];
72+
73+
return new Ternary(
74+
$this->nodeFactory->createFuncCall('method_exists', $args),
75+
$node,
76+
$this->nodeFactory->createFalse()
77+
);
6978
}
7079
}

0 commit comments

Comments
 (0)