Chris@0: Chris@0: * Marcello Duarte Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Prophecy\Doubler\ClassPatch; Chris@0: Chris@0: use Prophecy\Doubler\Generator\Node\ClassNode; Chris@0: use Prophecy\Doubler\Generator\Node\MethodNode; Chris@0: Chris@0: /** Chris@0: * Traversable interface patch. Chris@0: * Forces classes that implement interfaces, that extend Traversable to also implement Iterator. Chris@0: * Chris@0: * @author Konstantin Kudryashov Chris@0: */ Chris@0: class TraversablePatch implements ClassPatchInterface Chris@0: { Chris@0: /** Chris@0: * Supports nodetree, that implement Traversable, but not Iterator or IteratorAggregate. Chris@0: * Chris@0: * @param ClassNode $node Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function supports(ClassNode $node) Chris@0: { Chris@0: if (in_array('Iterator', $node->getInterfaces())) { Chris@0: return false; Chris@0: } Chris@0: if (in_array('IteratorAggregate', $node->getInterfaces())) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: foreach ($node->getInterfaces() as $interface) { Chris@0: if ('Traversable' !== $interface && !is_subclass_of($interface, 'Traversable')) { Chris@0: continue; Chris@0: } Chris@0: if ('Iterator' === $interface || is_subclass_of($interface, 'Iterator')) { Chris@0: continue; Chris@0: } Chris@0: if ('IteratorAggregate' === $interface || is_subclass_of($interface, 'IteratorAggregate')) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Forces class to implement Iterator interface. Chris@0: * Chris@0: * @param ClassNode $node Chris@0: */ Chris@0: public function apply(ClassNode $node) Chris@0: { Chris@0: $node->addInterface('Iterator'); Chris@0: Chris@0: $node->addMethod(new MethodNode('current')); Chris@0: $node->addMethod(new MethodNode('key')); Chris@0: $node->addMethod(new MethodNode('next')); Chris@0: $node->addMethod(new MethodNode('rewind')); Chris@0: $node->addMethod(new MethodNode('valid')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns patch priority, which determines when patch will be applied. Chris@0: * Chris@0: * @return int Priority number (higher - earlier) Chris@0: */ Chris@0: public function getPriority() Chris@0: { Chris@0: return 100; Chris@0: } Chris@0: }