comparison vendor/phpspec/prophecy/src/Prophecy/Doubler/ClassPatch/MagicCallPatch.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 * Marcello Duarte <marcello.duarte@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Prophecy\Doubler\ClassPatch;
13
14 use Prophecy\Doubler\Generator\Node\ClassNode;
15 use Prophecy\Doubler\Generator\Node\MethodNode;
16 use Prophecy\PhpDocumentor\ClassAndInterfaceTagRetriever;
17 use Prophecy\PhpDocumentor\MethodTagRetrieverInterface;
18
19 /**
20 * Discover Magical API using "@method" PHPDoc format.
21 *
22 * @author Thomas Tourlourat <thomas@tourlourat.com>
23 * @author Kévin Dunglas <dunglas@gmail.com>
24 * @author Théo FIDRY <theo.fidry@gmail.com>
25 */
26 class MagicCallPatch implements ClassPatchInterface
27 {
28 private $tagRetriever;
29
30 public function __construct(MethodTagRetrieverInterface $tagRetriever = null)
31 {
32 $this->tagRetriever = null === $tagRetriever ? new ClassAndInterfaceTagRetriever() : $tagRetriever;
33 }
34
35 /**
36 * Support any class
37 *
38 * @param ClassNode $node
39 *
40 * @return boolean
41 */
42 public function supports(ClassNode $node)
43 {
44 return true;
45 }
46
47 /**
48 * Discover Magical API
49 *
50 * @param ClassNode $node
51 */
52 public function apply(ClassNode $node)
53 {
54 $types = array_filter($node->getInterfaces(), function ($interface) {
55 return 0 !== strpos($interface, 'Prophecy\\');
56 });
57 $types[] = $node->getParentClass();
58
59 foreach ($types as $type) {
60 $reflectionClass = new \ReflectionClass($type);
61 $tagList = $this->tagRetriever->getTagList($reflectionClass);
62
63 foreach($tagList as $tag) {
64 $methodName = $tag->getMethodName();
65
66 if (empty($methodName)) {
67 continue;
68 }
69
70 if (!$reflectionClass->hasMethod($methodName)) {
71 $methodNode = new MethodNode($methodName);
72 $methodNode->setStatic($tag->isStatic());
73 $node->addMethod($methodNode);
74 }
75 }
76 }
77 }
78
79 /**
80 * Returns patch priority, which determines when patch will be applied.
81 *
82 * @return integer Priority number (higher - earlier)
83 */
84 public function getPriority()
85 {
86 return 50;
87 }
88 }
89