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 |
||
34 | class ClassProxy extends AbstractProxy |
||
35 | { |
||
36 | /** |
||
37 | * Parent class reflection |
||
38 | * |
||
39 | * @var null|ReflectionClass |
||
40 | */ |
||
41 | protected $class; |
||
42 | |||
43 | /** |
||
44 | * Parent class name, can be changed manually |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $parentClassName; |
||
49 | |||
50 | /** |
||
51 | * Source code for methods |
||
52 | * |
||
53 | * @var array Name of method => source code for it |
||
54 | */ |
||
55 | protected $methodsCode = []; |
||
56 | |||
57 | /** |
||
58 | * Static mappings for class name for excluding if..else check |
||
59 | * |
||
60 | * @var null|array |
||
61 | */ |
||
62 | protected static $invocationClassMap = [ |
||
63 | AspectContainer::METHOD_PREFIX => DynamicClosureMethodInvocation::class, |
||
64 | AspectContainer::STATIC_METHOD_PREFIX => StaticClosureMethodInvocation::class, |
||
65 | AspectContainer::PROPERTY_PREFIX => ClassFieldAccess::class, |
||
66 | AspectContainer::STATIC_INIT_PREFIX => StaticInitializationJoinpoint::class, |
||
67 | AspectContainer::INIT_PREFIX => ReflectionConstructorInvocation::class |
||
68 | ]; |
||
69 | |||
70 | /** |
||
71 | * List of additional interfaces to implement |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $interfaces = []; |
||
76 | |||
77 | /** |
||
78 | * List of additional traits for using |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $traits = []; |
||
83 | |||
84 | /** |
||
85 | * Source code for properties |
||
86 | * |
||
87 | * @var array Name of property => source code for it |
||
88 | */ |
||
89 | protected $propertiesCode = []; |
||
90 | |||
91 | /** |
||
92 | * Name for the current class |
||
93 | * |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $name = ''; |
||
97 | |||
98 | /** |
||
99 | * Flag to determine if we need to add a code for property interceptors |
||
100 | * |
||
101 | * @var bool |
||
102 | */ |
||
103 | private $isFieldsIntercepted = false; |
||
104 | |||
105 | /** |
||
106 | * List of intercepted properties names |
||
107 | * |
||
108 | * @var array |
||
109 | */ |
||
110 | private $interceptedProperties = []; |
||
111 | |||
112 | /** |
||
113 | * Generates an child code by parent class reflection and joinpoints for it |
||
114 | * |
||
115 | * @param ReflectionClass $parent Parent class reflection |
||
116 | * @param array|Advice[][] $classAdvices List of advices for class |
||
117 | * |
||
118 | * @throws \InvalidArgumentException if there are unknown type of advices |
||
119 | */ |
||
120 | 6 | public function __construct(ReflectionClass $parent, array $classAdvices) |
|
171 | |||
172 | |||
173 | /** |
||
174 | * Updates parent name for child |
||
175 | * |
||
176 | * @param string $newParentName New class name |
||
177 | */ |
||
178 | 6 | public function setParentName(string $newParentName) |
|
182 | |||
183 | /** |
||
184 | * Override parent method with new body |
||
185 | * |
||
186 | * @param string $methodName Method name to override |
||
187 | * @param string $body New body for method |
||
188 | */ |
||
189 | 6 | public function override(string $methodName, string $body) |
|
193 | |||
194 | /** |
||
195 | * Creates a method |
||
196 | * |
||
197 | * @param int $methodFlags See ReflectionMethod modifiers |
||
198 | * @param string $methodName Name of the method |
||
199 | * @param bool $byReference Is method should return value by reference |
||
200 | * @param string $body Body of method |
||
201 | * @param string $parameters Definition of parameters |
||
202 | */ |
||
203 | public function setMethod(int $methodFlags, string $methodName, bool $byReference, string $body, string $parameters) |
||
219 | |||
220 | /** |
||
221 | * Inject advices into given class |
||
222 | * |
||
223 | * NB This method will be used as a callback during source code evaluation to inject joinpoints |
||
224 | * |
||
225 | * @param string $className Aop child proxy class |
||
226 | */ |
||
227 | public static function injectJoinPoints(string $className) |
||
242 | |||
243 | /** |
||
244 | * Wrap advices with joinpoint object |
||
245 | * |
||
246 | * @param array|Advice[] $classAdvices Advices for specific class |
||
247 | * @param string $className Name of the original class to use |
||
248 | * |
||
249 | * @throws \UnexpectedValueException If joinPoint type is unknown |
||
250 | * |
||
251 | * NB: Extension should be responsible for wrapping advice with join point. |
||
252 | * |
||
253 | * @return array|Joinpoint[] returns list of joinpoint ready to use |
||
254 | */ |
||
255 | protected static function wrapWithJoinPoints(array $classAdvices, string $className): array |
||
285 | |||
286 | /** |
||
287 | * Add an interface for child |
||
288 | * |
||
289 | * @param string $interfaceName Name of the interface to add |
||
290 | */ |
||
291 | 6 | public function addInterface(string $interfaceName) |
|
296 | |||
297 | /** |
||
298 | * Add a trait for child |
||
299 | * |
||
300 | * @param string $traitName Name of the trait to add |
||
301 | */ |
||
302 | public function addTrait(string $traitName) |
||
307 | |||
308 | /** |
||
309 | * Creates a property |
||
310 | * |
||
311 | * @param int $propFlags See ReflectionProperty modifiers |
||
312 | * @param string $propName Name of the property |
||
313 | * @param null|string $defaultText Default value, should be string text! |
||
314 | */ |
||
315 | 6 | public function setProperty(int $propFlags, string $propName, string $defaultText = null) |
|
326 | |||
327 | /** |
||
328 | * Adds a definition for joinpoints private property in the class |
||
329 | */ |
||
330 | 6 | protected function addJoinpointsProperty() |
|
344 | |||
345 | /** |
||
346 | * Override parent method with joinpoint invocation |
||
347 | * |
||
348 | * @param ReflectionMethod $method Method reflection |
||
349 | */ |
||
350 | 6 | protected function overrideMethod(ReflectionMethod $method) |
|
357 | |||
358 | /** |
||
359 | * Creates definition for method body |
||
360 | * |
||
361 | * @param ReflectionMethod $method Method reflection |
||
362 | * |
||
363 | * @return string new method body |
||
364 | */ |
||
365 | 6 | protected function getJoinpointInvocationBody(ReflectionMethod $method): string |
|
389 | |||
390 | /** |
||
391 | * Makes property intercepted |
||
392 | * |
||
393 | * @param ReflectionProperty $property Reflection of property to intercept |
||
394 | */ |
||
395 | protected function interceptProperty(ReflectionProperty $property) |
||
400 | |||
401 | /** |
||
402 | * {@inheritDoc} |
||
403 | */ |
||
404 | public function __toString() |
||
433 | |||
434 | /** |
||
435 | * Add code for intercepting properties |
||
436 | * |
||
437 | * @param null|ReflectionMethod $constructor Constructor reflection or null |
||
438 | */ |
||
439 | protected function addFieldInterceptorsCode(ReflectionMethod $constructor = null) |
||
449 | |||
450 | /** |
||
451 | * Returns constructor code |
||
452 | * |
||
453 | * @param ReflectionMethod $constructor Constructor reflection |
||
454 | * @param bool $isCallParent Is there is a need to call parent code |
||
455 | * |
||
456 | * @return string |
||
457 | */ |
||
458 | private function getConstructorBody(ReflectionMethod $constructor = null, bool $isCallParent = false): string |
||
485 | } |
||
486 |