1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00
psalm/tests/TypeReconciliation
Alberto Piai 144bb37f76
Fix array_key_exists() with all int literal keys (#5197)
When checking code like the following:

```
<?php

function checkNegated(string $key): void {
    $arr = [
        0 => "foo",
        1 => "bar",
    ];

    if (!array_key_exists($key, $arr)) {
        printf("not found\n");
    }
}

function check(string $key): void {
    $arr = [
        0 => "foo",
        1 => "bar",
    ];

    if (array_key_exists($key, $arr)) {
        printf("found\n");
    }
}
```

the `if` in `checkNegated` would cause:

```
ERROR: RedundantCondition - 9:10 - Type string for $key is never =int(0)
```

This happens when the array keys are all int literals, but the "needle"
is a string.

`array_key_exists()` uses a loose equality comparison, but the generated
assertions for this specific case
(`AssertionFinder::getArrayKeyExistsAssertions`) was generating strict
equality clauses. This commit fixes it by changing the generated clause
from `=` to `~`.
2021-02-12 17:00:38 -05:00
..
ArrayKeyExistsTest.php Fix array_key_exists() with all int literal keys (#5197) 2021-02-12 17:00:38 -05:00
AssignmentInConditionalTest.php
ConditionalTest.php Revert "Correct Psalm’s concept of string emptiness" 2021-02-03 01:09:58 -05:00
EmptyTest.php
IssetTest.php Fix #4802 - don’t clone context when analysing coalesce 2020-12-07 14:30:57 -05:00
ReconcilerTest.php Ensure getId() output can always be parsed as a type 2021-01-25 23:41:51 -05:00
RedundantConditionTest.php Unset should change array with keyed offsets 2021-01-13 18:10:59 -05:00
ScopeTest.php Full support for $this in @var annotations (#4922) 2021-01-03 01:44:35 +00:00
TypeAlgebraTest.php
TypeTest.php
ValueTest.php Add non-falsy-string to allow more accurate checks 2021-02-03 17:19:48 -05:00