Skip to content

Invalid error shown for argument type checks #1950

@ADmad

Description

@ADmad

Summary of a problem or a feature request

For the code snippet showed below phpstan generates error:

+----------------------------------------------+
| Line | test.php                              |
+----------------------------------------------+
| 10   | Result of && is always false. |
+----------------------------------------------+

It seems because the type checks done are same as the types mention in docblock phpstan thinks the checks are unnecessary and will always equate to false. But due to lack of typehint PHP won't throw any error even if an argument of invalid type is passed to the method and extra check is necessary.

Code snippet that reproduces the problem

https://phpstan.org/r/9438ae1f-53d3-495d-98d5-951684ad888f

<?php declare(strict_types = 1);

class HelloWorld
{
	/**
	 * @param array|\ArrayAccess $arg
	 */
	public function sayHello($arg): void
	{
            if (!is_array($arg) && !($arg instanceof \ArrayAccess)) {
                throw new InvalidArgumentException();
            }
	}
}

Expected output

No error shown.

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