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

Make ResultSet iterator current() not return a mixed value #1506

Merged
merged 5 commits into from Oct 30, 2018

Conversation

Projects
None yet
4 participants
@Tobion
Collaborator

Tobion commented Jun 19, 2018

Returning potentially false from the iterator current() means each result set item could be false which makes it impractical to safely use it.
When using phpstan, you will get errors like Cannot call method getId() on bool|Elastica\Result. when you just iterate over the result set.

Also current() should not need to check valid() again because that is done automatically by php when using foreach. When valid returns false, the loop is ended. So that is part of the Iterator spec. So using current on a non-existing item should not happen unless you misuse the Iterator.

@@ -253,8 +249,6 @@ public function current()
public function next()
{
++$this->_position;
return $this->current();

This comment has been minimized.

@Tobion

Tobion Jun 19, 2018

Collaborator

next() is not meant to return something.

This comment has been minimized.

@Tobion

Tobion Oct 28, 2018

Collaborator

also it's not documented. and it would also not be really useful because it might return false or null or whatever when the iterator reaches the end which is what happens as it happens before valid is called. so you cannot rely on chaining next()->getData() like a test does.

return $this->valid()
? $this->_bulkResponses[$this->key()]
: false;
return $this->_bulkResponses[$this->key()];

This comment has been minimized.

@ruflin

ruflin Jun 25, 2018

Owner

I wonder if we should treat this as a bug or breaking change ...

This comment has been minimized.

@Tobion

Tobion Jun 27, 2018

Collaborator

I hope nobody would be affected by this potential bc break as it means people would use the Iterator in a wrong way. As an alternative we could leave the implementation and only change the phpdoc. But that would also be confusing.

This comment has been minimized.

@ruflin

ruflin Jul 2, 2018

Owner

It reminds me of #1374 Let's ping people in there on what they think.

This comment has been minimized.

@curry684

curry684 Jul 2, 2018

I'd take the pragmatic approach here - it could only break code that was already abusing a well-defined mechanism, and I can't think of a really practical example how it would hurt existing code, it would more probably fix it by accident. So I wouldn't call BC on this.

This comment has been minimized.

@greg0ire

greg0ire Jul 2, 2018

Contributor

Looks indeed like a case of xkcd workflow
xkcd workflow

This comment has been minimized.

@ruflin

ruflin Jul 3, 2018

Owner

What would we do without xkcd?

Sounds like we are all agreement we should move forward with this.

@Tobion I suggest we still add an entry to the breaking changes for people to be aware of it.

This comment has been minimized.

@Tobion

Tobion Oct 28, 2018

Collaborator

@ruflin done. This is ready from my point of view.

Tobion added some commits Jun 19, 2018

Make ResultSet iterator current() not return a mixed value
Returning potentially false from the iterator current() means each result set item could be `false` which makes it impractical to safely use it.
When using phpstan, you will get errors like `Cannot call method getId() on bool|Elastica\Result.` when you just iterate over the result set.

Also `current()` should not need to check `valid()` again because that is done automatically by php when using foreach. When valid returns false, the loop is ended. So that is part of the Iterator spec. So using current on a non-existing item should not happen unless you misuse the Iterator.

@Tobion Tobion force-pushed the no-bool-current-iterator branch from d66d692 to 465b14a Oct 28, 2018

@ruflin

ruflin approved these changes Oct 30, 2018

@ruflin ruflin merged commit 044be5e into master Oct 30, 2018

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@ruflin ruflin deleted the no-bool-current-iterator branch Oct 30, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment