Skip to content

False positive on Assert::upper() always evaluates as true #85

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

Closed
p4veI opened this issue Jan 10, 2022 · 8 comments · Fixed by #89
Closed

False positive on Assert::upper() always evaluates as true #85

p4veI opened this issue Jan 10, 2022 · 8 comments · Fixed by #89

Comments

@p4veI
Copy link

p4veI commented Jan 10, 2022

Hello I just updated phpstan-webmozart-assert to v1.0.7 and my codebase started reporting this error:

Call to static method Webmozart\Assert\Assert::upper() with non-empty-string will always evaluate to true.

It's failing in the following class construct

final class CityCode
{
    private function __construct(private string $cityCode)
    {
    }

    public static function fromString(string $cityCode) : self
    {
        Assert::length($cityCode, 3);
        Assert::upper($cityCode);

        return new self($cityCode);
    }
}

I'm not using the non-empty-string type assertion anywhere so it must be resolved from the length assertion, however that doesn't ensure what upper is doing.

Thank you for looking into this, and all your great work.

@herndlm
Copy link
Contributor

herndlm commented Jan 10, 2022

I broke this and will look into it

@ondrejmirtes from a type perspective everything is fine here, but the "integration" with phpstan seems to be problematic then. Is there a way to add such kind of phpstan integration tests here to prevent regressions? So far we're only testing the types. UPDATE: I think I found it. UPDATE2: No, I think I didn't xD UPDATE3: guess I need PHPStanTestCase, will continue

I guess I was taking too many shortcuts with my re-used non-empty-string expression :/

@herndlm
Copy link
Contributor

herndlm commented Jan 15, 2022

@p4veI are you using the phpstan-strict-rules extension too?

@p4veI
Copy link
Author

p4veI commented Jan 15, 2022

@p4veI are you using the phpstan-strict-rules extension too?

Yes, I am.

@herndlm
Copy link
Contributor

herndlm commented Jan 17, 2022

@ondrejmirtes there are more global non-empty-string problems related to ImpossibleCheckTypeStaticMethodCallRule and my last 2 feature commits (19b869e and 9009135). Maybe only triggered if phpstan-strict-rules is used too (assumingly because checkAlwaysTrueCheckTypeFunctionCall of the rule is true then). See also #86 (comment) where I tried to initially fix it but failed.

As I know how busy you are - maybe it makes sense to just revert those 2 commits for now? I'd like to add integration / regression tests then too to avoid such failures in the future. Sorry for the troubles caused.

@herndlm
Copy link
Contributor

herndlm commented Jan 17, 2022

Created #89 which fixes this by reverting and adding the longed-for regression test

@ruudk
Copy link
Contributor

ruudk commented Jan 17, 2022

@herndlm Thanks for trying to improve PHPStan's extensions, things like this happen, don't let it discourage you to try again 💙

@herndlm
Copy link
Contributor

herndlm commented Jan 17, 2022

@ruudk thx. Yeah, don't worry about that 😅

@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 Feb 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants