annotate vendor/phpspec/prophecy/src/Prophecy/Doubler/ClassPatch/MagicCallPatch.php @ 9:1fc0ff908d1f

Add another data file
author Chris Cannam
date Mon, 05 Feb 2018 12:34:32 +0000
parents 4c8ae668cc8c
children 129ea1e6d783
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 $tagList = $this->tagRetriever->getTagList($reflectionClass);
Chris@0 62
Chris@0 63 foreach($tagList as $tag) {
Chris@0 64 $methodName = $tag->getMethodName();
Chris@0 65
Chris@0 66 if (empty($methodName)) {
Chris@0 67 continue;
Chris@0 68 }
Chris@0 69
Chris@0 70 if (!$reflectionClass->hasMethod($methodName)) {
Chris@0 71 $methodNode = new MethodNode($methodName);
Chris@0 72 $methodNode->setStatic($tag->isStatic());
Chris@0 73 $node->addMethod($methodNode);
Chris@0 74 }
Chris@0 75 }
Chris@0 76 }
Chris@0 77 }
Chris@0 78
Chris@0 79 /**
Chris@0 80 * Returns patch priority, which determines when patch will be applied.
Chris@0 81 *
Chris@0 82 * @return integer Priority number (higher - earlier)
Chris@0 83 */
Chris@0 84 public function getPriority()
Chris@0 85 {
Chris@0 86 return 50;
Chris@0 87 }
Chris@0 88 }
Chris@0 89