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

Fix collections when iterating over certain classes #11396

Merged
merged 6 commits into from Nov 15, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions phpstan.neon
Expand Up @@ -22,6 +22,7 @@ parameters:
- '#Access to an undefined property Exception::\$queryString#'
- '#Access to an undefined property PHPUnit\\Framework\\Test::\$fixtureManager#'
- '#Method Redis::#'
- '#Call to an undefined method Traversable::getArrayCopy().#'
earlyTerminatingMethodCalls:
Cake\Shell\Shell:
- abort
Expand Down
2 changes: 1 addition & 1 deletion src/Collection/CollectionTrait.php
Expand Up @@ -819,7 +819,7 @@ protected function optimizeUnwrap()
{
$iterator = $this->unwrap();

if ($iterator instanceof ArrayIterator) {
if (get_class($iterator) === ArrayIterator::class) {
$iterator = $iterator->getArrayCopy();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Collection/Iterator/ExtractIterator.php
Expand Up @@ -88,7 +88,7 @@ public function unwrap()
$iterator = $iterator->unwrap();
}

if (!$iterator instanceof ArrayIterator) {
if (get_class($iterator) !== ArrayIterator::class) {
return $this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Collection/Iterator/FilterIterator.php
Expand Up @@ -74,7 +74,7 @@ public function unwrap()
$iterator = $iterator->unwrap();
}

if (!$iterator instanceof ArrayIterator) {
if (get_class($iterator) !== ArrayIterator::class) {
return $filter;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Collection/Iterator/ReplaceIterator.php
Expand Up @@ -86,7 +86,7 @@ public function unwrap()
$iterator = $iterator->unwrap();
}

if (!$iterator instanceof ArrayIterator) {
if (get_class($iterator) !== ArrayIterator::class) {
return $this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Collection/Iterator/StoppableIterator.php
Expand Up @@ -97,7 +97,7 @@ public function unwrap()
$iterator = $iterator->unwrap();
}

if (!$iterator instanceof ArrayIterator) {
if (get_class($iterator) !== ArrayIterator::class) {
return $this;
}

Expand Down
48 changes: 48 additions & 0 deletions tests/TestCase/Collection/CollectionTest.php
Expand Up @@ -41,6 +41,28 @@ public function __construct($items)
}
}

/**
* Special class to test that extending \ArrayIterator works as expected
*/
class TestIterator extends ArrayIterator
{
use CollectionTrait;

public $data = [];

public function __construct($data)
{
$this->data = $data;

parent::__construct($data);
}

public function checkValues()
{
return true;
}
}

/**
* CollectionTest
*/
Expand Down Expand Up @@ -2512,4 +2534,30 @@ protected function datePeriod($start, $end)
{
return new \DatePeriod(new \DateTime($start), new \DateInterval('P1D'), new \DateTime($end));
}

/**
* Tests to ensure that collection classes extending ArrayIterator work as expected.
*
* @return void
*/
public function testArrayIteratorExtend()
{
$iterator = new TestIterator(range(0, 10));

$this->assertTrue(method_exists($iterator, 'checkValues'));
$this->assertTrue($iterator->checkValues());

//We need to perform at least two collection operation to trigger the issue.
$newIterator = $iterator
->filter(function ($item) {
return $item < 5;
})
->reject(function ($item) {
return $item > 2;
});

$this->assertTrue(method_exists($newIterator, 'checkValues'), 'Our method has gone missing!');
$this->assertTrue($newIterator->checkValues());
Copy link
Contributor

Choose a reason for hiding this comment

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

@lorenzo Is this a correct use of the Collection class? Personally, I don't think so. Because, for example, if the method of inner iterator is getPrimeNumbers(), people would expect that it returns array(1), as 2, 3, 5 and 7 have been already filtered by filter() and reject() calls. However it would return array(1, 2, 3, 5, 7) actually, as Collection doesn't remove any elements of the original iterator. I thought that was why we blocked calling ArrayIterator::count() via the Collection class.

Refs #11440

Copy link
Member

Choose a reason for hiding this comment

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

It is another method, though. Not the results you get when iterating, so it could be argued that this is vivo the correct and at the same time not.

Copy link
Contributor

Choose a reason for hiding this comment

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

If it's a feature I think it would need to be documented in the Collection page of our cookbook explicitly. Also, I would have to revert my PR #11045, as method_exists($collection, 'cuustomMethod') no longer works. IteretorIterator seems to handle method_exists() magically if the __call() method is not overridden, and we would not be able to emulate the behavior by any PHP code.

Copy link
Member

Choose a reason for hiding this comment

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

Let's not support that feature, then

Copy link
Contributor

Choose a reason for hiding this comment

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

OK. Then I will send a patch to fix the problematic test case.

$this->assertCount(3, $newIterator->toArray());
}
}