Browse Source

Disable access to methods of inner iterator of Collection

Refs #11045
chinpei215 8 years ago
parent
commit
661ea328a1
2 changed files with 9 additions and 7 deletions
  1. 7 6
      src/Collection/Collection.php
  2. 2 1
      tests/TestCase/Collection/CollectionTest.php

+ 7 - 6
src/Collection/Collection.php

@@ -15,9 +15,9 @@
 namespace Cake\Collection;
 
 use ArrayIterator;
+use BadMethodCallException;
 use InvalidArgumentException;
 use IteratorIterator;
-use LogicException;
 use Serializable;
 use Traversable;
 
@@ -75,15 +75,16 @@ class Collection extends IteratorIterator implements CollectionInterface, Serial
     /**
      * Throws an exception.
      *
-     * Issuing a count on a Collection can have many side effects, some making the
-     * Collection unusable after the count operation.
+     * Collection doesn't permit access to methods of the inner iterator.
      *
+     * @param string $name Method name.
+     * @param array $args Method arguments.
      * @return void
-     * @throws \LogicException
+     * @throws \BadMethodCallException
      */
-    public function count()
+    public function __call($name, $args)
     {
-        throw new LogicException('You cannot issue a count on a Collection.');
+        throw new BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_class($this), $name));
     }
 
     /**

+ 2 - 1
tests/TestCase/Collection/CollectionTest.php

@@ -964,7 +964,8 @@ class CollectionTest extends TestCase
     /**
      * Tests that issuing a count will throw an exception
      *
-     * @expectedException \LogicException
+     * @expectedException \BadMethodCallException
+     * @expectedExceptionMessage Call to undefined method Cake\Collection\Collection::count()
      * @return void
      */
     public function testCollectionCount()