Skip to content

Commit 9939155

Browse files
authored
Fix scope on enum on combine DowngradeEnumToConstantListClassRector + RenameMethodRector (#8727)
* Fix scope on enum on combine DowngradeEnumToConstantListClassRector + RenameMethodRector * Fix scope on enum on combine DowngradeEnumToConstantListClassRector + RenameMethodRector
1 parent 805947f commit 9939155

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎src/Application/ChangedNodeScopeRefresher.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use PHPStan\Analyser\MutatingScope;
3737
use Rector\Exception\ShouldNotHappenException;
3838
use Rector\NodeAnalyzer\ScopeAnalyzer;
39+
use Rector\NodeTypeResolver\Node\AttributeKey;
3940
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver;
4041
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
4142

@@ -80,6 +81,8 @@ public function refresh(Node $node, string $filePath, ?MutatingScope $mutatingSc
8081
// a class-like scope is already inside the class, so PHPStan cannot enter it again and would fail
8182
// on the class node, leaving its methods with the class scope; process the class stmts directly instead
8283
if ($node instanceof ClassLike && $mutatingScope->isInClass()) {
84+
// the class node itself is not part of its stmts, e.g. an Enum_ replaced by a new Class_ has no scope yet
85+
$node->setAttribute(AttributeKey::SCOPE, $mutatingScope);
8386
$this->phpStanNodeScopeResolver->processNodes($node->stmts, $filePath, $mutatingScope);
8487
return;
8588
}
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\EnumToClassScopeRefresh;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class EnumToClassScopeRefreshTest 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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\Tests\Issues\EnumToClassScopeRefresh\Fixture;
4+
5+
enum ScopeOnClassFromEnum
6+
{
7+
case Char;
8+
case Word;
9+
}
10+
11+
?>
12+
-----
13+
<?php
14+
15+
namespace Rector\Tests\Issues\EnumToClassScopeRefresh\Fixture;
16+
17+
class ScopeOnClassFromEnum
18+
{
19+
public const Char = 'char';
20+
public const Word = 'word';
21+
}
22+
23+
?>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector;
7+
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
8+
use Rector\Renaming\ValueObject\MethodCallRename;
9+
10+
return RectorConfig::configure()
11+
->withRules([DowngradeEnumToConstantListClassRector::class])
12+
->withConfiguredRule(RenameMethodRector::class, [
13+
new MethodCallRename('SomeClass', 'oldMethod', 'newMethod'),
14+
]);

0 commit comments

Comments
 (0)