Skip to content

[RenameCastRector] removes necessary parentheses when renaming casts. #9313

@mickverm

Description

@mickverm

Bug Report

Subject Details
Rector version v2.1.4

When using RenameCastRector, parentheses around expressions are removed incorrectly, changing the meaning of the code.

Minimal PHP Code Causing Issue

- $val = (integer) ($i * $i);
+ $val = (int) $i * $i; // incorrect: parentheses removed

Expected Behaviour

- $val = (integer) ($i * $i);
+ $val = (int) ($i * $i); // correct: parentheses preserved

I tried creating a reproduction link with the following configs, but keep getting greeted with the Expected config should return callable RectorConfig instance warning:

Reproduction
final class DemoFile
{
    public function run(float $i): int
    {
        return (integer) ($i * $i);
    }
}
<?php

use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\Cast\Double;
use PhpParser\Node\Expr\Cast\Int_;
use Rector\Config\RectorConfig;
use Rector\ValueObject\PhpVersion;
use Rector\Renaming\Rector\Cast\RenameCastRector;
use Rector\Renaming\ValueObject\RenameCast;

return RectorConfig::configure()
    ->withConfiguredRule(
        RenameCastRector::class,
        [
            new RenameCast(Int_::class, Int_::KIND_INTEGER, Int_::KIND_INT),
            new RenameCast(Bool_::class, Bool_::KIND_BOOLEAN, Bool_::KIND_BOOL),
            new RenameCast(Double::class, Double::KIND_DOUBLE, Double::KIND_FLOAT),
        ],
    )
    ->withPhpVersion(PhpVersion::PHP_84);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions