Skip to content

False positive when object === null is excluded via XOR #3385

@artjomsimon

Description

@artjomsimon

Bug report

Eliminating the possibility for a null reference and returning early in that case, PHPStan still warns about a possible null reference:

Introducing another (redundant) null check for $greeterA makes PHPStan think that $greeterB can be null:

The xor in the code can also be refactored as OR and the error still shows, so it's the conceptual xor that's causing problems, not the literal `xor:

Code snippet that reproduces the problem

<?php declare(strict_types=1);

class Greeter {
	public function sayHello() : string {
    	return 'hello';
    }
    
    public function isEqualTo(Greeter $otherGreeter) : bool {
    	return $this->sayHello() === $otherGreeter->sayHello();
    }
}

function isGreeterDifferent(?Greeter  $greeterA, ?Greeter $greeterB) : bool {
 
    if ($greeterA === null && $greeterB !== null) {
        return true;
    }
    
    if ($greeterA !== null && $greeterB === null) {
        return true;
    }
    
    if ($greeterA === null && $greeterB === null) {
        return false;
    }

    return $greeterA->isEqualTo($greeterB);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions