Skip to content

Commit 1cd55d4

Browse files
committed
[DeadCode] Handle Infinite recursion on self referenced trait on PHPStanNodeScopeResolver
1 parent 014b976 commit 1cd55d4

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector\Fixture;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
use Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector\Source\RecursiveTrait;
9+
10+
/**
11+
* @method static Builder<static>|BaseModel query()
12+
*/
13+
class BaseModel extends Model {
14+
use RecursiveTrait;
15+
16+
public function parent(): BelongsTo {
17+
return $this->belongsTo(self::class);
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector\Source;
6+
7+
trait RecursiveTrait {
8+
public function getRecursive(): object {
9+
return new class () {
10+
use RecursiveTrait;
11+
};
12+
}
13+
}

src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,14 @@ private function nodeScopeResolverProcessNodes(
477477
// in the middle of process
478478
// fallback to fill by found scope
479479
RectorNodeScopeResolver::processNodes($stmts, $mutatingScope);
480+
} catch (Error $error) {
481+
if (! str_contains($error->getMessage(), 'Infinite recursion?')) {
482+
throw $error;
483+
}
484+
485+
// handle stack overflow from recursive structures (e.g. self-referencing traits)
486+
// fallback to fill by found scope
487+
RectorNodeScopeResolver::processNodes($stmts, $mutatingScope);
480488
}
481489
}
482490

0 commit comments

Comments
 (0)