Skip to content

Commit 32fa86e

Browse files
committed
Fix combine DowngradeThrowExprRector + DowngradeNullsafeToTernaryOperatorRector
1 parent db13b8f commit 32fa86e

4 files changed

Lines changed: 89 additions & 3 deletions

File tree

rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpParser\Node\Expr\PropertyFetch;
1313
use PhpParser\Node\Expr\Ternary;
1414
use PhpParser\Node\Expr\Variable;
15+
use PhpParser\Node\Stmt\Expression;
1516
use Rector\Rector\AbstractRector;
1617
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1718
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -43,13 +44,19 @@ public function getRuleDefinition(): RuleDefinition
4344
*/
4445
public function getNodeTypes(): array
4546
{
46-
return [MethodCall::class, PropertyFetch::class, NullsafeMethodCall::class, NullsafePropertyFetch::class];
47+
return [
48+
Expression::class,
49+
MethodCall::class,
50+
PropertyFetch::class,
51+
NullsafeMethodCall::class,
52+
NullsafePropertyFetch::class,
53+
];
4754
}
4855

4956
/**
50-
* @param MethodCall|NullsafeMethodCall|NullsafePropertyFetch $node
57+
* @param Expression|MethodCall|NullsafeMethodCall|NullsafePropertyFetch $node
5158
*/
52-
public function refactor(Node $node): ?Ternary
59+
public function refactor(Node $node): null|Ternary|Expression
5360
{
5461
static $currentFile = null;
5562

@@ -61,6 +68,15 @@ public function refactor(Node $node): ?Ternary
6168
$currentFile = $this->file->getFilePath();
6269
}
6370

71+
if ($node instanceof Expression) {
72+
if ($node->expr instanceof Assign && ($node->expr->expr instanceof NullsafeMethodCall || $node->expr->expr instanceof NullsafePropertyFetch)) {
73+
$node->expr = new Assign($node->expr->var, $this->refactor($node->expr->expr));
74+
return $node;
75+
}
76+
77+
return null;
78+
}
79+
6480
if ($node instanceof MethodCall || $node instanceof PropertyFetch) {
6581
if ($node->var instanceof NullsafeMethodCall || $node->var instanceof NullsafePropertyFetch) {
6682
$nullsafeVariable = $this->createNullsafeVariable();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\DowngradeThrowNullsafe;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradeThrowNullsafeTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\Issues\DowngradeThrowNullsafe\Fixture;
4+
5+
class Fixture
6+
{
7+
public function run($s)
8+
{
9+
$s = \Transliterator::create('Any-Latin; Latin-ASCII')?->transliterate($s)
10+
?? throw new Nette\InvalidStateException('Transliterator::transliterate() failed.');
11+
}
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Rector\Tests\Issues\DowngradeThrowNullsafe\Fixture;
19+
20+
class Fixture
21+
{
22+
public function run($s)
23+
{
24+
if ((($nullsafeVariable1 = \Transliterator::create('Any-Latin; Latin-ASCII')) ? $nullsafeVariable1->transliterate($s) : null) === null) {
25+
throw new Nette\InvalidStateException('Transliterator::transliterate() failed.');
26+
}
27+
$s = ($nullsafeVariable2 = \Transliterator::create('Any-Latin; Latin-ASCII')) ? $nullsafeVariable2->transliterate($s) : null;
28+
}
29+
}
30+
31+
?>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector;
7+
use Rector\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->rules([DowngradeThrowExprRector::class, DowngradeNullsafeToTernaryOperatorRector::class]);
11+
};

0 commit comments

Comments
 (0)