Skip to content

array_key_exist inference is lost when adding a false condition #11276

@VincentLanglet

Description

@VincentLanglet

Bug report

Adding a false condition in a if remove sometimes the PHPStan assertion inferences.
An easy way to reproduce is adding || false.

I know this is might appear as a useless fix because the developer should remove the always false part ; but this seems necessary for phpstan plugins like the phpunit one because of code like https://github.com/phpstan/phpstan-phpunit/blob/f3ea021866f4263f07ca3636bf22c64be9610c11/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php#L292-L300

static::assertArrayHasKey is changed to

if (($foo instanceof ArrayAccess && ...) || \array_key_exist($key, $foo)) {}

I'm not sure if the fix can be done on phpstan-src side or if it must be done on phpstan-phpunit side.

Code snippet that reproduces the problem

https://phpstan.org/r/54abc129-7eb5-46d9-856b-96f6981eaebf

Expected output

I expect not having the error

Offset 'from'|'to' might not exist on array{0?: string, from?: string, 1?: string, to?: string, 2?: string}.

Which is the case for https://phpstan.org/r/ccfd18ce-41ee-4fa2-afac-0f00aac03a3d

Notice, the error is not reported when the array only have one key
https://phpstan.org/r/b0f29d64-8c7b-482e-8a35-1ed024028db4

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

Sure, the latests release are great.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions