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

After upgrading PHPStan from 0.12.82 to 0.12.83: Assert::notNull($object->getEvent()) no longer removes nullability #44

Closed
ruudk opened this issue Apr 6, 2021 · 4 comments

Comments

@ruudk
Copy link
Contributor

ruudk commented Apr 6, 2021

This code used to work fine, until I upgraded from 0.12.82 to 0.12.83

class Draft {
    public function getEvent(): ?Event {}
}

class Listing {
    public function setEvent(Event $event): void {}
}

private function createListing(Draft $draft) : Listing
{
    Assert::notNull($draft->getEvent());

    $listing = new Listing();
 
    // Parameter #1 $event of method Listing::setEvent() expects Event, Event|null given.
    $listing->setEvent($draft->getEvent());

    return $listing;
}

Tried to reproduce it but I cannot: https://phpstan.org/r/c0ef8412-208b-441d-95c5-e3a9df165e55

@ondrejmirtes
Copy link
Member

Hi, this is definitely happening because you're using bleedingEdge, this is a preview how 1.0 is going to behave.

There's an article about this: https://phpstan.org/blog/remembering-and-forgetting-returned-values

Your solution is to store event into a variable right after the assert.

@ondrejmirtes
Copy link
Member

This is what's probably going on in your original code: https://phpstan.org/r/22970d69-43e2-4607-9d82-cbaf6e5c884c

@ruudk
Copy link
Contributor Author

ruudk commented Apr 6, 2021

Got it, makes sense! Thanks.

@ruudk ruudk closed this as completed Apr 6, 2021
@github-actions
Copy link

github-actions bot commented May 8, 2021

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 May 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants