1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00
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
2021-01-25 22:04:29 -05:00
2021-02-11 13:59:52 -05:00
2020-12-29 12:57:09 +00:00
2021-02-11 14:06:10 -05:00
2021-02-10 12:20:36 -05:00
2020-12-04 15:11:14 -05:00
2020-05-19 17:51:28 -04:00
2021-01-11 13:36:23 -05:00
2020-11-29 16:27:00 -05:00
2020-12-04 15:11:14 -05:00
2020-11-19 19:40:40 -05:00
2020-10-12 00:59:19 -04:00

Psalm

Packagist Packagist Coverage Status Psalm coverage

Psalm is a static analysis tool for finding errors in PHP applications.

Installation

To get started, check out the installation guide.

Live Demo

You can play around with Psalm on its website.

Documentation

Documentation is available on Psalms website, generated from the docs folder.

Interested in contributing?

Have a look at CONTRIBUTING.md.

Who made this

Built by Matt Brown (@muglug).

Maintained by Matt and Bruce Weirdan (@weirdan).

The engineering team at Vimeo have provided a lot encouragement, especially @nbeliard, @erunion and @nickyr.

Description
No description provided
Readme MIT 108 MiB
Languages
PHP 100%