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 = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Parent class name, can be changed manually |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $parentClassName = null; |
||
| 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) |
|
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * Updates parent name for child |
||
| 174 | * |
||
| 175 | * @param string $newParentName New class name |
||
| 176 | */ |
||
| 177 | 6 | public function setParentName(string $newParentName) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Override parent method with new body |
||
| 184 | * |
||
| 185 | * @param string $methodName Method name to override |
||
| 186 | * @param string $body New body for method |
||
| 187 | */ |
||
| 188 | 6 | public function override(string $methodName, string $body) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Creates a method |
||
| 195 | * |
||
| 196 | * @param int $methodFlags See ReflectionMethod modifiers |
||
| 197 | * @param string $methodName Name of the method |
||
| 198 | * @param bool $byReference Is method should return value by reference |
||
| 199 | * @param string $body Body of method |
||
| 200 | * @param string $parameters Definition of parameters |
||
| 201 | */ |
||
| 202 | public function setMethod(int $methodFlags, string $methodName, bool $byReference, string $body, string $parameters) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Inject advices into given class |
||
| 221 | * |
||
| 222 | * NB This method will be used as a callback during source code evaluation to inject joinpoints |
||
| 223 | * |
||
| 224 | * @param string $className Aop child proxy class |
||
| 225 | * @param array|Advice[] $advices List of advices to inject into class |
||
| 226 | */ |
||
| 227 | public static function injectJoinPoints(string $className, array $advices = []) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Wrap advices with joinpoint object |
||
| 244 | * |
||
| 245 | * @param array|Advice[] $classAdvices Advices for specific class |
||
| 246 | * @param string $className Name of the original class to use |
||
| 247 | * |
||
| 248 | * @throws \UnexpectedValueException If joinPoint type is unknown |
||
| 249 | * |
||
| 250 | * NB: Extension should be responsible for wrapping advice with join point. |
||
| 251 | * |
||
| 252 | * @return array|Joinpoint[] returns list of joinpoint ready to use |
||
| 253 | */ |
||
| 254 | protected static function wrapWithJoinPoints(array $classAdvices, string $className): array |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Add an interface for child |
||
| 287 | * |
||
| 288 | * @param string $interfaceName Name of the interface to add |
||
| 289 | */ |
||
| 290 | 6 | public function addInterface(string $interfaceName) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Add a trait for child |
||
| 298 | * |
||
| 299 | * @param string $traitName Name of the trait to add |
||
| 300 | */ |
||
| 301 | public function addTrait(string $traitName) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Creates a property |
||
| 309 | * |
||
| 310 | * @param int $propFlags See ReflectionProperty modifiers |
||
| 311 | * @param string $propName Name of the property |
||
| 312 | * @param null|string $defaultText Default value, should be string text! |
||
| 313 | */ |
||
| 314 | 6 | public function setProperty(int $propFlags, string $propName, string $defaultText = null) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Adds a definition for joinpoints private property in the class |
||
| 328 | */ |
||
| 329 | 6 | protected function addJoinpointsProperty() |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Override parent method with joinpoint invocation |
||
| 340 | * |
||
| 341 | * @param ReflectionMethod $method Method reflection |
||
| 342 | */ |
||
| 343 | 6 | protected function overrideMethod(ReflectionMethod $method) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Creates definition for method body |
||
| 353 | * |
||
| 354 | * @param ReflectionMethod $method Method reflection |
||
| 355 | * |
||
| 356 | * @return string new method body |
||
| 357 | */ |
||
| 358 | 6 | protected function getJoinpointInvocationBody(ReflectionMethod $method): string |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Makes property intercepted |
||
| 385 | * |
||
| 386 | * @param ReflectionProperty $property Reflection of property to intercept |
||
| 387 | */ |
||
| 388 | protected function interceptProperty(ReflectionProperty $property) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * {@inheritDoc} |
||
| 396 | */ |
||
| 397 | 6 | public function __toString() |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Add code for intercepting properties |
||
| 431 | * |
||
| 432 | * @param null|ReflectionMethod $constructor Constructor reflection or null |
||
| 433 | */ |
||
| 434 | protected function addFieldInterceptorsCode(ReflectionMethod $constructor = null) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Returns constructor code |
||
| 447 | * |
||
| 448 | * @param ReflectionMethod $constructor Constructor reflection |
||
| 449 | * @param bool $isCallParent Is there is a need to call parent code |
||
| 450 | * |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | private function getConstructorBody(ReflectionMethod $constructor = null, bool $isCallParent = false): string |
||
| 481 | } |
||
| 482 |