Skip to content

0|positive-int += $bool : 0 ? 1, should not give a 'does not accept int' error #4843

@HenkPoley

Description

@HenkPoley

Bug report

When you declare a class attribute as 0 or higher, and initialise it as 0. Then adding 0 or 1 to it, should not be a problem. It's still 0 or higher.

Code snippet that reproduces the problem

https://phpstan.org/r/4e349804-7493-43ab-81fd-ed54f3f388de

<?php

class Bar {
    /**
     * @var int
     * @psalm-var 0|positive-int
     */
    protected $depth = 0;

    function foo(bool $isRoot): void {
        $this->depth += $isRoot ? 0 : 1;
    }
}

Real world example: https://github.com/FreeDSx/LDAP/blob/7722aea71a840d9cbf133a61ef326fc049b3dd34/src/FreeDSx/Ldap/Search/FilterParser.php#L127

Expected output

No output in particular

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions