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

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
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 use Prophecy\PhpDocumentor\ClassAndInterfaceTagRetriever;
Chris@0 17 use Prophecy\PhpDocumentor\MethodTagRetrieverInterface;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Discover Magical API using "@method" PHPDoc format.
Chris@0 21 *
Chris@0 22 * @author Thomas Tourlourat <thomas@tourlourat.com>
Chris@0 23 * @author Kévin Dunglas <dunglas@gmail.com>
Chris@0 24 * @author Théo FIDRY <theo.fidry@gmail.com>
Chris@0 25 */
Chris@0 26 class MagicCallPatch implements ClassPatchInterface
Chris@0 27 {
Chris@0 28 private $tagRetriever;
Chris@0 29
Chris@0 30 public function __construct(MethodTagRetrieverInterface $tagRetriever = null)
Chris@0 31 {
Chris@0 32 $this->tagRetriever = null === $tagRetriever ? new ClassAndInterfaceTagRetriever() : $tagRetriever;
Chris@0 33 }
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Support any class
Chris@0 37 *
Chris@0 38 * @param ClassNode $node
Chris@0 39 *
Chris@0 40 * @return boolean
Chris@0 41 */
Chris@0 42 public function supports(ClassNode $node)
Chris@0 43 {
Chris@0 44 return true;
Chris@0 45 }
Chris@0 46
Chris@0 47 /**
Chris@0 48 * Discover Magical API
Chris@0 49 *
Chris@0 50 * @param ClassNode $node
Chris@0 51 */
Chris@0 52 public function apply(ClassNode $node)
Chris@0 53 {
Chris@0 54 $types = array_filter($node->getInterfaces(), function ($interface) {
Chris@0 55 return 0 !== strpos($interface, 'Prophecy\\');
Chris@0 56 });
Chris@0 57 $types[] = $node->getParentClass();
Chris@0 58
Chris@0 59 foreach ($types as $type) {
Chris@0 60 $reflectionClass = new \ReflectionClass($type);
Chris@0 61
Chris@17 62 while ($reflectionClass) {
Chris@17 63 $tagList = $this->tagRetriever->getTagList($reflectionClass);
Chris@0 64
Chris@17 65 foreach ($tagList as $tag) {
Chris@17 66 $methodName = $tag->getMethodName();
Chris@17 67
Chris@17 68 if (empty($methodName)) {
Chris@17 69 continue;
Chris@17 70 }
Chris@17 71
Chris@17 72 if (!$reflectionClass->hasMethod($methodName)) {
Chris@17 73 $methodNode = new MethodNode($methodName);
Chris@17 74 $methodNode->setStatic($tag->isStatic());
Chris@17 75 $node->addMethod($methodNode);
Chris@17 76 }
Chris@0 77 }
Chris@0 78
Chris@17 79 $reflectionClass = $reflectionClass->getParentClass();
Chris@0 80 }
Chris@0 81 }
Chris@0 82 }
Chris@0 83
Chris@0 84 /**
Chris@0 85 * Returns patch priority, which determines when patch will be applied.
Chris@0 86 *
Chris@0 87 * @return integer Priority number (higher - earlier)
Chris@0 88 */
Chris@0 89 public function getPriority()
Chris@0 90 {
Chris@0 91 return 50;
Chris@0 92 }
Chris@0 93 }
Chris@0 94