-
-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Php80][CodeQuality] Handle crash on ChangeSwitchToMatchRector + Tern…
…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
1 parent
b71c326
commit 7e56672
Showing
5 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
tests/Issues/ChangeSwitchTernary/ChangeSwitchTernaryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
tests/Issues/ChangeSwitchTernary/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]); | ||
}; |