| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Go! AOP framework | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright Copyright 2012, Lisachenko Alexander <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * This source file is subject to the license that is bundled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * with this source code in the file LICENSE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Go\Proxy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use ReflectionFunctionAbstract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use ReflectionParameter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Abstract class for building different proxies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |      * Constructs an abstract proxy class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param array $advices List of advices | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 5 |  |     public function __construct(array $advices = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 5 |  |         $this->advices = $this->flattenAdvices($advices); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Returns text representation of class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     abstract public function __toString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * Indent block of code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param string $text Non-indented text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return string Indented text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 5 |  |     protected function indent($text) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 5 |  |         $pad   = str_pad('', $this->indent, ' '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 5 |  |         $lines = array_map(function($line) use ($pad) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 5 |  |             return $pad . $line; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 5 |  |         }, explode("\n", $text)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 5 |  |         return join("\n", $lines); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * Returns list of string representation of parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param array|ReflectionParameter[] $parameters List of parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 5 |  |     protected function getParameters(array $parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 5 |  |         $parameterDefinitions = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 5 |  |         foreach ($parameters as $parameter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |             $parameterDefinitions[] = $this->getParameterCode($parameter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 5 |  |         return $parameterDefinitions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Return string representation of parameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param ReflectionParameter $parameter Reflection parameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |     protected function getParameterCode(ReflectionParameter $parameter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |         $type = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 2 |  |         if ($parameter->isArray()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $type = 'array'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  |         } elseif ($parameter->isCallable()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $type = 'callable'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |         } elseif ($parameter->getClass()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $type = '\\' . $parameter->getClass()->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  |         $defaultValue = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 2 |  |         $isDefaultValueAvailable = $parameter->isDefaultValueAvailable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |         if ($isDefaultValueAvailable) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  |             $defaultValue = var_export($parameter->getDefaultValue(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  |         } elseif ($parameter->isOptional()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             $defaultValue = 'null'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $code = ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 2 |  |             ($type ? "$type " : '') . // Typehint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  |             ($parameter->isPassedByReference() ? '&' : '') . // By reference sign | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 2 |  |             ($parameter->isVariadic() ? '...' : '') . // Variadic symbol | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 2 |  |             '$' . // Variable symbol | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 2 |  |             ($parameter->name) . // Name of the argument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 2 |  |             ($defaultValue !== null ? (" = " . $defaultValue) : '') // Default value if present | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 2 |  |         return $code; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * Replace concrete advices with list of ids | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param $advices | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @return array flatten list of advices | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 5 |  |     private function flattenAdvices($advices) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 5 |  |         $flattenAdvices = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 5 |  |         foreach ($advices as $type => $typedAdvices) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 5 |  |             foreach ($typedAdvices as $name => $concreteAdvices) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 5 |  |                 if (is_array($concreteAdvices)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 5 |  |                     $flattenAdvices[$type][$name] = array_keys($concreteAdvices); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 5 |  |         return $flattenAdvices; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * Prepares a line with args from the method definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @param ReflectionFunctionAbstract $functionLike | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 157 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 158 | 5 |  |     protected function prepareArgsLine(ReflectionFunctionAbstract $functionLike) | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 160 | 5 |  |         $argumentsPart = []; | 
            
                                                        
            
                                    
            
            
                | 161 | 5 |  |         $arguments     = []; | 
            
                                                        
            
                                    
            
            
                | 162 | 5 |  |         $hasOptionals  = false; | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 164 | 5 |  |         foreach ($functionLike->getParameters() as $parameter) { | 
            
                                                        
            
                                    
            
            
                | 165 | 2 |  |             $byReference  = ($parameter->isPassedByReference() && !$parameter->isVariadic()) ? '&' : ''; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 166 | 2 |  |             $hasOptionals = $hasOptionals || $parameter->isOptional(); | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 168 | 2 |  |             $arguments[] = $byReference . '$' . $parameter->name; | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 171 | 5 |  |         $isVariadic = $functionLike->isVariadic(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 172 | 5 |  |         if ($isVariadic) { | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |             $argumentsPart[] = array_pop($arguments); | 
            
                                                        
            
                                    
            
            
                | 174 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 175 | 5 |  |         if (!empty($arguments)) { | 
            
                                                        
            
                                    
            
            
                | 176 |  |  |             // Unshifting to keep correct order | 
            
                                                        
            
                                    
            
            
                | 177 | 2 |  |             $argumentLine = '[' . join(', ', $arguments) . ']'; | 
            
                                                        
            
                                    
            
            
                | 178 | 2 |  |             if ($hasOptionals) { | 
            
                                                        
            
                                    
            
            
                | 179 | 2 |  |                 $argumentLine = "\\array_slice($argumentLine, 0, \\func_num_args())"; | 
            
                                                        
            
                                    
            
            
                | 180 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 181 | 2 |  |             array_unshift($argumentsPart, $argumentLine); | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 184 | 5 |  |         return join(', ', $argumentsPart); | 
            
                                                        
            
                                    
            
            
                | 185 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 186 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 187 |  |  |  | 
            
                        
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: