Complex classes like BaseFieldDescription 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 BaseFieldDescription, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 62 | abstract class BaseFieldDescription implements FieldDescriptionInterface |
||
| 63 | { |
||
| 64 | /** |
||
| 65 | * @var string the field name |
||
| 66 | */ |
||
| 67 | protected $name; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string|int the type |
||
| 71 | */ |
||
| 72 | protected $type; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string|int the original mapping type |
||
| 76 | */ |
||
| 77 | protected $mappingType; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string the field name (of the form) |
||
| 81 | */ |
||
| 82 | protected $fieldName; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var array the ORM association mapping |
||
| 86 | */ |
||
| 87 | protected $associationMapping; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var array the ORM field information |
||
| 91 | */ |
||
| 92 | protected $fieldMapping; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var array the ORM parent mapping association |
||
| 96 | */ |
||
| 97 | protected $parentAssociationMappings; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string the template name |
||
| 101 | */ |
||
| 102 | protected $template; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var array the option collection |
||
| 106 | */ |
||
| 107 | protected $options = []; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var AdminInterface|null the parent Admin instance |
||
| 111 | */ |
||
| 112 | protected $parent = null; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var AdminInterface the related admin instance |
||
| 116 | */ |
||
| 117 | protected $admin; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var AdminInterface the associated admin class if the object is associated to another entity |
||
| 121 | */ |
||
| 122 | protected $associationAdmin; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var string the help message to display |
||
| 126 | */ |
||
| 127 | protected $help; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var array[] cached object field getters |
||
| 131 | */ |
||
| 132 | private static $fieldGetters = []; |
||
| 133 | |||
| 134 | public function setFieldName($fieldName): void |
||
| 138 | |||
| 139 | public function getFieldName() |
||
| 143 | |||
| 144 | public function setName($name): void |
||
| 152 | |||
| 153 | public function getName() |
||
| 157 | |||
| 158 | public function getOption($name, $default = null) |
||
| 162 | |||
| 163 | public function setOption($name, $value): void |
||
| 167 | |||
| 168 | public function setOptions(array $options): void |
||
| 199 | |||
| 200 | public function getOptions() |
||
| 204 | |||
| 205 | public function setTemplate($template): void |
||
| 209 | |||
| 210 | public function getTemplate() |
||
| 214 | |||
| 215 | public function setType($type): void |
||
| 219 | |||
| 220 | public function getType() |
||
| 224 | |||
| 225 | public function setParent(AdminInterface $parent): void |
||
| 229 | |||
| 230 | public function getParent() |
||
| 234 | |||
| 235 | public function getAssociationMapping() |
||
| 239 | |||
| 240 | public function getFieldMapping() |
||
| 244 | |||
| 245 | public function getParentAssociationMappings() |
||
| 249 | |||
| 250 | public function setAssociationAdmin(AdminInterface $associationAdmin): void |
||
| 255 | |||
| 256 | public function getAssociationAdmin() |
||
| 260 | |||
| 261 | public function hasAssociationAdmin() |
||
| 265 | |||
| 266 | public function getFieldValue($object, $fieldName) |
||
| 267 | { |
||
| 268 | if ($this->isVirtual() || null === $object) { |
||
| 269 | return; |
||
| 270 | } |
||
| 271 | |||
| 272 | $getters = []; |
||
| 273 | $parameters = []; |
||
| 274 | |||
| 275 | // prefer method name given in the code option |
||
| 276 | if ($this->getOption('code')) { |
||
| 277 | $getters[] = $this->getOption('code'); |
||
| 278 | } |
||
| 279 | // parameters for the method given in the code option |
||
| 280 | if ($this->getOption('parameters')) { |
||
| 281 | $parameters = $this->getOption('parameters'); |
||
| 282 | } |
||
| 283 | |||
| 284 | if (is_string($fieldName) && '' !== $fieldName) { |
||
| 285 | if ($this->hasCachedFieldGetter($object, $fieldName)) { |
||
| 286 | return $this->callCachedGetter($object, $fieldName, $parameters); |
||
| 287 | } |
||
| 288 | |||
| 289 | $camelizedFieldName = Inflector::classify($fieldName); |
||
| 290 | |||
| 291 | $getters[] = 'get'.$camelizedFieldName; |
||
| 292 | $getters[] = 'is'.$camelizedFieldName; |
||
| 293 | $getters[] = 'has'.$camelizedFieldName; |
||
| 294 | } |
||
| 295 | |||
| 296 | foreach ($getters as $getter) { |
||
| 297 | if (method_exists($object, $getter) && is_callable([$object, $getter])) { |
||
| 298 | $this->cacheFieldGetter($object, $fieldName, 'getter', $getter); |
||
| 299 | |||
| 300 | return call_user_func_array([$object, $getter], $parameters); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | if (method_exists($object, '__call')) { |
||
| 305 | $this->cacheFieldGetter($object, $fieldName, 'call'); |
||
| 306 | |||
| 307 | return call_user_func_array([$object, '__call'], [$fieldName, $parameters]); |
||
| 308 | } |
||
| 309 | |||
| 310 | if (isset($object->{$fieldName})) { |
||
| 311 | $this->cacheFieldGetter($object, $fieldName, 'var'); |
||
| 312 | |||
| 313 | return $object->{$fieldName}; |
||
| 314 | } |
||
| 315 | |||
| 316 | throw new NoValueException(sprintf('Unable to retrieve the value of `%s`', $this->getName())); |
||
| 317 | } |
||
| 318 | |||
| 319 | public function setAdmin(AdminInterface $admin): void |
||
| 323 | |||
| 324 | public function getAdmin() |
||
| 328 | |||
| 329 | public function mergeOption($name, array $options = []): void |
||
| 341 | |||
| 342 | public function mergeOptions(array $options = []): void |
||
| 346 | |||
| 347 | public function setMappingType($mappingType): void |
||
| 351 | |||
| 352 | public function getMappingType() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Defines the help message. |
||
| 359 | * |
||
| 360 | * @param string $help |
||
| 361 | */ |
||
| 362 | public function setHelp($help): void |
||
| 366 | |||
| 367 | public function getHelp() |
||
| 371 | |||
| 372 | public function getLabel() |
||
| 376 | |||
| 377 | public function isSortable(): bool |
||
| 381 | |||
| 382 | public function getSortFieldMapping() |
||
| 386 | |||
| 387 | public function getSortParentAssociationMapping() |
||
| 391 | |||
| 392 | public function getTranslationDomain() |
||
| 396 | |||
| 397 | public function isVirtual(): bool |
||
| 398 | { |
||
| 399 | return false !== $this->getOption('virtual_field', false); |
||
| 400 | } |
||
| 401 | |||
| 402 | private function getFieldGetterKey($object, $fieldName): ?string |
||
| 403 | { |
||
| 404 | if (!\is_string($fieldName)) { |
||
| 405 | return null; |
||
| 406 | } |
||
| 407 | |||
| 408 | if (!is_object($object)) { |
||
| 409 | return null; |
||
| 410 | } |
||
| 411 | $components = [\get_class($object), $fieldName]; |
||
| 412 | |||
| 413 | $code = $this->getOption('code'); |
||
| 414 | if (\is_string($code) && '' !== $code) { |
||
| 415 | $components[] = $code; |
||
| 416 | } |
||
| 417 | |||
| 418 | return implode('-', $components); |
||
| 419 | } |
||
| 420 | |||
| 421 | private function hasCachedFieldGetter($object, $fieldName): bool |
||
| 427 | |||
| 428 | private function callCachedGetter($object, $fieldName, array $parameters = []) |
||
| 445 | |||
| 446 | private function cacheFieldGetter($object, $fieldName, $method, $getter = null): void |
||
| 458 | } |
||
| 459 |