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\Doubler\ClassPatch; |
13
|
|
|
|
14
|
|
|
use phpDocumentor\Reflection\DocBlock; |
15
|
|
|
use Prophecy\Doubler\Generator\Node\ClassNode; |
16
|
|
|
use Prophecy\Doubler\Generator\Node\MethodNode; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Discover Magical API using "@method" PHPDoc format. |
20
|
|
|
* |
21
|
|
|
* @author Thomas Tourlourat <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class MagicCallPatch implements ClassPatchInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Support any class |
27
|
|
|
* |
28
|
|
|
* @param ClassNode $node |
29
|
|
|
* |
30
|
|
|
* @return boolean |
31
|
|
|
*/ |
32
|
|
|
public function supports(ClassNode $node) |
33
|
|
|
{ |
34
|
|
|
return true; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Discover Magical API |
39
|
|
|
* |
40
|
|
|
* @param ClassNode $node |
41
|
|
|
*/ |
42
|
|
|
public function apply(ClassNode $node) |
43
|
|
|
{ |
44
|
|
|
$parentClass = $node->getParentClass(); |
45
|
|
|
$reflectionClass = new \ReflectionClass($parentClass); |
46
|
|
|
|
47
|
|
|
$phpdoc = new DocBlock($reflectionClass->getDocComment()); |
48
|
|
|
|
49
|
|
|
$tagList = $phpdoc->getTagsByName('method'); |
50
|
|
|
|
51
|
|
|
$interfaces = $reflectionClass->getInterfaces(); |
52
|
|
|
foreach($interfaces as $interface) { |
53
|
|
|
$phpdoc = new DocBlock($interface); |
54
|
|
|
$tagList = array_merge($tagList, $phpdoc->getTagsByName('method')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
foreach($tagList as $tag) { |
58
|
|
|
$methodName = $tag->getMethodName(); |
|
|
|
|
59
|
|
|
|
60
|
|
|
if (!$reflectionClass->hasMethod($methodName)) { |
61
|
|
|
$methodNode = new MethodNode($tag->getMethodName()); |
|
|
|
|
62
|
|
|
$methodNode->setStatic($tag->isStatic()); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$node->addMethod($methodNode); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns patch priority, which determines when patch will be applied. |
71
|
|
|
* |
72
|
|
|
* @return integer Priority number (higher - earlier) |
73
|
|
|
*/ |
74
|
|
|
public function getPriority() |
75
|
|
|
{ |
76
|
|
|
return 50; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: