Skip to content

[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
merged 1 commit into from
Aug 11, 2025
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
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;

?>
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';
}
}
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),
]);
};
79 changes: 79 additions & 0 deletions rules/Renaming/Rector/Cast/RenameCastRector.php
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;
}
}
40 changes: 40 additions & 0 deletions rules/Renaming/ValueObject/RenameCast.php
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> */
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
}
Loading