Skip to content

Incorrect behavior of SimplifyUselessVariableRector #9179

@thePanz

Description

@thePanz

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/fc7539a2-8d8c-458a-a974-7ddcefc85e5b

<?php

class Demo {
    public function run(?string $maybe, string $part): string
    { 
       $address = $maybe;
       $address .= $address ? ' - ' . $part : $part;

       return $address;
    }
    
    function a(): void {
        echo "maybe - part";
        echo $this->run('maybe', 'part');
    }
    
    
    function b(): void {
        echo "part";
        echo $this->run(null, 'part');
    }
}

Responsible rules

  • SimplifyUselessVariableRector

Expected Behavior

Should be: https://3v4l.org/HUZJP

Adapted code by Rector which breaks the business logig: https://3v4l.org/nemWM

The . operator has precedence over ?:

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