-
Notifications
You must be signed in to change notification settings - Fork 98
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
Narrow type of Collection::first() when using Collection::isEmpty() #172
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the code is fine except for this one comment.
Your other code that you posted in your comment doesn't work, it does something else. When the context is truthy, the type in the second parameter is intersected with the current expression type.
When the context is falsey, the type in the second parameter is removed from the current expression type.
c2f10c6
to
9ce54d8
Compare
@ondrejmirtes That explains it. Really clever! I updated the PR and squashed the commits into one. |
If the tests with the lowest dependencies fail, it usually means that the inspected method (isEmpty/first) is missing in the installed dependency version... |
Hmm, I installed the lowest possible version:
The methods do exist however the generic annotations are not there. I tried to add them to the Stub file, but I guess that's not how it works. Any advice how to proceed? |
I'd just bump the dep version in require-dev... |
Co-authored-by: Semyon <[email protected]>
0e726dd
to
fdba25e
Compare
😁 Done! |
Thank you! |
❤️ Can this also be supported? Or impossible? if (count($collection) > 0) {
$entity2 = $collection->first();
$entity2;
}
if ($collection->count() > 0) {
$entity3 = $collection->first();
$entity3;
} |
Right now it's not possible. We've had some relevant discussion here: phpstan/phpstan-src#355 (comment) |
@ruudk @ondrejmirtes I tried by modifying the tests in https://github.com/phpstan/phpstan-doctrine/blob/master/tests/Type/Doctrine/Collection/data/collection.php and there is now the the following behavior
Should we do something about it ? Like resetting the context when modifying the collection. |
@VincentLanglet Please open a new issue about it. It depends on what |
Done #203 |
I really wanted support for
Collection::isEmpty()
together withCollection::first()
as proposed in #153.This adds tests and they pass.
However, I'm not too sure if the logic in the extension is correct.
When I change the code to:
The test fails for this scenario: