1 | <?php |
||
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) |
||
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() |
||
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: