Skip to content

Commit

Permalink
[Php72][TypeDeclaration] Fix infinite loop on ParseStrWithResultArgum…
Browse files Browse the repository at this point in the history
…entRector+DeclareStrictTypesRector (#5699)

* [Php72][TypeDeclaration] Fix infinite loop on ParseStrWithResultArgumentRector+DeclareStrictTypesRector

* [Php72][TypeDeclaration] Fix infinite loop on ParseStrWithResultArgumentRector+DeclareStrictTypesRector

* fix

* fix

* fix

* dont use current()

* fix phpstan

* rename class

* rename class
  • Loading branch information
samsonasik committed Mar 7, 2024
1 parent 6225460 commit 13bafcb
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ public function beforeTraverse(array $nodes): ?array
return null;
}

$rootStmt = current($newStmts);
// use 0 index to avoid infinite loop
$rootStmt = $newStmts[0] ?? null;
$stmt = $rootStmt;

if ($rootStmt instanceof FileWithoutNamespace) {
$currentStmt = current($rootStmt->stmts);
// use 0 index to avoid infinite loop
$currentStmt = $rootStmt->stmts[0] ?? null;

if (! $currentStmt instanceof Stmt) {
return null;
Expand All @@ -78,6 +80,10 @@ public function beforeTraverse(array $nodes): ?array
$stmt = $currentStmt;
}

if (! $stmt instanceof Stmt) {
return null;
}

if ($this->shouldSkip($stmt)) {
return null;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Issues/InfiniteLoop/DeclareStrictTypesParseStrTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\InfiniteLoop;

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

final class DeclareStrictTypesParseStrTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/FixtureDeclareStrictTypesParseStr');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/declare_strict_types_parsestr.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Foo;

function foo()
{
return 'foo';
}

echo foo();

?>
-----
<?php

declare(strict_types=1);

namespace Foo;

function foo()
{
return 'foo';
}

echo foo();
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

function foo()
{
return 'foo';
}

echo foo();

?>
-----
<?php

declare(strict_types=1);

function foo()
{
return 'foo';
}

echo foo();

?>
13 changes: 13 additions & 0 deletions tests/Issues/InfiniteLoop/config/declare_strict_types_parsestr.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\Php72\Rector\FuncCall\ParseStrWithResultArgumentRector;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;

return RectorConfig::configure()
->withRules([
DeclareStrictTypesRector::class,
ParseStrWithResultArgumentRector::class,
]);

0 comments on commit 13bafcb

Please sign in to comment.