1 | <?php |
||
21 | abstract class AbstractProxy |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Indent for source code |
||
26 | * |
||
27 | * @var int |
||
28 | */ |
||
29 | protected $indent = 4; |
||
30 | |||
31 | /** |
||
32 | * List of advices that are used for generation of child |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $advices = []; |
||
37 | |||
38 | /** |
||
39 | * PHP expression string for accessing LSB information |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected static $staticLsbExpression = 'static::class'; |
||
44 | |||
45 | /** |
||
46 | * Constructs an abstract proxy class |
||
47 | * |
||
48 | * @param array $advices List of advices |
||
49 | */ |
||
50 | 6 | public function __construct(array $advices = []) |
|
54 | |||
55 | /** |
||
56 | * Returns text representation of class |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | abstract public function __toString(); |
||
61 | |||
62 | /** |
||
63 | * Indent block of code |
||
64 | * |
||
65 | * @param string $text Non-indented text |
||
66 | * |
||
67 | * @return string Indented text |
||
68 | */ |
||
69 | 6 | protected function indent($text) |
|
78 | |||
79 | /** |
||
80 | * Returns list of string representation of parameters |
||
81 | * |
||
82 | * @param array|ReflectionParameter[] $parameters List of parameters |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | 6 | protected function getParameters(array $parameters) |
|
95 | |||
96 | /** |
||
97 | * Return string representation of parameter |
||
98 | * |
||
99 | * @param ReflectionParameter $parameter Reflection parameter |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | 3 | protected function getParameterCode(ReflectionParameter $parameter) |
|
139 | |||
140 | /** |
||
141 | * Replace concrete advices with list of ids |
||
142 | * |
||
143 | * @param $advices |
||
144 | * |
||
145 | * @return array flatten list of advices |
||
146 | */ |
||
147 | 6 | private function flattenAdvices($advices) |
|
160 | |||
161 | /** |
||
162 | * Prepares a line with args from the method definition |
||
163 | * |
||
164 | * @param ReflectionFunctionAbstract $functionLike |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | 6 | protected function prepareArgsLine(ReflectionFunctionAbstract $functionLike) |
|
196 | |||
197 | /** |
||
198 | * Creates a function code from Reflection |
||
199 | * |
||
200 | * @param ReflectionFunctionAbstract $functionLike Reflection for method |
||
201 | * @param string $body Body of method |
||
202 | * |
||
203 | * @return string |
||
204 | */ |
||
205 | 6 | protected function getOverriddenFunction(ReflectionFunctionAbstract $functionLike, $body) |
|
235 | } |
||
236 |
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: