Skip to content

Commit

Permalink
[Php56][Php70][Php74] Handle infinite loop on AddDefaultValueForUndef…
Browse files Browse the repository at this point in the history
…inedVariableRector+IfToSpaceshipRector+ClosureToArrowFunctionRector (#3701)

* [Php56][Php70][Php74] Handle infinite loop on AddDefaultValueForUndefinedVariableRector+IfToSpaceshipRector+ClosureToArrowFunctionRector

* fixtue

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Apr 28, 2023
1 parent b3e2696 commit 31f64cc
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
6 changes: 3 additions & 3 deletions rules/Php56/NodeAnalyzer/UndefinedVariableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public function resolve(ClassMethod | Function_ | Closure $node): array
}

/**
* The Stmt that doesn't have origNode attribute yet
* means the Stmt is a replacement below other changed node
* The Node that doesn't have origNode attribute yet
* means the Node is a replacement below other changed node
*/
if ($node instanceof Stmt && ! $node->hasAttribute(AttributeKey::ORIGINAL_NODE)) {
if (! $node->hasAttribute(AttributeKey::ORIGINAL_NODE)) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private function processFromPropertyFetch(PropertyFetch $propertyFetch): ?Proper
if (! $nodeVarType instanceof Type) {
$nodeVarType = $this->nodeTypeResolver->getType($propertyFetch->var);
}

if ($nodeVarType instanceof ThisType ) {
$this->renameProperty($class, $renamedProperty);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\DefaultValueSpaceShipClosure;

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

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

namespace Rector\Core\Tests\Issues\DefaultValueSpaceShipClosure\Fixture;

class Fixture
{
public function run()
{
uasort([], function ($a, $b) {
if ($a == $b) {
return 0;
}

return ($a > $b) ? -1 : 1;
});
}
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\DefaultValueSpaceShipClosure\Fixture;

class Fixture
{
public function run()
{
uasort([], fn($a, $b) => $b <=> $a);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector;
use Rector\Php70\Rector\If_\IfToSpaceshipRector;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;

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

0 comments on commit 31f64cc

Please sign in to comment.