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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file based on the

### Backward Compatibility Breaks

* Made result sets adhere to `\Iterator` interface definition that they implement. Specifically, you need to call `valid()` on the result set before calling `current()`. When using `foreach` this is done by PHP automatically. When `valid` returns false, the return value of `current` is undefined instead of false.
* `\Elastica\ResultSet::next` returns `void` instead of `\Elastica\Result|false`
* `\Elastica\Bulk\ResponseSet::current` returns `\Elastica\Bulk\Response` instead of `\Elastica\Bulk\Response|false`
* `\Elastica\Multi\ResultSet::current` returns `\Elastica\ResultSet` instead of `\Elastica\ResultSet|false`

### Bugfixes

### Added
Expand Down
6 changes: 2 additions & 4 deletions lib/Elastica/Bulk/ResponseSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,11 @@ public function hasError()
}

/**
* @return bool|\Elastica\Bulk\Response
* @return \Elastica\Bulk\Response
*/
public function current()
{
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.

}

/**
Expand Down
11 changes: 4 additions & 7 deletions lib/Elastica/Multi/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Elastica\Multi;

use Elastica\Response;
use Elastica\ResultSet as BaseResultSet;

/**
* Elastica multi search result set
Expand Down Expand Up @@ -36,8 +35,8 @@ class ResultSet implements \Iterator, \ArrayAccess, \Countable
/**
* Constructs ResultSet object.
*
* @param \Elastica\Response $response
* @param BaseResultSet[]
* @param \Elastica\Response $response
* @param \Elastica\ResultSet[] $resultSets
*/
public function __construct(Response $response, $resultSets)
{
Expand Down Expand Up @@ -80,13 +79,11 @@ public function hasError()
}

/**
* @return bool|\Elastica\ResultSet
* @return \Elastica\ResultSet
*/
public function current()
{
return $this->valid()
? $this->_resultSets[$this->key()]
: false;
return $this->_resultSets[$this->key()];
}

/**
Expand Down
12 changes: 3 additions & 9 deletions lib/Elastica/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,11 @@ public function countSuggests()
/**
* Returns the current object of the set.
*
* @return \Elastica\Result|bool Set object or false if not valid (no more entries)
* @return \Elastica\Result Set object
*/
public function current()
{
if ($this->valid()) {
return $this->_results[$this->key()];
}

return false;
return $this->_results[$this->key()];
}

/**
Expand All @@ -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.

}

/**
Expand Down Expand Up @@ -308,7 +302,7 @@ public function offsetExists($offset)
*
* @throws Exception\InvalidException If offset doesn't exist
*
* @return Result|null
* @return Result
*/
public function offsetGet($offset)
{
Expand Down
28 changes: 4 additions & 24 deletions test/Elastica/Bulk/ResponseSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,6 @@ public function testIterator()
}

$this->assertFalse($responseSet->valid());
$this->assertNotInstanceOf(Bulk\Response::class, $responseSet->current());
$this->assertFalse($responseSet->current());

$responseSet->next();

$this->assertFalse($responseSet->valid());
$this->assertNotInstanceOf(Bulk\Response::class, $responseSet->current());
$this->assertFalse($responseSet->current());

$responseSet->rewind();

Expand All @@ -126,21 +118,12 @@ public function isOkDataProvider()
{
list($responseData, $actions) = $this->_getFixture();

$return = [];
$return[] = [$responseData, $actions, true];
yield [$responseData, $actions, true];
$responseData['items'][2]['index']['ok'] = false;
$return[] = [$responseData, $actions, false];

return $return;
yield [$responseData, $actions, false];
}

/**
* @param array $responseData
* @param array $actions
*
* @return ResponseSet
*/
protected function _createResponseSet(array $responseData, array $actions)
protected function _createResponseSet(array $responseData, array $actions): ResponseSet
{
$client = $this->createMock(Client::class);

Expand All @@ -155,10 +138,7 @@ protected function _createResponseSet(array $responseData, array $actions)
return $bulk->send();
}

/**
* @return array
*/
protected function _getFixture()
protected function _getFixture(): array
{
$responseData = [
'took' => 5,
Expand Down
6 changes: 4 additions & 2 deletions test/Elastica/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public function testSetSort()
$this->assertEquals(2, $resultSet->count());

$first = $resultSet->current()->getData();
$second = $resultSet->next()->getData();
$resultSet->next();
$second = $resultSet->current()->getData();

$this->assertEquals('guschti', $first['firstname']);
$this->assertEquals('nicolas', $second['firstname']);
Expand All @@ -130,7 +131,8 @@ public function testSetSort()
$this->assertEquals(2, $resultSet->count());

$first = $resultSet->current()->getData();
$second = $resultSet->next()->getData();
$resultSet->next();
$second = $resultSet->current()->getData();

$this->assertEquals('nicolas', $first['firstname']);
$this->assertEquals('guschti', $second['firstname']);
Expand Down