annotate vendor/phpspec/prophecy/src/Prophecy/Doubler/ClassPatch/TraversablePatch.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Prophecy.
Chris@0 5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
Chris@0 6 * Marcello Duarte <marcello.duarte@gmail.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Prophecy\Doubler\ClassPatch;
Chris@0 13
Chris@0 14 use Prophecy\Doubler\Generator\Node\ClassNode;
Chris@0 15 use Prophecy\Doubler\Generator\Node\MethodNode;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Traversable interface patch.
Chris@0 19 * Forces classes that implement interfaces, that extend Traversable to also implement Iterator.
Chris@0 20 *
Chris@0 21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
Chris@0 22 */
Chris@0 23 class TraversablePatch implements ClassPatchInterface
Chris@0 24 {
Chris@0 25 /**
Chris@0 26 * Supports nodetree, that implement Traversable, but not Iterator or IteratorAggregate.
Chris@0 27 *
Chris@0 28 * @param ClassNode $node
Chris@0 29 *
Chris@0 30 * @return bool
Chris@0 31 */
Chris@0 32 public function supports(ClassNode $node)
Chris@0 33 {
Chris@0 34 if (in_array('Iterator', $node->getInterfaces())) {
Chris@0 35 return false;
Chris@0 36 }
Chris@0 37 if (in_array('IteratorAggregate', $node->getInterfaces())) {
Chris@0 38 return false;
Chris@0 39 }
Chris@0 40
Chris@0 41 foreach ($node->getInterfaces() as $interface) {
Chris@0 42 if ('Traversable' !== $interface && !is_subclass_of($interface, 'Traversable')) {
Chris@0 43 continue;
Chris@0 44 }
Chris@0 45 if ('Iterator' === $interface || is_subclass_of($interface, 'Iterator')) {
Chris@0 46 continue;
Chris@0 47 }
Chris@0 48 if ('IteratorAggregate' === $interface || is_subclass_of($interface, 'IteratorAggregate')) {
Chris@0 49 continue;
Chris@0 50 }
Chris@0 51
Chris@0 52 return true;
Chris@0 53 }
Chris@0 54
Chris@0 55 return false;
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * Forces class to implement Iterator interface.
Chris@0 60 *
Chris@0 61 * @param ClassNode $node
Chris@0 62 */
Chris@0 63 public function apply(ClassNode $node)
Chris@0 64 {
Chris@0 65 $node->addInterface('Iterator');
Chris@0 66
Chris@0 67 $node->addMethod(new MethodNode('current'));
Chris@0 68 $node->addMethod(new MethodNode('key'));
Chris@0 69 $node->addMethod(new MethodNode('next'));
Chris@0 70 $node->addMethod(new MethodNode('rewind'));
Chris@0 71 $node->addMethod(new MethodNode('valid'));
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * Returns patch priority, which determines when patch will be applied.
Chris@0 76 *
Chris@0 77 * @return int Priority number (higher - earlier)
Chris@0 78 */
Chris@0 79 public function getPriority()
Chris@0 80 {
Chris@0 81 return 100;
Chris@0 82 }
Chris@0 83 }