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 |
||
| 33 | class ClassProxy extends AbstractProxy |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Parent class reflection |
||
| 37 | * |
||
| 38 | * @var null|ReflectionClass |
||
| 39 | */ |
||
| 40 | protected $class; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Parent class name, can be changed manually |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $parentClassName; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Source code for methods |
||
| 51 | * |
||
| 52 | * @var array Name of method => source code for it |
||
| 53 | */ |
||
| 54 | protected $methodsCode = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Static mappings for class name for excluding if..else check |
||
| 58 | * |
||
| 59 | * @var null|array |
||
| 60 | */ |
||
| 61 | protected static $invocationClassMap = [ |
||
| 62 | AspectContainer::METHOD_PREFIX => DynamicClosureMethodInvocation::class, |
||
| 63 | AspectContainer::STATIC_METHOD_PREFIX => StaticClosureMethodInvocation::class, |
||
| 64 | AspectContainer::PROPERTY_PREFIX => ClassFieldAccess::class, |
||
| 65 | AspectContainer::STATIC_INIT_PREFIX => StaticInitializationJoinpoint::class, |
||
| 66 | AspectContainer::INIT_PREFIX => ReflectionConstructorInvocation::class |
||
| 67 | ]; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * List of additional interfaces to implement |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $interfaces = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * List of additional traits for using |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $traits = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Source code for properties |
||
| 85 | * |
||
| 86 | * @var array Name of property => source code for it |
||
| 87 | */ |
||
| 88 | protected $propertiesCode = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Name for the current class |
||
| 92 | * |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | protected $name = ''; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Flag to determine if we need to add a code for property interceptors |
||
| 99 | * |
||
| 100 | * @var bool |
||
| 101 | */ |
||
| 102 | private $isFieldsIntercepted = false; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * List of intercepted properties names |
||
| 106 | * |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | private $interceptedProperties = []; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Generates an child code by parent class reflection and joinpoints for it |
||
| 113 | * |
||
| 114 | * @param ReflectionClass $parent Parent class reflection |
||
| 115 | * @param array|Advice[][] $classAdvices List of advices for class |
||
| 116 | * |
||
| 117 | * @throws \InvalidArgumentException if there are unknown type of advices |
||
| 118 | */ |
||
| 119 | 4 | 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 | * @return static |
||
| 178 | */ |
||
| 179 | 4 | public function setParentName($newParentName) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Override parent method with new body |
||
| 188 | * |
||
| 189 | * @param string $methodName Method name to override |
||
| 190 | * @param string $body New body for method |
||
| 191 | * |
||
| 192 | * @return static |
||
| 193 | */ |
||
| 194 | 4 | public function override($methodName, $body) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Creates a method |
||
| 203 | * |
||
| 204 | * @param int $methodFlags See ReflectionMethod modifiers |
||
| 205 | * @param string $methodName Name of the method |
||
| 206 | * @param bool $byReference Is method should return value by reference |
||
| 207 | * @param string $body Body of method |
||
| 208 | * @param string $parameters Definition of parameters |
||
| 209 | * |
||
| 210 | * @return static |
||
| 211 | */ |
||
| 212 | public function setMethod($methodFlags, $methodName, $byReference, $body, $parameters) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Inject advices into given class |
||
| 233 | * |
||
| 234 | * NB This method will be used as a callback during source code evaluation to inject joinpoints |
||
| 235 | * |
||
| 236 | * @param string $className Aop child proxy class |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | public static function injectJoinPoints($className) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Wrap advices with joinpoint object |
||
| 258 | * |
||
| 259 | * @param array|Advice[] $classAdvices Advices for specific class |
||
| 260 | * @param string $className Name of the original class to use |
||
| 261 | * |
||
| 262 | * @throws \UnexpectedValueException If joinPoint type is unknown |
||
| 263 | * |
||
| 264 | * NB: Extension should be responsible for wrapping advice with join point. |
||
| 265 | * |
||
| 266 | * @return array|Joinpoint[] returns list of joinpoint ready to use |
||
| 267 | */ |
||
| 268 | protected static function wrapWithJoinPoints($classAdvices, $className) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Add an interface for child |
||
| 301 | * |
||
| 302 | * @param string|ReflectionClass $interface |
||
| 303 | * |
||
| 304 | * @throws \InvalidArgumentException If object is not an interface |
||
| 305 | */ |
||
| 306 | 4 | public function addInterface($interface) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Add a trait for child |
||
| 321 | * |
||
| 322 | * @param string|ReflectionClass $trait |
||
| 323 | * |
||
| 324 | * @throws \InvalidArgumentException If object is not a trait |
||
| 325 | */ |
||
| 326 | public function addTrait($trait) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Creates a property |
||
| 341 | * |
||
| 342 | * @param int $propFlags See ReflectionProperty modifiers |
||
| 343 | * @param string $propName Name of the property |
||
| 344 | * @param null|string $defaultText Default value, should be string text! |
||
| 345 | * |
||
| 346 | * @return static |
||
| 347 | */ |
||
| 348 | 4 | public function setProperty($propFlags, $propName, $defaultText = null) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Adds a definition for joinpoints private property in the class |
||
| 364 | * |
||
| 365 | * @return void |
||
| 366 | */ |
||
| 367 | 4 | protected function addJoinpointsProperty() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Override parent method with joinpoint invocation |
||
| 384 | * |
||
| 385 | * @param ReflectionMethod $method Method reflection |
||
| 386 | */ |
||
| 387 | 4 | protected function overrideMethod(ReflectionMethod $method) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Creates definition for method body |
||
| 394 | * |
||
| 395 | * @param ReflectionMethod $method Method reflection |
||
| 396 | * |
||
| 397 | * @return string new method body |
||
| 398 | */ |
||
| 399 | 4 | protected function getJoinpointInvocationBody(ReflectionMethod $method) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Makes property intercepted |
||
| 426 | * |
||
| 427 | * @param ReflectionProperty $property Reflection of property to intercept |
||
| 428 | */ |
||
| 429 | protected function interceptProperty(ReflectionProperty $property) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * {@inheritDoc} |
||
| 437 | */ |
||
| 438 | 4 | public function __toString() |
|
| 467 | |||
| 468 | /** |
||
| 469 | * Add code for intercepting properties |
||
| 470 | * |
||
| 471 | * @param null|ReflectionMethod $constructor Constructor reflection or null |
||
| 472 | */ |
||
| 473 | protected function addFieldInterceptorsCode(ReflectionMethod $constructor = null) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Returns constructor code |
||
| 486 | * |
||
| 487 | * @param ReflectionMethod $constructor Constructor reflection |
||
| 488 | * @param bool $isCallParent Is there is a need to call parent code |
||
| 489 | * |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | private function getConstructorBody(ReflectionMethod $constructor = null, $isCallParent = false) |
||
| 519 | } |
||
| 520 |