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) |
|
| 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) |
||
| 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 = []) |
||
| 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) |
||
| 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) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Makes property intercepted |
||
| 414 | * |
||
| 415 | * @param ReflectionProperty $property Reflection of property to intercept |
||
| 416 | */ |
||
| 417 | protected function interceptProperty(ReflectionProperty $property) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * {@inheritDoc} |
||
| 425 | */ |
||
| 426 | 6 | public function __toString() |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Add code for intercepting properties |
||
| 460 | * |
||
| 461 | * @param null|ReflectionMethod $constructor Constructor reflection or null |
||
| 462 | */ |
||
| 463 | protected function addFieldInterceptorsCode(ReflectionMethod $constructor = null) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Returns a code for magic getter to perform interception |
||
| 479 | * |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | private function getMagicGetterBody() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Returns a code for magic setter to perform interception |
||
| 503 | * |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | private function getMagicSetterBody() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Returns constructor code |
||
| 526 | * |
||
| 527 | * @param ReflectionMethod $constructor Constructor reflection |
||
| 528 | * @param bool $isCallParent Is there is a need to call parent code |
||
| 529 | * |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | private function getConstructorBody(ReflectionMethod $constructor = null, $isCallParent = false) |
||
| 560 | } |
||
| 561 |