1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +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 Detect redundant cast (#4695) 2020-11-25 12:04:48 -05:00
ConditionalTest.php Revert "Correct Psalm’s concept of string emptiness" 2021-02-03 01:09:58 -05:00
EmptyTest.php Ensure that strict empty handling always happens 2020-12-01 22:50:21 -05:00
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 Fix #4759 – don’t generate assertion when assigning to same-named variable 2020-12-03 09:42:26 -05:00
TypeTest.php Fix #3017 - use correct keys when converting list to array 2020-11-27 20:05:16 -05:00
ValueTest.php Add non-falsy-string to allow more accurate checks 2021-02-03 17:19:48 -05:00