Skip to content

Commit b595739

Browse files
authored
Fix class scope refresh losing method scope when class node is already in class (#8725)
1 parent 3b02457 commit b595739

4 files changed

Lines changed: 102 additions & 0 deletions

File tree

‎src/Application/ChangedNodeScopeRefresher.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PhpParser\Node\StaticVar;
2424
use PhpParser\Node\Stmt;
2525
use PhpParser\Node\Stmt\Class_;
26+
use PhpParser\Node\Stmt\ClassLike;
2627
use PhpParser\Node\Stmt\Declare_;
2728
use PhpParser\Node\Stmt\Else_;
2829
use PhpParser\Node\Stmt\ElseIf_;
@@ -76,6 +77,13 @@ public function refresh(Node $node, string $filePath, ?MutatingScope $mutatingSc
7677
fn (Node $subNode): ?Node => NodeAttributeReIndexer::reIndexNodeAttributes($subNode)
7778
);
7879

80+
// a class-like scope is already inside the class, so PHPStan cannot enter it again and would fail
81+
// on the class node, leaving its methods with the class scope; process the class stmts directly instead
82+
if ($node instanceof ClassLike && $mutatingScope->isInClass()) {
83+
$this->phpStanNodeScopeResolver->processNodes($node->stmts, $filePath, $mutatingScope);
84+
return;
85+
}
86+
7987
$stmts = $this->resolveStmts($node);
8088
$this->phpStanNodeScopeResolver->processNodes($stmts, $filePath, $mutatingScope);
8189
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\ClassScopeRefresh;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class ClassScopeRefreshTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Rector\Tests\Issues\ClassScopeRefresh\Fixture;
4+
5+
final class KeepMethodScopeAfterClassChange
6+
{
7+
public function __construct(
8+
private readonly int $value
9+
) {
10+
}
11+
12+
public function run(array $items): array
13+
{
14+
$result = [];
15+
foreach ($items as $item) {
16+
$result = [...$result, ...$items];
17+
}
18+
19+
return $result;
20+
}
21+
}
22+
23+
?>
24+
-----
25+
<?php
26+
27+
namespace Rector\Tests\Issues\ClassScopeRefresh\Fixture;
28+
29+
final class KeepMethodScopeAfterClassChange
30+
{
31+
/**
32+
* @readonly
33+
*/
34+
private int $value;
35+
public function __construct(int $value)
36+
{
37+
$this->value = $value;
38+
}
39+
40+
public function run(array $items): array
41+
{
42+
$result = [];
43+
foreach ($items as $item) {
44+
$result = array_merge($result, $items);
45+
}
46+
47+
return $result;
48+
}
49+
}
50+
51+
?>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector;
7+
use Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector;
8+
use Rector\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector;
9+
10+
return RectorConfig::configure()
11+
->withRules([
12+
DowngradePropertyPromotionRector::class,
13+
DowngradeReadonlyPropertyRector::class,
14+
DowngradeArraySpreadRector::class,
15+
]);

0 commit comments

Comments
 (0)