Skip to content

Commit c8942f0

Browse files
committed
Add tag retriever
1 parent bc75bea commit c8942f0

File tree

5 files changed

+187
-63
lines changed

5 files changed

+187
-63
lines changed

src/Prophecy/Doubler/ClassPatch/MagicCallPatch.php

+6-63
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,10 @@
1111

1212
namespace Prophecy\Doubler\ClassPatch;
1313

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;
2214
use Prophecy\Doubler\Generator\Node\ClassNode;
2315
use Prophecy\Doubler\Generator\Node\MethodNode;
16+
use Prophecy\PhpDocumentor\ClassAndInterfaceTagRetriever;
17+
use Prophecy\PhpDocumentor\MethodTagRetrieverInterface;
2418

2519
/**
2620
* Discover Magical API using "@method" PHPDoc format.
@@ -31,22 +25,11 @@
3125
*/
3226
class MagicCallPatch implements ClassPatchInterface
3327
{
34-
/**
35-
* @var DocBlockFactory|null
36-
*/
37-
private $docBlockFactory;
38-
39-
/**
40-
* @var ContextFactory|null
41-
*/
42-
private $contextFactory;
28+
private $tagRetriever;
4329

44-
public function __construct()
30+
public function __construct(MethodTagRetrieverInterface $tagRetriever = null)
4531
{
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;
5033
}
5134

5235
/**
@@ -71,13 +54,9 @@ public function apply(ClassNode $node)
7154
$parentClass = $node->getParentClass();
7255
$reflectionClass = new \ReflectionClass($parentClass);
7356

74-
$tagList = array_merge(
75-
$this->getClassTagList($reflectionClass),
76-
$this->getClassInterfacesTagList($reflectionClass)
77-
);
57+
$tagList = $this->tagRetriever->getTagList($reflectionClass);
7858

7959
foreach($tagList as $tag) {
80-
/* @var LegacyMethodTag|Method $tag */
8160
$methodName = $tag->getMethodName();
8261

8362
if (empty($methodName)) {
@@ -102,41 +81,5 @@ public function getPriority()
10281
{
10382
return 50;
10483
}
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-
}
14184
}
14285

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Prophecy.
5+
* (c) Konstantin Kudryashov <[email protected]>
6+
* Marcello Duarte <[email protected]>
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\PhpDocumentor;
13+
14+
use phpDocumentor\Reflection\DocBlock\Tag\MethodTag as LegacyMethodTag;
15+
use phpDocumentor\Reflection\DocBlock\Tags\Method;
16+
17+
/**
18+
* @author Théo FIDRY <[email protected]>
19+
*/
20+
final class ClassAndInterfaceTagRetriever implements MethodTagRetrieverInterface
21+
{
22+
/**
23+
* @var MethodTagRetrieverInterface
24+
*/
25+
private $classRetriever;
26+
27+
public function __construct(MethodTagRetrieverInterface $classRetriever = null)
28+
{
29+
if (null !== $classRetriever) {
30+
$this->classRetriever = $classRetriever;
31+
32+
return;
33+
}
34+
35+
$this->classRetriever = (class_exists('phpDocumentor\Reflection\DocBlockFactory') && class_exists('phpDocumentor\Reflection\Types\ContextFactory'))
36+
? new ClassTagRetriever()
37+
: new LegacyClassTagRetriever()
38+
;
39+
}
40+
41+
/**
42+
* @param \ReflectionClass $reflectionClass
43+
*
44+
* @return LegacyMethodTag[]|Method[]
45+
*/
46+
public function getTagList(\ReflectionClass $reflectionClass)
47+
{
48+
return array_merge(
49+
$this->getTagList($reflectionClass),
50+
$this->getInterfacesTagList($reflectionClass)
51+
);
52+
}
53+
54+
/**
55+
* @param \ReflectionClass $reflectionClass
56+
*
57+
* @return LegacyMethodTag[]|Method[]
58+
*/
59+
private function getInterfacesTagList(\ReflectionClass $reflectionClass)
60+
{
61+
$interfaces = $reflectionClass->getInterfaces();
62+
$tagList = array();
63+
64+
foreach($interfaces as $interface) {
65+
$tagList = array_merge($tagList, $this->getTagList($interface));
66+
}
67+
68+
return $tagList;
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Prophecy.
5+
* (c) Konstantin Kudryashov <[email protected]>
6+
* Marcello Duarte <[email protected]>
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\PhpDocumentor;
13+
14+
use phpDocumentor\Reflection\DocBlock\Tags\Method;
15+
use phpDocumentor\Reflection\DocBlockFactory;
16+
use phpDocumentor\Reflection\Types\ContextFactory;
17+
18+
/**
19+
* @author Théo FIDRY <[email protected]>
20+
*/
21+
final class ClassTagRetriever implements MethodTagRetrieverInterface
22+
{
23+
private $docBlockFactory;
24+
private $contextFactory;
25+
26+
public function __construct()
27+
{
28+
$this->docBlockFactory = DocBlockFactory::createInstance();
29+
$this->contextFactory = new ContextFactory();
30+
}
31+
32+
/**
33+
* @param \ReflectionClass $reflectionClass
34+
*
35+
* @return Method[]
36+
*/
37+
public function getTagList(\ReflectionClass $reflectionClass)
38+
{
39+
try {
40+
$phpdoc = $this->docBlockFactory->create(
41+
$reflectionClass,
42+
$this->contextFactory->createFromReflector($reflectionClass)
43+
);
44+
45+
return $phpdoc->getTagsByName('method');
46+
} catch (\InvalidArgumentException $e) {
47+
return array();
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Prophecy.
5+
* (c) Konstantin Kudryashov <[email protected]>
6+
* Marcello Duarte <[email protected]>
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\PhpDocumentor;
13+
14+
use phpDocumentor\Reflection\DocBlock;
15+
use phpDocumentor\Reflection\DocBlock\Tag\MethodTag as LegacyMethodTag;
16+
17+
/**
18+
* @author Théo FIDRY <[email protected]>
19+
*/
20+
final class LegacyClassTagRetriever implements MethodTagRetrieverInterface
21+
{
22+
/**
23+
* @param \ReflectionClass $reflectionClass
24+
*
25+
* @return LegacyMethodTag[]
26+
*/
27+
public function getTagList(\ReflectionClass $reflectionClass)
28+
{
29+
$phpdoc = new DocBlock($reflectionClass->getDocComment());
30+
31+
return $phpdoc->getTagsByName('method');
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Prophecy.
5+
* (c) Konstantin Kudryashov <[email protected]>
6+
* Marcello Duarte <[email protected]>
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\PhpDocumentor;
13+
14+
use phpDocumentor\Reflection\DocBlock\Tag\MethodTag as LegacyMethodTag;
15+
use phpDocumentor\Reflection\DocBlock\Tags\Method;
16+
17+
/**
18+
* @author Théo FIDRY <[email protected]>
19+
*/
20+
interface MethodTagRetrieverInterface
21+
{
22+
/**
23+
* @param \ReflectionClass $reflectionClass
24+
*
25+
* @return LegacyMethodTag[]|Method[]
26+
*/
27+
public function getTagList(\ReflectionClass $reflectionClass);
28+
}

0 commit comments

Comments
 (0)