Skip to content

Commit

Permalink
[CodingStyle] Handle crash on empty cases on BinarySwitchToIfElseRect…
Browse files Browse the repository at this point in the history
…or (#4618)

* [CodingStyle] Handle crash on empty cases on BinarySwitchToIfElseRector

* Fixed 🎉

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jul 29, 2023
1 parent 42c4c63 commit 784894e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector\Fixture;

final class SkipEmptyCases
{
public function run()
{
switch(true) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (count($node->cases) > 2) {
if ($node->cases === [] || count($node->cases) > 2) {
return null;
}

Expand Down
3 changes: 2 additions & 1 deletion src/PhpParser/NodeFinder/PropertyFetchFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down

0 comments on commit 784894e

Please sign in to comment.