Skip to content

Commit

Permalink
[Php80] Skip ObjectType on ChangeSwitchToMatchRector (#5719)
Browse files Browse the repository at this point in the history
* [Php80] Skip ObjectType on ChangeSwitchToMatchRector

* [Php80] Skip ObjectType on ChangeSwitchToMatchRector
  • Loading branch information
samsonasik committed Mar 14, 2024
1 parent d01d16e commit fbba860
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture;

use Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Source\SomeResponse;

class SkipObjectType {
public function doTheAction(SomeResponse $example): string
{
switch ($example) {
case $example::build('a', 'b'):
return 'here';
break;
default:
return 'there';
}
}
}
Expand Up @@ -9,4 +9,9 @@ final class SomeResponse
public function __construct($content, $statusCode)
{
}

public static function build($a, $b): self
{
return new self($a, $b);
}
}
5 changes: 5 additions & 0 deletions rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use PHPStan\Type\ObjectType;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Php80\NodeAnalyzer\MatchSwitchAnalyzer;
use Rector\Php80\NodeFactory\MatchFactory;
Expand Down Expand Up @@ -102,6 +103,10 @@ public function refactor(Node $node): ?Node

$isReturn = $this->matchSwitchAnalyzer->isReturnCondsAndExprs($condAndExprs);

if ($this->nodeTypeResolver->getType($stmt->cond) instanceof ObjectType) {
continue;
}

$matchResult = $this->matchFactory->createFromCondAndExprs($stmt->cond, $condAndExprs, $nextStmt);
if (! $matchResult instanceof MatchResult) {
continue;
Expand Down

0 comments on commit fbba860

Please sign in to comment.