Skip to content

Commit

Permalink
Count Constaint - check \Countable first
Browse files Browse the repository at this point in the history
A problem in a project where a class has been created `EmptyIteratorWithCount` meant to iterate nothing, but still have a count on it. It is used to display a count somewhere in the project where an iterator is expected, but we don't want to actually fetch any data.

Unit-tests on this project suddenly started to fail after upgrading to latest PhpUnit because it checks EmptyIterator first.
  • Loading branch information
jasverix authored and sebastianbergmann committed Oct 28, 2019
1 parent 24976a8 commit d703dcb
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Framework/Constraint/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ protected function matches($other): bool
*/
protected function getCountOf($other): ?int
{
if ($other instanceof \EmptyIterator) {
return 0;
}

if ($other instanceof Countable || \is_array($other)) {
return \count($other);
}

if ($other instanceof \EmptyIterator) {
return 0;
}

if ($other instanceof Traversable) {
while ($other instanceof IteratorAggregate) {
$other = $other->getIterator();
Expand Down

0 comments on commit d703dcb

Please sign in to comment.