Skip to content

Commit

Permalink
[Php56][Php70] Handle infinite loop on TernaryToNullCoalescingRector+…
Browse files Browse the repository at this point in the history
…AddDefaultValueForUndefinedVariableRector (#3639)

* reproduce hang

* Closes rectorphp/rector#7889

* clean up

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Apr 20, 2023
1 parent fff2232 commit 8425c90
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,13 @@ private function isObjectTypeOfObjectType(ObjectType $resolvedObjectType, Object
if (! $this->reflectionProvider->hasClass($requiredClassName)) {
return false;
}

$requiredClassReflection = $this->reflectionProvider->getClass($requiredClassName);

if (! $this->reflectionProvider->hasClass($resolvedClassName)) {
return false;
}

$resolvedClassReflection = $this->reflectionProvider->getClass($resolvedClassName);

if ($requiredClassReflection->isTrait()) {
Expand Down
8 changes: 4 additions & 4 deletions rules/Php56/NodeAnalyzer/UndefinedVariableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,19 @@ private function isAssign(Node $parentNode): bool

private function shouldSkipVariable(Variable $variable, Node $parentNode): bool
{
if ($this->variableAnalyzer->isStaticOrGlobal($variable)) {
if ($this->isAsCoalesceLeftOrAssignOpCoalesceVar($parentNode, $variable)) {
return true;
}

if ($this->isAssign($parentNode)) {
if ($this->variableAnalyzer->isStaticOrGlobal($variable)) {
return true;
}

if ($this->issetOrUnsetOrEmptyParent($parentNode)) {
if ($this->isAssign($parentNode)) {
return true;
}

if ($this->isAsCoalesceLeftOrAssignOpCoalesceVar($parentNode, $variable)) {
if ($this->issetOrUnsetOrEmptyParent($parentNode)) {
return true;
}

Expand Down
29 changes: 29 additions & 0 deletions tests/Issues/TernaryCoalesceDefault/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

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

class Fixture
{
public function run()
{
$http = env('HTTP_X');
return ($http === null) ? false : $http;
}
}

?>
-----
<?php

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

class Fixture
{
public function run()
{
$http = env('HTTP_X');
return $http ?? false;
}
}

?>
28 changes: 28 additions & 0 deletions tests/Issues/TernaryCoalesceDefault/TernaryCoalesceDefaultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\TernaryCoalesceDefault;

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

final class TernaryCoalesceDefaultTest 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';
}
}
13 changes: 13 additions & 0 deletions tests/Issues/TernaryCoalesceDefault/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector;
use Rector\Php70\Rector\Ternary\TernaryToNullCoalescingRector;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
TernaryToNullCoalescingRector::class,
AddDefaultValueForUndefinedVariableRector::class,
]);
};

0 comments on commit 8425c90

Please sign in to comment.