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 |
||
| 35 | class ClassProxy extends AbstractProxy |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Parent class reflection |
||
| 39 | * |
||
| 40 | * @var null|ReflectionClass |
||
| 41 | */ |
||
| 42 | protected $class = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Parent class name, can be changed manually |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $parentClassName = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Source code for methods |
||
| 53 | * |
||
| 54 | * @var array Name of method => source code for it |
||
| 55 | */ |
||
| 56 | protected $methodsCode = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Static mappings for class name for excluding if..else check |
||
| 60 | * |
||
| 61 | * @var null|array |
||
| 62 | */ |
||
| 63 | protected static $invocationClassMap = [ |
||
| 64 | AspectContainer::METHOD_PREFIX => DynamicClosureSplatMethodInvocation::class, |
||
| 65 | AspectContainer::STATIC_METHOD_PREFIX => StaticClosureMethodInvocation::class, |
||
| 66 | AspectContainer::PROPERTY_PREFIX => ClassFieldAccess::class, |
||
| 67 | AspectContainer::STATIC_INIT_PREFIX => StaticInitializationJoinpoint::class, |
||
| 68 | AspectContainer::INIT_PREFIX => ReflectionConstructorInvocation::class |
||
| 69 | ]; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * List of additional interfaces to implement |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $interfaces = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * List of additional traits for using |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected $traits = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Source code for properties |
||
| 87 | * |
||
| 88 | * @var array Name of property => source code for it |
||
| 89 | */ |
||
| 90 | protected $propertiesCode = []; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Name for the current class |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | protected $name = ''; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Flag to determine if we need to add a code for property interceptors |
||
| 101 | * |
||
| 102 | * @var bool |
||
| 103 | */ |
||
| 104 | private $isFieldsIntercepted = false; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * List of intercepted properties names |
||
| 108 | * |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | private $interceptedProperties = []; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Generates an child code by parent class reflection and joinpoints for it |
||
| 115 | * |
||
| 116 | * @param ReflectionClass $parent Parent class reflection |
||
| 117 | * @param array|Advice[] $classAdvices List of advices for class |
||
| 118 | * |
||
| 119 | * @throws \InvalidArgumentException if there are unknown type of advices |
||
| 120 | */ |
||
| 121 | 8 | public function __construct(ReflectionClass $parent, array $classAdvices) |
|
| 122 | { |
||
| 123 | 8 | parent::__construct($classAdvices); |
|
| 124 | |||
| 125 | 8 | $this->class = $parent; |
|
| 126 | 8 | $this->name = $parent->getShortName(); |
|
| 127 | 8 | $this->parentClassName = $parent->getShortName(); |
|
| 128 | |||
| 129 | 8 | $this->addInterface('\Go\Aop\Proxy'); |
|
| 130 | 8 | $this->addJoinpointsProperty(); |
|
| 131 | |||
| 132 | 8 | foreach ($classAdvices as $type => $typedAdvices) { |
|
| 133 | |||
| 134 | switch ($type) { |
||
| 135 | 8 | case AspectContainer::METHOD_PREFIX: |
|
| 136 | case AspectContainer::STATIC_METHOD_PREFIX: |
||
| 137 | 8 | foreach ($typedAdvices as $joinPointName => $advice) { |
|
| 138 | 8 | $method = $parent->getMethod($joinPointName); |
|
| 139 | 8 | $this->overrideMethod($method); |
|
| 140 | } |
||
| 141 | 8 | break; |
|
| 142 | |||
| 143 | case AspectContainer::PROPERTY_PREFIX: |
||
| 144 | foreach ($typedAdvices as $joinPointName => $advice) { |
||
| 145 | $property = $parent->getProperty($joinPointName); |
||
| 146 | $this->interceptProperty($property); |
||
| 147 | } |
||
| 148 | break; |
||
| 149 | |||
| 150 | case AspectContainer::INTRODUCTION_TRAIT_PREFIX: |
||
| 151 | foreach ($typedAdvices as $advice) { |
||
| 152 | /** @var $advice IntroductionInfo */ |
||
| 153 | foreach ($advice->getInterfaces() as $interface) { |
||
| 154 | $this->addInterface($interface); |
||
| 155 | } |
||
| 156 | foreach ($advice->getTraits() as $trait) { |
||
| 157 | $this->addTrait($trait); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | break; |
||
| 161 | |||
| 162 | case AspectContainer::INIT_PREFIX: |
||
| 163 | case AspectContainer::STATIC_INIT_PREFIX: |
||
| 164 | break; // No changes for class |
||
| 165 | |||
| 166 | default: |
||
| 167 | 8 | throw new \InvalidArgumentException("Unsupported point `$type`"); |
|
| 168 | } |
||
| 169 | } |
||
| 170 | 8 | } |
|
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * Updates parent name for child |
||
| 175 | * |
||
| 176 | * @param string $newParentName New class name |
||
| 177 | * |
||
| 178 | * @return static |
||
| 179 | */ |
||
| 180 | 8 | public function setParentName($newParentName) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Override parent method with new body |
||
| 189 | * |
||
| 190 | * @param string $methodName Method name to override |
||
| 191 | * @param string $body New body for method |
||
| 192 | * |
||
| 193 | * @return static |
||
| 194 | */ |
||
| 195 | 8 | public function override($methodName, $body) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Creates a method |
||
| 204 | * |
||
| 205 | * @param int $methodFlags See ReflectionMethod modifiers |
||
| 206 | * @param string $methodName Name of the method |
||
| 207 | * @param bool $byReference Is method should return value by reference |
||
| 208 | * @param string $body Body of method |
||
| 209 | * @param string $parameters Definition of parameters |
||
| 210 | * |
||
| 211 | * @return static |
||
| 212 | */ |
||
| 213 | public function setMethod($methodFlags, $methodName, $byReference, $body, $parameters) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Inject advices into given class |
||
| 234 | * |
||
| 235 | * NB This method will be used as a callback during source code evaluation to inject joinpoints |
||
| 236 | * |
||
| 237 | * @param string $className Aop child proxy class |
||
| 238 | * @param array|Advice[] $advices List of advices to inject into class |
||
| 239 | * |
||
| 240 | * @return void |
||
| 241 | */ |
||
| 242 | public static function injectJoinPoints($className, array $advices = []) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Wrap advices with joinpoint object |
||
| 259 | * |
||
| 260 | * @param array|Advice[] $classAdvices Advices for specific class |
||
| 261 | * @param string $className Name of the original class to use |
||
| 262 | * |
||
| 263 | * @throws \UnexpectedValueException If joinPoint type is unknown |
||
| 264 | * |
||
| 265 | * NB: Extension should be responsible for wrapping advice with join point. |
||
| 266 | * |
||
| 267 | * @return array|Joinpoint[] returns list of joinpoint ready to use |
||
| 268 | */ |
||
| 269 | protected static function wrapWithJoinPoints($classAdvices, $className) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Add an interface for child |
||
| 302 | * |
||
| 303 | * @param string|ReflectionClass $interface |
||
| 304 | * |
||
| 305 | * @throws \InvalidArgumentException If object is not an interface |
||
| 306 | */ |
||
| 307 | 8 | public function addInterface($interface) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Add a trait for child |
||
| 322 | * |
||
| 323 | * @param string|ReflectionClass $trait |
||
| 324 | * |
||
| 325 | * @throws \InvalidArgumentException If object is not a trait |
||
| 326 | */ |
||
| 327 | public function addTrait($trait) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Creates a property |
||
| 342 | * |
||
| 343 | * @param int $propFlags See ReflectionProperty modifiers |
||
| 344 | * @param string $propName Name of the property |
||
| 345 | * @param null|string $defaultText Default value, should be string text! |
||
| 346 | * |
||
| 347 | * @return static |
||
| 348 | */ |
||
| 349 | 8 | public function setProperty($propFlags, $propName, $defaultText = null) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Adds a definition for joinpoints private property in the class |
||
| 365 | * |
||
| 366 | * @return void |
||
| 367 | */ |
||
| 368 | 8 | protected function addJoinpointsProperty() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Override parent method with joinpoint invocation |
||
| 379 | * |
||
| 380 | * @param ReflectionMethod $method Method reflection |
||
| 381 | */ |
||
| 382 | 8 | protected function overrideMethod(ReflectionMethod $method) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Creates definition for method body |
||
| 392 | * |
||
| 393 | * @param ReflectionMethod $method Method reflection |
||
| 394 | * |
||
| 395 | * @return string new method body |
||
| 396 | */ |
||
| 397 | 8 | protected function getJoinpointInvocationBody(ReflectionMethod $method) |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Makes property intercepted |
||
| 417 | * |
||
| 418 | * @param ReflectionProperty $property Reflection of property to intercept |
||
| 419 | */ |
||
| 420 | protected function interceptProperty(ReflectionProperty $property) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * {@inheritDoc} |
||
| 428 | */ |
||
| 429 | 8 | public function __toString() |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Add code for intercepting properties |
||
| 463 | * |
||
| 464 | * @param null|ReflectionMethod $constructor Constructor reflection or null |
||
| 465 | */ |
||
| 466 | protected function addFieldInterceptorsCode(ReflectionMethod $constructor = null) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Creates a method code from Reflection |
||
| 482 | * |
||
| 483 | * @param ReflectionMethod $method Reflection for method |
||
| 484 | * @param string $body Body of method |
||
| 485 | * |
||
| 486 | * @return string |
||
| 487 | */ |
||
| 488 | 8 | protected function getOverriddenMethod(ReflectionMethod $method, $body) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Returns a code for magic getter to perform interception |
||
| 509 | * |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | private function getMagicGetterBody() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Returns a code for magic setter to perform interception |
||
| 533 | * |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | private function getMagicSetterBody() |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Returns constructor code |
||
| 556 | * |
||
| 557 | * @param ReflectionMethod $constructor Constructor reflection |
||
| 558 | * @param bool $isCallParent Is there is a need to call parent code |
||
| 559 | * |
||
| 560 | * @return string |
||
| 561 | */ |
||
| 562 | private function getConstructorBody(ReflectionMethod $constructor = null, $isCallParent = false) |
||
| 590 | } |
||
| 591 |