Skip to content

Incorrect assertion that "array_key_exists" will always be false #1613

@benm-brainlabs

Description

@benm-brainlabs

Summary of a problem or a feature request

Using a string argument in the function declaration that then is used in array_key_exists with an array where all the key's are string versions of integers will provide the error Call to function array_key_exists() with string and array(123 => 'test') will always evaluate to false.

With phpstan 0.10.5 and php 7.2.9.

Code snippet that reproduces the problem

<?php 

namespace Project\Test;

class TestClass
{
    public function test(string $index)
    {
        $array = [
            "123" => "test"
        ];
        return array_key_exists($index, $array);
    }
}

but if you run

<?php
declare(strict_types = 1);

use Project\Test\TestClass;

require_once __DIR__ . '/vendor/autoload.php';


$test = new TestClass();
var_dump($test->test("123"));

it will log bool(true)

https://phpstan.org/r/e2d1c03a-235d-410a-8966-24bc4987167d

Expected output

There should not be any issue raised.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions