Skip to content

Commit

Permalink
[Php80][CodeQuality] Handle crash on ChangeSwitchToMatchRector + Tern…
Browse files Browse the repository at this point in the history
…aryFalseExpressionToIfRector on under if else (#5446)

* [Php80][CodeQuality] Handle crash on ChangeSwitchToMatchRector + TernaryFalseExpressionToIfRector on under if else

* [ci-review] Rector Rectify

* rules()

* fix

* remove debug

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jan 8, 2024
1 parent b71c326 commit 7e56672
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\NodeTypeResolver;

use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
Expand Down Expand Up @@ -301,7 +302,7 @@ public function getFullyQualifiedClassName(TypeWithClassName $typeWithClassName)

public function isMethodStaticCallOrClassMethodObjectType(Node $node, ObjectType $objectType): bool
{
if ($node instanceof MethodCall || $node instanceof Expr\NullsafeMethodCall) {
if ($node instanceof MethodCall || $node instanceof NullsafeMethodCall) {
// method call is variable return
return $this->isObjectType($node->var, $objectType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeAnalyzer\ClassAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
Expand Down Expand Up @@ -126,6 +127,12 @@ public function processNodes(
return;
}

if ($node instanceof StmtsAwareInterface && $node->stmts !== null) {
foreach ($node->stmts as $stmt) {
$stmt->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
}

if ((
$node instanceof Expression ||
$node instanceof Return_ ||
Expand Down
28 changes: 28 additions & 0 deletions tests/Issues/ChangeSwitchTernary/ChangeSwitchTernaryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\ChangeSwitchTernary;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ChangeSwitchTernaryTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
56 changes: 56 additions & 0 deletions tests/Issues/ChangeSwitchTernary/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\ChangeSwitchTernary\Fixture;

final class Fixture
{
public function run()
{
$random = rand(1, 3);

if(rand(0, 1)) {
// this if makes it crash
} else {
switch($random) {
case 1:
$out = 'one';
break;
case 2:
$out = 'two';
break;
default:
$out = 'other';
}
}
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\ChangeSwitchTernary\Fixture;

final class Fixture
{
public function run()
{
$random = rand(1, 3);

if(rand(0, 1)) {
// this if makes it crash
} else {
$out = match ($random) {
1 => 'one',
2 => 'two',
default => 'other',
};
}
}
}

?>
14 changes: 14 additions & 0 deletions tests/Issues/ChangeSwitchTernary/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector;
use Rector\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
ChangeSwitchToMatchRector::class,
TernaryFalseExpressionToIfRector::class
]);
};

0 comments on commit 7e56672

Please sign in to comment.