Complex classes like Structure 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 Structure, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | abstract class Structure extends Mapper { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array|object |
||
| 31 | */ |
||
| 32 | private $structure; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var AdapterInterface |
||
| 36 | */ |
||
| 37 | private $annotationAdapter; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Reflection |
||
| 41 | */ |
||
| 42 | private $reflector; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return array|object |
||
| 46 | */ |
||
| 47 | 47 | protected function getInputStructure() { |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @param array|object $structure |
||
| 53 | * @return $this |
||
| 54 | */ |
||
| 55 | 47 | protected function setInputStructure($structure) { |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @return Reflection |
||
| 63 | */ |
||
| 64 | 47 | private function getReflector() { |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @param Reflection $reflector |
||
| 70 | * @return $this |
||
| 71 | */ |
||
| 72 | 47 | private function setReflector(Reflection $reflector) { |
|
| 77 | |||
| 78 | /** |
||
| 79 | * @return AdapterInterface |
||
| 80 | */ |
||
| 81 | 47 | private function getAnnotationAdapter() { |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param AdapterInterface $adapter |
||
| 87 | * @return $this |
||
| 88 | */ |
||
| 89 | 47 | private function setAnnotationAdapter(AdapterInterface $adapter) { |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param integer $adapter |
||
| 97 | * @return $this |
||
| 98 | */ |
||
| 99 | 50 | public function setAnnotationAdapterType($adapter) { |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $class |
||
| 112 | * @return $this |
||
| 113 | */ |
||
| 114 | 1 | public function setOutputClass($class) { |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @param object $object |
||
| 122 | * @return $this |
||
| 123 | */ |
||
| 124 | 3 | public function setOutputObject($object) { |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @param array|object $inputStructure |
||
| 132 | * @param string|object $outputClassOrObject |
||
| 133 | * @param integer $adapter |
||
| 134 | */ |
||
| 135 | 50 | public function __construct( |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @return $this |
||
| 150 | * @throws UnknownAnnotationAdapter |
||
| 151 | */ |
||
| 152 | 50 | private function createAnnotationAdapter() { |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | 43 | private function getCacheDirectory() { |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | 47 | protected function updateReflector() { |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @return object |
||
| 195 | * @throws UnknownFieldException |
||
| 196 | */ |
||
| 197 | 47 | public function map() { |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | protected function getCurrentSkipAttributes() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param string $parentAttribute |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | 42 | protected function getSkipAttributesByParent($parentAttribute) { |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param $attribute |
||
| 274 | * @return boolean |
||
| 275 | */ |
||
| 276 | 47 | protected function needToSkip($attribute) { |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $attribute |
||
| 282 | * @param mixed $value |
||
| 283 | * @param Annotations $annotations |
||
| 284 | * @param Transform|null $transform |
||
| 285 | * @return mixed |
||
| 286 | * @throws MustBeSimpleException |
||
| 287 | * @throws MustBeSequenceException |
||
| 288 | * @throws MustBeObjectException |
||
| 289 | */ |
||
| 290 | 47 | protected function buildValueToMap( |
|
| 359 | |||
| 360 | /** |
||
| 361 | * @param string $attribute |
||
| 362 | * @return boolean |
||
| 363 | */ |
||
| 364 | 47 | protected function hasAttribute($attribute) { |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @param string $attribute |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | 47 | protected function createSetter($attribute) { |
|
| 379 | |||
| 380 | /** |
||
| 381 | * @param string $attribute |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | 47 | protected function createGetter($attribute) { |
|
| 387 | |||
| 388 | /** |
||
| 389 | * @param mixed $value |
||
| 390 | * @return boolean |
||
| 391 | */ |
||
| 392 | abstract protected function isObject($value); |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param mixed $value |
||
| 396 | * @return boolean |
||
| 397 | */ |
||
| 398 | abstract protected function isSequential($value); |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param mixed $value |
||
| 402 | * @return boolean |
||
| 403 | */ |
||
| 404 | abstract protected function isStructure($value); |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | abstract protected function getInputAttributes(); |
||
| 410 | |||
| 411 | } |