4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 * Marcello Duarte <marcello.duarte@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Prophecy\Doubler\ClassPatch;
14 use Prophecy\Doubler\Generator\Node\ClassNode;
15 use Prophecy\Doubler\Generator\Node\MethodNode;
18 * Traversable interface patch.
19 * Forces classes that implement interfaces, that extend Traversable to also implement Iterator.
21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
23 class TraversablePatch implements ClassPatchInterface
26 * Supports nodetree, that implement Traversable, but not Iterator or IteratorAggregate.
28 * @param ClassNode $node
32 public function supports(ClassNode $node)
34 if (in_array('Iterator', $node->getInterfaces())) {
37 if (in_array('IteratorAggregate', $node->getInterfaces())) {
41 foreach ($node->getInterfaces() as $interface) {
42 if ('Traversable' !== $interface && !is_subclass_of($interface, 'Traversable')) {
45 if ('Iterator' === $interface || is_subclass_of($interface, 'Iterator')) {
48 if ('IteratorAggregate' === $interface || is_subclass_of($interface, 'IteratorAggregate')) {
59 * Forces class to implement Iterator interface.
61 * @param ClassNode $node
63 public function apply(ClassNode $node)
65 $node->addInterface('Iterator');
67 $node->addMethod(new MethodNode('current'));
68 $node->addMethod(new MethodNode('key'));
69 $node->addMethod(new MethodNode('next'));
70 $node->addMethod(new MethodNode('rewind'));
71 $node->addMethod(new MethodNode('valid'));
75 * Returns patch priority, which determines when patch will be applied.
77 * @return int Priority number (higher - earlier)
79 public function getPriority()