← 输入框|github.com|刷新
Skip to content

False positive "Coalesce operator ?? is unnecessary" when the last method in the chain has return type null #15134

Closed

Description

@LastDragon-ru

Bug report

I've updated from 2.2.5 to the latest (2.2.9/2.2.10) and noticed that phpstan now complains when null is the last type in the ?? chain. It is fine for variables, but not fine if the last item is the method with null type. For example, the parseSpace() method cannot be deleted because it may perform some work.

abstract class Parser {
    protected function parseExpressionChild(bool $value): ?Node {
        return $this->parseNumber($value) // Coalesce operator ?? is unnecessary because the left side is always set and the right side is null.

            ?? $this->parseSpace($value);
    }

    abstract protected function parseNumber(bool $value): ?A;

    protected function parseSpace(bool $value): null {
        if ($value === false) {
            throw new LogicException('Value is not valid.');
        }

        return null;
    }
}

Code snippet that reproduces the problem

https://phpstan.org/r/9a6c2952-a66e-4961-b746-113f5d2a2909

Expected output

No error.

Did PHPStan help you today? Did it make you happy in any way?

No response

Activity

  1. added this to the Easy fixes milestone on Sep 11, 2026
  2. phpstan-bot commented on Sep 12, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#6418

  3. github-actions commented on Oct 14, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  4. locked as resolved and limited conversation to collaborators on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions