Skip to content

Commit

Permalink
Issuing a count() on a Collection should throw an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
HavokInspiration committed Oct 22, 2015
1 parent 4055928 commit c96f30c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Collection/Collection.php
Expand Up @@ -17,6 +17,7 @@
use ArrayIterator;
use InvalidArgumentException;
use IteratorIterator;
use LogicException;
use Serializable;
use Traversable;

Expand Down Expand Up @@ -71,6 +72,20 @@ public function unserialize($collection)
$this->__construct(unserialize($collection));
}

/**
* Throws an exception.
*
* Issuing a count on a Collection can have many side effects, some making the
* Collection unusable after the count operation.
*
* @return void
* @throws \LogicException
*/
public function count()
{
throw new LogicException('You cannot issue a count on a Collection.');
}

/**
* Returns an array that can be used to describe the internal state of this
* object.
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/Collection/CollectionTest.php
Expand Up @@ -611,6 +611,19 @@ public function testInvalidConstructorArgument()
new Collection('Derp');
}

/**
* Tests that issuing a count will throw an exception
*
* @expectedException \LogicException
* @return void
*/
public function testCollectionCount()
{
$data = [1, 2, 3, 4];
$collection = new Collection($data);
$collection->count();
}

/**
* Tests take method
*
Expand Down

0 comments on commit c96f30c

Please sign in to comment.