Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Application/ChangedNodeScopeRefresher.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PhpParser\Node\StaticVar;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\ElseIf_;
Expand Down Expand Up @@ -76,6 +77,13 @@ public function refresh(Node $node, string $filePath, ?MutatingScope $mutatingSc
fn (Node $subNode): ?Node => NodeAttributeReIndexer::reIndexNodeAttributes($subNode)
);

// a class-like scope is already inside the class, so PHPStan cannot enter it again and would fail
// on the class node, leaving its methods with the class scope; process the class stmts directly instead
if ($node instanceof ClassLike && $mutatingScope->isInClass()) {
$this->phpStanNodeScopeResolver->processNodes($node->stmts, $filePath, $mutatingScope);
return;
}

$stmts = $this->resolveStmts($node);
$this->phpStanNodeScopeResolver->processNodes($stmts, $filePath, $mutatingScope);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Issues/ClassScopeRefresh/ClassScopeRefreshTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\ClassScopeRefresh;

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

final class ClassScopeRefreshTest 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';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Rector\Tests\Issues\ClassScopeRefresh\Fixture;

final class KeepMethodScopeAfterClassChange
{
public function __construct(
private readonly int $value
) {
}

public function run(array $items): array
{
$result = [];
foreach ($items as $item) {
$result = [...$result, ...$items];
}

return $result;
}
}

?>
-----
<?php

namespace Rector\Tests\Issues\ClassScopeRefresh\Fixture;

final class KeepMethodScopeAfterClassChange
{
/**
* @readonly
*/
private int $value;
public function __construct(int $value)
{
$this->value = $value;
}

public function run(array $items): array
{
$result = [];
foreach ($items as $item) {
$result = array_merge($result, $items);
}

return $result;
}
}

?>
15 changes: 15 additions & 0 deletions tests/Issues/ClassScopeRefresh/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector;
use Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector;
use Rector\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector;

return RectorConfig::configure()
->withRules([
DowngradePropertyPromotionRector::class,
DowngradeReadonlyPropertyRector::class,
DowngradeArraySpreadRector::class,
]);
Loading