-
-
Notifications
You must be signed in to change notification settings - Fork 407
[Renaming] Add RenameCastRector
#7117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TomasVotruba
merged 1 commit into
rectorphp:main
from
mttsch:feature/rename-cast-rector
Aug 11, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
rules-tests/Renaming/Rector/Cast/RenameCastRector/Fixture/fixture.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Renaming\Rector\Cast\RenameCastRector\Fixture; | ||
|
||
$real = (real) $real; | ||
$double = (double) $double; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Renaming\Rector\Cast\RenameCastRector\Fixture; | ||
|
||
$real = (float) $real; | ||
$double = (float) $double; | ||
|
||
?> |
26 changes: 26 additions & 0 deletions
26
rules-tests/Renaming/Rector/Cast/RenameCastRector/RenameCastRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Renaming\Rector\Cast\RenameCastRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
class RenameCastRectorTest 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'; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
rules-tests/Renaming/Rector/Cast/RenameCastRector/config/configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PhpParser\Node\Expr\Cast\Double; | ||
use Rector\Config\RectorConfig; | ||
use Rector\Renaming\Rector\Cast\RenameCastRector; | ||
use Rector\Renaming\ValueObject\RenameCast; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig | ||
->ruleWithConfiguration(RenameCastRector::class, [ | ||
new RenameCast(Double::class, Double::KIND_REAL, Double::KIND_FLOAT), | ||
new RenameCast(Double::class, Double::KIND_DOUBLE, Double::KIND_FLOAT), | ||
]); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Renaming\Rector\Cast; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Cast; | ||
use PhpParser\Node\Expr\Cast\Double; | ||
use Rector\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\Renaming\ValueObject\RenameCast; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @see \Rector\Tests\Renaming\Rector\Cast\RenameCastRector\RenameCastRectorTest | ||
*/ | ||
final class RenameCastRector extends AbstractRector implements ConfigurableRectorInterface | ||
{ | ||
/** | ||
* @var array<RenameCast> | ||
*/ | ||
private array $renameCasts = []; | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Renames casts', [ | ||
new ConfiguredCodeSample( | ||
'$real = (real) $real;', | ||
'$real = (float) $real;', | ||
[new RenameCast(Double::class, Double::KIND_REAL, Double::KIND_FLOAT)] | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Cast::class]; | ||
} | ||
|
||
/** | ||
* @param Cast $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
foreach ($this->renameCasts as $renameCast) { | ||
$expectedClassName = $renameCast->getFromCastExprClass(); | ||
if ( | ||
! $node instanceof $expectedClassName | ||
|| $node->getAttribute(AttributeKey::KIND) !== $renameCast->getFromCastKind() | ||
) { | ||
continue; | ||
} | ||
|
||
$node->setAttribute(AttributeKey::KIND, $renameCast->getToCastKind()); | ||
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null); | ||
|
||
return $node; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param mixed[] $configuration | ||
*/ | ||
public function configure(array $configuration): void | ||
{ | ||
Assert::allIsInstanceOf($configuration, RenameCast::class); | ||
|
||
$this->renameCasts = $configuration; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Renaming\ValueObject; | ||
|
||
use PhpParser\Node\Expr\Cast; | ||
use Rector\Validation\RectorAssert; | ||
use Webmozart\Assert\Assert; | ||
|
||
final readonly class RenameCast | ||
{ | ||
public function __construct( | ||
/** @var class-string<Cast> */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering whether this is okay, to directly expose PHP parser class names in value objects. This avoids having to add a mapping layer between the value passed here and the cast class. |
||
private string $fromCastExprClass, | ||
private int $fromCastKind, | ||
private int $toCastKind, | ||
) { | ||
RectorAssert::className($fromCastExprClass); | ||
Assert::subclassOf($fromCastExprClass, Cast::class); | ||
} | ||
|
||
/** | ||
* @return class-string<Cast> | ||
*/ | ||
public function getFromCastExprClass(): string | ||
{ | ||
return $this->fromCastExprClass; | ||
} | ||
|
||
public function getFromCastKind(): int | ||
{ | ||
return $this->fromCastKind; | ||
} | ||
|
||
public function getToCastKind(): int | ||
{ | ||
return $this->toCastKind; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.