Skip to content

RemoveAlwaysTrueIfConditionRector removes conditions when comparing method call and constant #8700

@nikolicaleksa

Description

@nikolicaleksa

Bug Report

Subject Details
Rector version last dev-main

Minimal PHP Code Causing Issue

See https://getrector.com/demo/2b19a5fb-5e83-4b97-9968-a7cda22b54f3

<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\Fixture;

class ApiException extends \Exception
{
    public function __construct(
        string $message,
        ?string $code = null,
        ?\Throwable $previous = null
    ) {
        parent::__construct($message, 0, $previous);

        $this->code = $code;
    }
}

class SkipMethodCall {
    private const WEBSITE_ALREADY_EXISTS = 'website_already_exists';

    public function run()
    {
        try {
            return $this->someApiAction();
        } catch (ApiException $ex) {
            if ($ex->getCode() !== self::WEBSITE_ALREADY_EXISTS) {
                throw $ex;
            }
        }
    }

    private function someApiAction()
    {
        if (rand() % 2) {
            throw new ApiException('API Action failed', 'website_already_exists');
        }

        return 1;
    }
}

Expected Behaviour

Rector should not remove the condition as we cannot know what method call will return

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