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

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

Merged
merged 5 commits into from
Oct 30, 2018

Conversation

Tobion
Copy link
Collaborator

@Tobion 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();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next() is not meant to return something.

Copy link
Collaborator Author

@Tobion Tobion Oct 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()];
Copy link
Owner

@ruflin ruflin Jun 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

@greg0ire greg0ire Jul 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks indeed like a case of xkcd workflow
xkcd workflow

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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.
@ruflin ruflin merged commit 044be5e into master Oct 30, 2018
@ruflin ruflin deleted the no-bool-current-iterator branch October 30, 2018 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants