-
Notifications
You must be signed in to change notification settings - Fork 112
Narrow type of Collection::first() when using Collection::isEmpty() #172
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
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:
$ composer update --prefer-lowest --no-interaction --no-progress --no-suggest
- Downgrading doctrine/collections (1.6.7 => v1.4.0): Extracting archive
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 <7ionmail@gmail.com>
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
if ($collection->isEmpty()) {
$collection->add(1);
$first = $collection->first();
$first; // Is considered as false.
}
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 $collection->add(1); returns. If void then the method is considered impure and this shouldn't be happening. If the return type is different, we need to add @phpstan-impure above the method. See for more details: https://phpstan.org/blog/remembering-and-forgetting-returned-values
Done #203
I'll try to take a look in the week when I'll find time.
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: