Skip to content

False positive identical.alwaysFalse on a repeated null check after a ternary when the type is PHPDoc-only and treatPhpDocTypesAsCertain is false (regression in 2.3.0) #15403

Open
phpstan/phpstan-src
#6822

Description

@zajca

Bug report

With treatPhpDocTypesAsCertain: false, PHPStan 2.3.0 reports the second $condition === null as always false. The method has no native return type, only @return Expr. The first ternary keeps null in its true branch, so $condition can still be null when the second ternary runs.

2.2.17 reports nothing here.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

namespace Repro;

class Expr
{
	/** @return string */
	public function getTitle() { return 't'; }
	/** @return string */
	public function getExpression() { return 'e'; }
}

interface Binding
{
	/** @return Expr */
	public function getCondition();
}

function sig(Binding $b): string
{
	$condition = $b->getCondition();
	return implode("\0", [
		$condition === null ? '' : (string) $condition->getTitle(),
		$condition === null ? '' : (string) $condition->getExpression(),
	]);
}

Config:

parameters:
	level: max
	treatPhpDocTypesAsCertain: false

Actual output (2.3.0)

24  Strict comparison using === between mixed and null will always evaluate to false.
    identical.alwaysFalse
    Type null has already been eliminated from mixed.

Expected output

No error on line 10, the same as in 2.2.17. The return type is PHPDoc-only, so the native type is mixed. The true branch of the first ternary is the null case, and merging the branches should bring null back.

Real-world case: Google\Service\CloudResourceManager\Binding::getCondition() from google/apiclient-services. It has @return Expr with no native type and returns null when no condition is set.

Related

The same family as #15250, #15169 and #15223, all fixed before 2.3.0: a PHPDoc type leaking into the native type when treatPhpDocTypesAsCertain is false. This path still reproduces on 2.3.0.

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

Yes. The 2.3 speed-up is very noticeable on our codebase.

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