1 | <?php |
||
19 | abstract class AbstractProxy |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * Indent for source code |
||
24 | * |
||
25 | * @var int |
||
26 | */ |
||
27 | protected $indent = 4; |
||
28 | |||
29 | /** |
||
30 | * List of advices that are used for generation of child |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $advices = []; |
||
35 | |||
36 | /** |
||
37 | * PHP expression string for accessing LSB information |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected static $staticLsbExpression = 'static::class'; |
||
42 | |||
43 | /** |
||
44 | * Should proxy use variadics support or not |
||
45 | * |
||
46 | * @var bool |
||
47 | */ |
||
48 | protected $useVariadics = false; |
||
49 | |||
50 | /** |
||
51 | * Constructs an abstract proxy class |
||
52 | * |
||
53 | * @param array $advices List of advices |
||
54 | * @param bool $useVariadics Should proxy use variadics syntax or not |
||
55 | */ |
||
56 | 5 | public function __construct(array $advices = [], $useVariadics = false) |
|
61 | |||
62 | /** |
||
63 | * Returns text representation of class |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | abstract public function __toString(); |
||
68 | |||
69 | /** |
||
70 | * Indent block of code |
||
71 | * |
||
72 | * @param string $text Non-indented text |
||
73 | * |
||
74 | * @return string Indented text |
||
75 | */ |
||
76 | 5 | protected function indent($text) |
|
85 | |||
86 | /** |
||
87 | * Returns list of string representation of parameters |
||
88 | * |
||
89 | * @param array|ReflectionParameter[] $parameters List of parameters |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | 5 | protected function getParameters(array $parameters) |
|
106 | |||
107 | /** |
||
108 | * Return string representation of parameter |
||
109 | * |
||
110 | * @param ReflectionParameter $parameter Reflection parameter |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | 2 | protected function getParameterCode(ReflectionParameter $parameter) |
|
115 | { |
||
116 | 2 | $type = ''; |
|
117 | 2 | if ($parameter->isArray()) { |
|
118 | $type = 'array'; |
||
119 | 2 | } elseif ($parameter->isCallable()) { |
|
120 | $type = 'callable'; |
||
121 | 2 | } elseif ($parameter->getClass()) { |
|
122 | $type = '\\' . $parameter->getClass()->name; |
||
123 | } |
||
124 | 2 | $defaultValue = null; |
|
125 | 2 | $isDefaultValueAvailable = $parameter->isDefaultValueAvailable(); |
|
126 | 2 | if ($isDefaultValueAvailable) { |
|
127 | 2 | $defaultValue = var_export($parameter->getDefaultValue(), true); |
|
128 | 2 | } elseif ($parameter->isOptional()) { |
|
129 | $defaultValue = 'null'; |
||
130 | } |
||
131 | $code = ( |
||
132 | 2 | ($type ? "$type " : '') . // Typehint |
|
133 | 2 | ($parameter->isPassedByReference() ? '&' : '') . // By reference sign |
|
134 | 2 | ($this->useVariadics && $parameter->isVariadic() ? '...' : '') . // Variadic symbol |
|
1 ignored issue
–
show
|
|||
135 | 2 | '$' . // Variable symbol |
|
136 | 2 | ($parameter->name) . // Name of the argument |
|
137 | 2 | ($defaultValue !== null ? (" = " . $defaultValue) : '') // Default value if present |
|
138 | ); |
||
139 | |||
140 | 2 | return $code; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Replace concrete advices with list of ids |
||
145 | * |
||
146 | * @param $advices |
||
147 | * |
||
148 | * @return array flatten list of advices |
||
149 | */ |
||
150 | 5 | private function flattenAdvices($advices) |
|
163 | |||
164 | /** |
||
165 | * Prepares a line with args from the method definition |
||
166 | * |
||
167 | * @param ReflectionMethod $method |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | protected function prepareArgsLine(ReflectionMethod $method) |
||
181 | } |
||
182 |
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: