Skip to content

Error when casting values from mixed array to int #4490

@davidlienhard

Description

@davidlienhard

Good Morning
I do have an array with multiple values that may be an int or string. If try to cast those values to int in a foreach loop, I get the following error: Cannot cast 1|2|3|'test'

So for example the following code:

<?php
declare(strict_types=1);

foreach ([ 1, 2, 3, 'test' ] as $value) {
    $value = (int) $value;
}

Returns this result.

5      Cannot cast 1|2|3|'test' to int.

Link to reproduce:
https://phpstan.org/r/91468695-25fb-4e96-922d-1db4fd680b40

Expected output

In my opinion this is valid code. Or am I missing something?

I'm on phpstan 0.12.71, level 7 with php 8.0.1

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