Skip to content

Incorrect behavior of ExplicitBoolCompareRector #8973

@jderusse

Description

@jderusse

Bug Report

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

Minimal PHP Code Causing Issue

See https://getrector.com/demo/fec3769b-6fbc-4e6e-a8ea-958bdea5ea9a

<?php

function getExpiredAt(): \Datetime|null
{
    return null;
}

if ($expired = getExpiredAt()) {
    echo $expired->format('Ymd');
}

Responsible rules

  • ExplicitBoolCompareRector

Expected Behavior

The rector converts $expired = getExpiredAt() into $expired = getExpiredAt() !== null.

The issue is, that the precedence is not the same: $expired = getExpiredAt() !== null is evaluated as $expired = (getExpiredAt() !== null) instead of (expected) ($expired = getExpiredAt()) !== null

see https://3v4l.org/ugYkh for precedence

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