Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
HenkPoley opened this issue Apr 12, 2021 · 7 comments
Labels
Milestone

Comments

@HenkPoley
Copy link

HenkPoley commented Apr 12, 2021

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

@ondrejmirtes
Copy link
Member

Root issue: https://phpstan.org/r/c0259855-d15d-4ee3-9816-8e6722179db2

This should still result in positive-int|0. It can be fixed in MutatingScope::resolveType().

@HenkPoley
Copy link
Author

HenkPoley commented Apr 12, 2021

Something similar happens further down the 'Real world example' code. Where function parameter int $startAt and $startAt += 2; seems to cause $startAt to be seen as int|null 🤷‍♂️

HenkPoley added a commit to HenkPoley/LDAP that referenced this issue Apr 12, 2021
ChadSikorra pushed a commit to FreeDSx/LDAP that referenced this issue Apr 19, 2021
* Updated PhpDoc, mainly made with Psalm

* Also add the few import changes

* More @param array vs splat operator fixes

* A few @return fixes

* Fix reeDSx/Ldap/Entry/Option.php:63 in `composer run-script analyse`

* Various cleanups around InheritDoc.

* Various cleanups around InheritDoc.

* Various cleanups around InheritDoc.

* Various cleanups around InheritDoc.

* Various cleanups around InheritDoc.

* This confuses phpstan: phpstan/phpstan#4843 (comment)
@phpstan-bot
Copy link
Contributor

@HenkPoley After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-11: Property Bar::$depth (int<0, max>) does not accept int.
+No errors

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-11: Dumped type: int
+11: Dumped type: int<0, max>
Full report
Line Error
11 Dumped type: int<0, max>

@staabm
Copy link
Contributor

staabm commented Aug 27, 2021

I think this was fixed with recent commits

@ondrejmirtes
Copy link
Member

Fixed by: phpstan/phpstan-src#637

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants