Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class MultipleCall
{
public function run($object)
{
$result = ($nullsafeVariable1 = ($nullsafeVariable3 = $object->multiple($args1)) ? $nullsafeVariable3->call($args2) : null) ? $nullsafeVariable1->otherCall($args3) : null;
$result = ($nullsafeVariable2 = ($nullsafeVariable4 = ($nullsafeVariable5 = $object->multiple($args1)) ? $nullsafeVariable5->call($args2) : null) ? $nullsafeVariable4->otherCall($args3) : null) ? $nullsafeVariable2->anotherCall($args4) : null;
$result = ($nullsafeVariable1 = ($nullsafeVariable2 = $object->multiple($args1)) ? $nullsafeVariable2->call($args2) : null) ? $nullsafeVariable1->otherCall($args3) : null;
$result = ($nullsafeVariable3 = ($nullsafeVariable4 = ($nullsafeVariable5 = $object->multiple($args1)) ? $nullsafeVariable5->call($args2) : null) ? $nullsafeVariable4->otherCall($args3) : null) ? $nullsafeVariable3->anotherCall($args4) : null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -43,13 +44,19 @@ public function getRuleDefinition(): RuleDefinition
*/
public function getNodeTypes(): array
{
return [MethodCall::class, PropertyFetch::class, NullsafeMethodCall::class, NullsafePropertyFetch::class];
return [
Expression::class,
MethodCall::class,
PropertyFetch::class,
NullsafeMethodCall::class,
NullsafePropertyFetch::class,
];
}

/**
* @param MethodCall|NullsafeMethodCall|NullsafePropertyFetch $node
* @param Expression|MethodCall|NullsafeMethodCall|NullsafePropertyFetch $node
*/
public function refactor(Node $node): ?Ternary
public function refactor(Node $node): null|Ternary|Expression
{
static $currentFile = null;

Expand All @@ -61,6 +68,18 @@ public function refactor(Node $node): ?Ternary
$currentFile = $this->file->getFilePath();
}

if ($node instanceof Expression) {
if ($node->expr instanceof Assign && ($node->expr->expr instanceof NullsafeMethodCall || $node->expr->expr instanceof NullsafePropertyFetch)) {
$refactorAssignExpr = $this->refactor($node->expr->expr);
if ($refactorAssignExpr instanceof Ternary) {
$node->expr->expr = $refactorAssignExpr;
return $node;
}
}

return null;
}

if ($node instanceof MethodCall || $node instanceof PropertyFetch) {
if ($node->var instanceof NullsafeMethodCall || $node->var instanceof NullsafePropertyFetch) {
$nullsafeVariable = $this->createNullsafeVariable();
Expand Down
28 changes: 28 additions & 0 deletions tests/Issues/DowngradeThrowNullsafe/DowngradeThrowNullsafeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\DowngradeThrowNullsafe;

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

final class DowngradeThrowNullsafeTest 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';
}
}
31 changes: 31 additions & 0 deletions tests/Issues/DowngradeThrowNullsafe/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Issues\DowngradeThrowNullsafe\Fixture;

class Fixture
{
public function run($s)
{
$s = \Transliterator::create('Any-Latin; Latin-ASCII')?->transliterate($s)
?? throw new Nette\InvalidStateException('Transliterator::transliterate() failed.');
}
}

?>
-----
<?php

namespace Rector\Tests\Issues\DowngradeThrowNullsafe\Fixture;

class Fixture
{
public function run($s)
{
if ((($nullsafeVariable1 = \Transliterator::create('Any-Latin; Latin-ASCII')) ? $nullsafeVariable1->transliterate($s) : null) === null) {
throw new Nette\InvalidStateException('Transliterator::transliterate() failed.');
}
$s = ($nullsafeVariable2 = \Transliterator::create('Any-Latin; Latin-ASCII')) ? $nullsafeVariable2->transliterate($s) : null;
}
}

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector;
use Rector\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector;

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