Skip to content

Incorrect behavior of ConsecutiveNullCompareReturnsToNullCoalesceQueueRector #7976

@jlherren

Description

@jlherren

Bug Report

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

Minimal PHP Code Causing Issue

See https://getrector.com/demo/8420e8f2-769b-453b-b2cb-b4289a8cb15c

<?php

class MyClass {
    protected ?stdClass $prop1 = null;
    protected ?stdClass $prop2 = null;

    public function getProp(): stdClass {
        if ($this->prop1 !== null) {
            return $this->prop1;
        }
        if ($this->prop2 !== null) {
            return $this->prop2;
        }
        throw new RuntimeException('No prop is set');
    }
}

Responsible rules

  • ConsecutiveNullCompareReturnsToNullCoalesceQueueRector

Expected Behavior

Desired behavior is either to do nothing at all, or transform to something like the following (throw expression requires PHP 8):

    public function getProp(): stdClass {
        return $this->prop1 ?? $this->prop2 ?? throw new RuntimeException('No prop is set');
    }

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