From 784894e8ed4260ae3c8d45a28e007f4b6fe98f4d Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 29 Jul 2023 16:40:35 +0700 Subject: [PATCH] [CodingStyle] Handle crash on empty cases on BinarySwitchToIfElseRector (#4618) * [CodingStyle] Handle crash on empty cases on BinarySwitchToIfElseRector * Fixed :tada: * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action --- .../Fixture/skip_empty_cases.php.inc | 12 ++++++++++++ .../Rector/Switch_/BinarySwitchToIfElseRector.php | 2 +- src/PhpParser/NodeFinder/PropertyFetchFinder.php | 3 ++- 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 rules-tests/CodingStyle/Rector/Switch_/BinarySwitchToIfElseRector/Fixture/skip_empty_cases.php.inc diff --git a/rules-tests/CodingStyle/Rector/Switch_/BinarySwitchToIfElseRector/Fixture/skip_empty_cases.php.inc b/rules-tests/CodingStyle/Rector/Switch_/BinarySwitchToIfElseRector/Fixture/skip_empty_cases.php.inc new file mode 100644 index 00000000000..bf76640afac --- /dev/null +++ b/rules-tests/CodingStyle/Rector/Switch_/BinarySwitchToIfElseRector/Fixture/skip_empty_cases.php.inc @@ -0,0 +1,12 @@ +cases) > 2) { + if ($node->cases === [] || count($node->cases) > 2) { return null; } diff --git a/src/PhpParser/NodeFinder/PropertyFetchFinder.php b/src/PhpParser/NodeFinder/PropertyFetchFinder.php index df68d3f15d6..7edc1f7a9c9 100644 --- a/src/PhpParser/NodeFinder/PropertyFetchFinder.php +++ b/src/PhpParser/NodeFinder/PropertyFetchFinder.php @@ -4,6 +4,7 @@ namespace Rector\Core\PhpParser\NodeFinder; +use PHPStan\Type\StaticType; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; @@ -141,7 +142,7 @@ public function isLocalPropertyFetchByName(Expr $expr, Class_|Trait_ $class, str } $type = $this->nodeTypeResolver->getType($expr->var); - if ($type instanceof \PHPStan\Type\StaticType || $type instanceof FullyQualifiedObjectType) { + if ($type instanceof StaticType || $type instanceof FullyQualifiedObjectType) { return $this->nodeNameResolver->isName($class, $type->getClassName()); }