Skip to content

Warn about implicit cast to int when using bool values as array keys. #121

@markuspoerschke

Description

@markuspoerschke

Feature request

When using bool values as array keys, then these keys are implicitly casted to integers. This will cause problems when a strict comparison === is applied on these keys.

Example Code:

<?php

declare(strict_types=1);

$array = [
    true => "true",
    false => "false",
];

foreach ($array as $key => $value) {
    var_export($key); echo " ";
}

The expected output (if boolean is assumed a valid array key) when running the code is

true
false

but because of int cast, the output is

1
0

Therefore PHPStan should output the following error messages for the given code:

 ------ -------------------------------------------------------------
  Line   test.php
 ------ -------------------------------------------------------------
  6      Invalid array key type bool.
  7      Invalid array key type bool.
 ------ -------------------------------------------------------------

Metadata

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