Complex classes like ClassProxy often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ClassProxy, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class ClassProxy extends AbstractProxy |
||
33 | { |
||
34 | /** |
||
35 | * Parent class reflection |
||
36 | * |
||
37 | * @var null|ReflectionClass |
||
38 | */ |
||
39 | protected $class = null; |
||
40 | |||
41 | /** |
||
42 | * Parent class name, can be changed manually |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $parentClassName = null; |
||
47 | |||
48 | /** |
||
49 | * Source code for methods |
||
50 | * |
||
51 | * @var array Name of method => source code for it |
||
52 | */ |
||
53 | protected $methodsCode = []; |
||
54 | |||
55 | /** |
||
56 | * Static mappings for class name for excluding if..else check |
||
57 | * |
||
58 | * @var null|array |
||
59 | */ |
||
60 | protected static $invocationClassMap = [ |
||
61 | AspectContainer::METHOD_PREFIX => DynamicClosureMethodInvocation::class, |
||
62 | AspectContainer::STATIC_METHOD_PREFIX => StaticClosureMethodInvocation::class, |
||
63 | AspectContainer::PROPERTY_PREFIX => ClassFieldAccess::class, |
||
64 | AspectContainer::STATIC_INIT_PREFIX => StaticInitializationJoinpoint::class, |
||
65 | AspectContainer::INIT_PREFIX => ReflectionConstructorInvocation::class |
||
66 | ]; |
||
67 | |||
68 | /** |
||
69 | * List of additional interfaces to implement |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $interfaces = []; |
||
74 | |||
75 | /** |
||
76 | * List of additional traits for using |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $traits = []; |
||
81 | |||
82 | /** |
||
83 | * Source code for properties |
||
84 | * |
||
85 | * @var array Name of property => source code for it |
||
86 | */ |
||
87 | protected $propertiesCode = []; |
||
88 | |||
89 | /** |
||
90 | * Name for the current class |
||
91 | * |
||
92 | * @var string |
||
93 | */ |
||
94 | protected $name = ''; |
||
95 | |||
96 | /** |
||
97 | * Flag to determine if we need to add a code for property interceptors |
||
98 | * |
||
99 | * @var bool |
||
100 | */ |
||
101 | private $isFieldsIntercepted = false; |
||
102 | |||
103 | /** |
||
104 | * List of intercepted properties names |
||
105 | * |
||
106 | * @var array |
||
107 | */ |
||
108 | private $interceptedProperties = []; |
||
109 | |||
110 | /** |
||
111 | * Generates an child code by parent class reflection and joinpoints for it |
||
112 | * |
||
113 | * @param ReflectionClass $parent Parent class reflection |
||
114 | * @param array|Advice[] $classAdvices List of advices for class |
||
115 | * |
||
116 | * @throws \InvalidArgumentException if there are unknown type of advices |
||
117 | */ |
||
118 | 6 | public function __construct(ReflectionClass $parent, array $classAdvices) |
|
119 | { |
||
120 | 6 | parent::__construct($classAdvices); |
|
121 | |||
122 | 6 | $this->class = $parent; |
|
123 | 6 | $this->name = $parent->getShortName(); |
|
124 | 6 | $this->parentClassName = $parent->getShortName(); |
|
125 | |||
126 | 6 | $this->addInterface('\Go\Aop\Proxy'); |
|
127 | 6 | $this->addJoinpointsProperty(); |
|
128 | |||
129 | 6 | foreach ($classAdvices as $type => $typedAdvices) { |
|
|
|||
130 | |||
131 | switch ($type) { |
||
132 | 6 | case AspectContainer::METHOD_PREFIX: |
|
133 | case AspectContainer::STATIC_METHOD_PREFIX: |
||
134 | 6 | foreach ($typedAdvices as $joinPointName => $advice) { |
|
135 | 6 | $method = $parent->getMethod($joinPointName); |
|
136 | 6 | $this->overrideMethod($method); |
|
137 | } |
||
138 | 6 | break; |
|
139 | |||
140 | case AspectContainer::PROPERTY_PREFIX: |
||
141 | foreach ($typedAdvices as $joinPointName => $advice) { |
||
142 | $property = $parent->getProperty($joinPointName); |
||
143 | $this->interceptProperty($property); |
||
144 | } |
||
145 | break; |
||
146 | |||
147 | case AspectContainer::INTRODUCTION_TRAIT_PREFIX: |
||
148 | foreach ($typedAdvices as $advice) { |
||
149 | /** @var $advice IntroductionInfo */ |
||
150 | foreach ($advice->getInterfaces() as $interface) { |
||
151 | $this->addInterface($interface); |
||
152 | } |
||
153 | foreach ($advice->getTraits() as $trait) { |
||
154 | $this->addTrait($trait); |
||
155 | } |
||
156 | } |
||
157 | break; |
||
158 | |||
159 | case AspectContainer::INIT_PREFIX: |
||
160 | case AspectContainer::STATIC_INIT_PREFIX: |
||
161 | break; // No changes for class |
||
162 | |||
163 | default: |
||
164 | 6 | throw new \InvalidArgumentException("Unsupported point `$type`"); |
|
165 | } |
||
166 | } |
||
167 | 6 | } |
|
168 | |||
169 | |||
170 | /** |
||
171 | * Updates parent name for child |
||
172 | * |
||
173 | * @param string $newParentName New class name |
||
174 | * |
||
175 | * @return static |
||
176 | */ |
||
177 | 6 | public function setParentName($newParentName) |
|
183 | |||
184 | /** |
||
185 | * Override parent method with new body |
||
186 | * |
||
187 | * @param string $methodName Method name to override |
||
188 | * @param string $body New body for method |
||
189 | * |
||
190 | * @return static |
||
191 | */ |
||
192 | 6 | public function override($methodName, $body) |
|
198 | |||
199 | /** |
||
200 | * Creates a method |
||
201 | * |
||
202 | * @param int $methodFlags See ReflectionMethod modifiers |
||
203 | * @param string $methodName Name of the method |
||
204 | * @param bool $byReference Is method should return value by reference |
||
205 | * @param string $body Body of method |
||
206 | * @param string $parameters Definition of parameters |
||
207 | * |
||
208 | * @return static |
||
209 | */ |
||
210 | public function setMethod($methodFlags, $methodName, $byReference, $body, $parameters) |
||
211 | { |
||
212 | $this->methodsCode[$methodName] = ( |
||
213 | "/**\n * Method was created automatically, do not change it manually\n */\n" . |
||
214 | join(' ', Reflection::getModifierNames($methodFlags)) . // List of method modifiers |
||
215 | ' function ' . // 'function' keyword |
||
216 | ($byReference ? '&' : '') . // Return value by reference |
||
217 | $methodName . // Method name |
||
218 | '(' . // Start of parameter list |
||
219 | $parameters . // List of parameters |
||
220 | ")\n" . // End of parameter list |
||
221 | "{\n" . // Start of method body |
||
222 | $this->indent($body) . "\n" . // Method body |
||
223 | "}\n" // End of method body |
||
224 | ); |
||
225 | |||
226 | return $this; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Inject advices into given class |
||
231 | * |
||
232 | * NB This method will be used as a callback during source code evaluation to inject joinpoints |
||
233 | * |
||
234 | * @param string $className Aop child proxy class |
||
235 | * @param array|Advice[] $advices List of advices to inject into class |
||
236 | * |
||
237 | * @return void |
||
238 | */ |
||
239 | public static function injectJoinPoints($className, array $advices = []) |
||
240 | { |
||
241 | $reflectionClass = new ReflectionClass($className); |
||
242 | $joinPoints = static::wrapWithJoinPoints($advices, $reflectionClass->getParentClass()->name); |
||
243 | |||
244 | $prop = $reflectionClass->getProperty('__joinPoints'); |
||
245 | $prop->setAccessible(true); |
||
246 | $prop->setValue($joinPoints); |
||
247 | |||
248 | $staticInit = AspectContainer::STATIC_INIT_PREFIX . ':root'; |
||
249 | if (isset($joinPoints[$staticInit])) { |
||
250 | $joinPoints[$staticInit]->__invoke(); |
||
251 | } |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Wrap advices with joinpoint object |
||
256 | * |
||
257 | * @param array|Advice[] $classAdvices Advices for specific class |
||
258 | * @param string $className Name of the original class to use |
||
259 | * |
||
260 | * @throws \UnexpectedValueException If joinPoint type is unknown |
||
261 | * |
||
262 | * NB: Extension should be responsible for wrapping advice with join point. |
||
263 | * |
||
264 | * @return array|Joinpoint[] returns list of joinpoint ready to use |
||
265 | */ |
||
266 | protected static function wrapWithJoinPoints($classAdvices, $className) |
||
267 | { |
||
268 | /** @var LazyAdvisorAccessor $accessor */ |
||
269 | static $accessor = null; |
||
270 | |||
271 | if (!isset($accessor)) { |
||
272 | $aspectKernel = AspectKernel::getInstance(); |
||
273 | $accessor = $aspectKernel->getContainer()->get('aspect.advisor.accessor'); |
||
274 | } |
||
275 | |||
276 | $joinPoints = []; |
||
277 | |||
278 | foreach ($classAdvices as $joinPointType => $typedAdvices) { |
||
279 | // if not isset then we don't want to create such invocation for class |
||
280 | if (!isset(self::$invocationClassMap[$joinPointType])) { |
||
281 | continue; |
||
282 | } |
||
283 | foreach ($typedAdvices as $joinPointName => $advices) { |
||
284 | $filledAdvices = []; |
||
285 | foreach ($advices as $advisorName) { |
||
286 | $filledAdvices[] = $accessor->$advisorName; |
||
287 | } |
||
288 | |||
289 | $joinpoint = new self::$invocationClassMap[$joinPointType]($className, $joinPointName, $filledAdvices); |
||
290 | $joinPoints["$joinPointType:$joinPointName"] = $joinpoint; |
||
291 | } |
||
292 | } |
||
293 | |||
294 | return $joinPoints; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Add an interface for child |
||
299 | * |
||
300 | * @param string|ReflectionClass $interface |
||
301 | * |
||
302 | * @throws \InvalidArgumentException If object is not an interface |
||
303 | */ |
||
304 | 6 | public function addInterface($interface) |
|
316 | |||
317 | /** |
||
318 | * Add a trait for child |
||
319 | * |
||
320 | * @param string|ReflectionClass $trait |
||
321 | * |
||
322 | * @throws \InvalidArgumentException If object is not a trait |
||
323 | */ |
||
324 | public function addTrait($trait) |
||
336 | |||
337 | /** |
||
338 | * Creates a property |
||
339 | * |
||
340 | * @param int $propFlags See ReflectionProperty modifiers |
||
341 | * @param string $propName Name of the property |
||
342 | * @param null|string $defaultText Default value, should be string text! |
||
343 | * |
||
344 | * @return static |
||
345 | */ |
||
346 | 6 | public function setProperty($propFlags, $propName, $defaultText = null) |
|
359 | |||
360 | /** |
||
361 | * Adds a definition for joinpoints private property in the class |
||
362 | * |
||
363 | * @return void |
||
364 | */ |
||
365 | 6 | protected function addJoinpointsProperty() |
|
373 | |||
374 | /** |
||
375 | * Override parent method with joinpoint invocation |
||
376 | * |
||
377 | * @param ReflectionMethod $method Method reflection |
||
378 | */ |
||
379 | 6 | protected function overrideMethod(ReflectionMethod $method) |
|
386 | |||
387 | /** |
||
388 | * Creates definition for method body |
||
389 | * |
||
390 | * @param ReflectionMethod $method Method reflection |
||
391 | * |
||
392 | * @return string new method body |
||
393 | */ |
||
394 | 6 | protected function getJoinpointInvocationBody(ReflectionMethod $method) |
|
418 | |||
419 | /** |
||
420 | * Makes property intercepted |
||
421 | * |
||
422 | * @param ReflectionProperty $property Reflection of property to intercept |
||
423 | */ |
||
424 | protected function interceptProperty(ReflectionProperty $property) |
||
429 | |||
430 | /** |
||
431 | * {@inheritDoc} |
||
432 | */ |
||
433 | 6 | public function __toString() |
|
464 | |||
465 | /** |
||
466 | * Add code for intercepting properties |
||
467 | * |
||
468 | * @param null|ReflectionMethod $constructor Constructor reflection or null |
||
469 | */ |
||
470 | protected function addFieldInterceptorsCode(ReflectionMethod $constructor = null) |
||
480 | |||
481 | /** |
||
482 | * Returns constructor code |
||
483 | * |
||
484 | * @param ReflectionMethod $constructor Constructor reflection |
||
485 | * @param bool $isCallParent Is there is a need to call parent code |
||
486 | * |
||
487 | * @return string |
||
488 | */ |
||
489 | private function getConstructorBody(ReflectionMethod $constructor = null, $isCallParent = false) |
||
517 | } |
||
518 |