Skip to content

Strict comparison in a loop excludes a checked value #12772

@kamil-tekiela

Description

@kamil-tekiela

Bug report

In the following code:

<?php declare(strict_types = 1);

class HelloWorld
{
	public function sayHello(DateTimeImutable $date): void
	{
		foreach ($tables as $table) {

            // If view exists, and 'add drop view' is selected: Drop it!
            if ($_POST['what'] !== 'nocopy' && isset($_POST['drop_if_exists']) && $_POST['drop_if_exists'] === 'true') {
				
            }
        }
	}
}

The check in the loop will be valid on every iteration. There is nothing to exclude the value from the possible set of values. And PHPStan reports that this will always be true.

Strict comparison using !== between mixed~'nocopy' and 'nocopy' will always evaluate to true.

Code snippet that reproduces the problem

https://phpstan.org/r/30b1edff-a8ea-4ea2-aadd-6536edd0b0e2

Expected output

There should be no warning about the always true strict comparison.

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

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions