Skip to content

Bitwise Operators & restricted integers & properties -> Error that shouldn't be #7912

@EliW

Description

@EliW

Bug report

So the problem appears to be that if you have two integers that are limited in value. And then do a bitwise comparison or assignment on them that should be always valid. phpstan is claiming that the result value of the bitwise comparator is int and doesn't match the signature (when it always will)

Code snippet that reproduces the problem

If you do direct bitwise assignments or comparisons, everything works fine ... Including if you declare just variables directly. For example:

/** @var int<0,1> */
$c = 0;
$c |= 1;

However, if that variable is instead a property on a class ... it will complain that:
Property A::$has (int<0, 1>) does not accept int.

Easiest to see in detail here:
https://phpstan.org/r/d548d784-9771-42c8-95fc-66e8cf165abd

Expected output

No errors should be reported in the first two cases in my above example:

$a->has |= $b->has;
$a->has = $a->has | $b->has;

Did PHPStan help you today? Did it make you happy in any way?

PHPStan helps every day. Finding bugs before they happen. And honestly helping refactor by 'lazily' renaming a variable then running phpstan to find all the places I need to change it. Total convert!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions