11
11
12
12
namespace Prophecy \Doubler \ClassPatch ;
13
13
14
- use phpDocumentor \Reflection \DocBlock ;
15
- use phpDocumentor \Reflection \DocBlock \Tag ;
16
- use phpDocumentor \Reflection \DocBlock \Tag as LegacyTag ;
17
- use phpDocumentor \Reflection \DocBlock \Tag \MethodTag as LegacyMethodTag ;
18
- use phpDocumentor \Reflection \DocBlock \Tags \Method ;
19
- use phpDocumentor \Reflection \DocBlockFactory ;
20
- use phpDocumentor \Reflection \DocBlockFactoryInterface ;
21
- use phpDocumentor \Reflection \Types \ContextFactory ;
22
14
use Prophecy \Doubler \Generator \Node \ClassNode ;
23
15
use Prophecy \Doubler \Generator \Node \MethodNode ;
16
+ use Prophecy \PhpDocumentor \ClassAndInterfaceTagRetriever ;
17
+ use Prophecy \PhpDocumentor \MethodTagRetrieverInterface ;
24
18
25
19
/**
26
20
* Discover Magical API using "@method" PHPDoc format.
31
25
*/
32
26
class MagicCallPatch implements ClassPatchInterface
33
27
{
34
- /**
35
- * @var DocBlockFactory|null
36
- */
37
- private $ docBlockFactory ;
38
-
39
- /**
40
- * @var ContextFactory|null
41
- */
42
- private $ contextFactory ;
28
+ private $ tagRetriever ;
43
29
44
- public function __construct ()
30
+ public function __construct (MethodTagRetrieverInterface $ tagRetriever = null )
45
31
{
46
- if (class_exists ('phpDocumentor\Reflection\DocBlockFactory ' ) && class_exists ('phpDocumentor\Reflection\Types\ContextFactory ' )) {
47
- $ this ->docBlockFactory = DocBlockFactory::createInstance ();
48
- $ this ->contextFactory = new ContextFactory ();
49
- }
32
+ $ this ->tagRetriever = (null === $ tagRetriever ) ? new ClassAndInterfaceTagRetriever () : $ tagRetriever ;
50
33
}
51
34
52
35
/**
@@ -71,13 +54,9 @@ public function apply(ClassNode $node)
71
54
$ parentClass = $ node ->getParentClass ();
72
55
$ reflectionClass = new \ReflectionClass ($ parentClass );
73
56
74
- $ tagList = array_merge (
75
- $ this ->getClassTagList ($ reflectionClass ),
76
- $ this ->getClassInterfacesTagList ($ reflectionClass )
77
- );
57
+ $ tagList = $ this ->tagRetriever ->getTagList ($ reflectionClass );
78
58
79
59
foreach ($ tagList as $ tag ) {
80
- /* @var LegacyMethodTag|Method $tag */
81
60
$ methodName = $ tag ->getMethodName ();
82
61
83
62
if (empty ($ methodName )) {
@@ -102,41 +81,5 @@ public function getPriority()
102
81
{
103
82
return 50 ;
104
83
}
105
-
106
- /**
107
- * @param \ReflectionClass $reflectionClass
108
- *
109
- * @return LegacyTag[]
110
- */
111
- private function getClassInterfacesTagList (\ReflectionClass $ reflectionClass )
112
- {
113
- $ interfaces = $ reflectionClass ->getInterfaces ();
114
- $ tagList = array ();
115
-
116
- foreach ($ interfaces as $ interface ) {
117
- $ tagList = array_merge ($ tagList , $ this ->getClassTagList ($ interface ));
118
- }
119
-
120
- return $ tagList ;
121
- }
122
-
123
- /**
124
- * @param \ReflectionClass $reflectionClass
125
- *
126
- * @return LegacyMethodTag[]|Method[]
127
- */
128
- private function getClassTagList (\ReflectionClass $ reflectionClass )
129
- {
130
- try {
131
- $ phpdoc = (null === $ this ->docBlockFactory || null === $ this ->contextFactory )
132
- ? new DocBlock ($ reflectionClass ->getDocComment ())
133
- : $ this ->docBlockFactory ->create ($ reflectionClass , $ this ->contextFactory ->createFromReflector ($ reflectionClass ))
134
- ;
135
-
136
- return $ phpdoc ->getTagsByName ('method ' );
137
- } catch (\InvalidArgumentException $ e ) {
138
- return array ();
139
- }
140
- }
141
84
}
142
85
0 commit comments