Skip to content

Casting to int something that's already an int #3884

@Synchro

Description

@Synchro

Bug report

This is a duplicate of #1251, but with a better use case.

phpstan reports Casting to int something that's already an int even when the something has a possibility of not being an int.

In my case this occurs with bindec, which has a documented return type of float|int, and I need to guarantee that it's an int, so I'm casting it (it's safe because I'm also limiting the input size to 3 hex chars, so it will never actually hit the overflow that would cause it to return a float).

The cast is also needed for other type checkers such as psalm.

Code snippet that reproduces the problem

$chunk = '123';
$offset = (int)bindec(base_convert($chunk, 16, 2));

phpstan's opinion would be correct if it could know all possible values of $chunk, but that's hard to know. For example FFFFFFFFFFFFFFFF would return an int from bindec, but FFFFFFFFFFFFFFFFF would return a float.

Expected output

No error

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