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
3 changes: 3 additions & 0 deletions src/Application/ChangedNodeScopeRefresher.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use PHPStan\Analyser\MutatingScope;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeAnalyzer\ScopeAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;

Expand Down Expand Up @@ -80,6 +81,8 @@ public function refresh(Node $node, string $filePath, ?MutatingScope $mutatingSc
// 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()) {
// the class node itself is not part of its stmts, e.g. an Enum_ replaced by a new Class_ has no scope yet
$node->setAttribute(AttributeKey::SCOPE, $mutatingScope);
$this->phpStanNodeScopeResolver->processNodes($node->stmts, $filePath, $mutatingScope);
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\EnumToClassScopeRefresh;

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

final class EnumToClassScopeRefreshTest 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,23 @@
<?php

namespace Rector\Tests\Issues\EnumToClassScopeRefresh\Fixture;

enum ScopeOnClassFromEnum
{
case Char;
case Word;
}

?>
-----
<?php

namespace Rector\Tests\Issues\EnumToClassScopeRefresh\Fixture;

class ScopeOnClassFromEnum
{
public const Char = 'char';
public const Word = 'word';
}

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;

return RectorConfig::configure()
->withRules([DowngradeEnumToConstantListClassRector::class])
->withConfiguredRule(RenameMethodRector::class, [
new MethodCallRename('SomeClass', 'oldMethod', 'newMethod'),
]);
Loading