Skip to content

array_keys() does not always infer the key type from the given array #2857

Closed
@BenMorel

Description

Bug report

This is similar to #1847, but we're still getting the error with phpstan 0.11.19 and 0.12.5.

Even though the array key type is known, in some situations it is not used to infer the type of array_keys().

Code snippet that reproduces the problem

    /**
     * @param array<string, true> $knownAliases
     */
    function getSQLForJoins(string $fromAlias, array &$knownAliases) : string
    {
        ...
            throw NonUniqueAlias::new($join['joinAlias'], array_keys($knownAliases));

Here the type of array_keys($knownAliases) is inferred as array<int, int|string> instead of array<int, string>.

Sandbox: https://phpstan.org/r/98b7db20-f773-49d9-86d7-014f5d209483
Travis: https://travis-ci.org/doctrine/dbal/jobs/638201455

If you remove lines 45-48 (after the culprit), it suddenly works:

https://phpstan.org/r/586a6826-b4f7-4ea0-8c62-f3fa8a643a1a

Expected output

[OK] No errors

Actual output

 ------ ----------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   lib/Doctrine/DBAL/Query/QueryBuilder.php                                                                                                                   
 ------ ----------------------------------------------------------------------------------------------------------------------------------------------------------- 
  1319   Parameter #2 $registeredAliases of static method Doctrine\DBAL\Query\Exception\NonUniqueAlias::new() expects array<string>, array<int, int|string> given.  
 ------ ----------------------------------------------------------------------------------------------------------------------------------------------------------- 

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions