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 |
||
| 60 | abstract class BaseFieldDescription implements FieldDescriptionInterface |
||
| 61 | { |
||
| 62 | /** |
||
| 63 | * @var string the field name |
||
| 64 | */ |
||
| 65 | protected $name; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string|int the type |
||
| 69 | */ |
||
| 70 | protected $type; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string|int the original mapping type |
||
| 74 | */ |
||
| 75 | protected $mappingType; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string the field name (of the form) |
||
| 79 | */ |
||
| 80 | protected $fieldName; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array the ORM association mapping |
||
| 84 | */ |
||
| 85 | protected $associationMapping; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var array the ORM field information |
||
| 89 | */ |
||
| 90 | protected $fieldMapping; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var array the ORM parent mapping association |
||
| 94 | */ |
||
| 95 | protected $parentAssociationMappings; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var string the template name |
||
| 99 | */ |
||
| 100 | protected $template; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var array the option collection |
||
| 104 | */ |
||
| 105 | protected $options = []; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var AdminInterface|null the parent Admin instance |
||
| 109 | */ |
||
| 110 | protected $parent = null; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var AdminInterface the related admin instance |
||
| 114 | */ |
||
| 115 | protected $admin; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var AdminInterface the associated admin class if the object is associated to another entity |
||
| 119 | */ |
||
| 120 | protected $associationAdmin; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var string the help message to display |
||
| 124 | */ |
||
| 125 | protected $help; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var array[] cached object field getters |
||
| 129 | */ |
||
| 130 | private static $fieldGetters = []; |
||
| 131 | |||
| 132 | public function setFieldName($fieldName) |
||
| 136 | |||
| 137 | public function getFieldName() |
||
| 141 | |||
| 142 | public function setName($name) |
||
| 150 | |||
| 151 | public function getName() |
||
| 155 | |||
| 156 | public function getOption($name, $default = null) |
||
| 160 | |||
| 161 | public function setOption($name, $value) |
||
| 165 | |||
| 166 | public function setOptions(array $options) |
||
| 197 | |||
| 198 | public function getOptions() |
||
| 202 | |||
| 203 | public function setTemplate($template) |
||
| 207 | |||
| 208 | public function getTemplate() |
||
| 212 | |||
| 213 | public function setType($type) |
||
| 217 | |||
| 218 | public function getType() |
||
| 222 | |||
| 223 | public function setParent(AdminInterface $parent) |
||
| 227 | |||
| 228 | public function getParent() |
||
| 232 | |||
| 233 | public function getAssociationMapping() |
||
| 237 | |||
| 238 | public function getFieldMapping() |
||
| 242 | |||
| 243 | public function getParentAssociationMappings() |
||
| 247 | |||
| 248 | public function setAssociationAdmin(AdminInterface $associationAdmin) |
||
| 253 | |||
| 254 | public function getAssociationAdmin() |
||
| 258 | |||
| 259 | public function hasAssociationAdmin() |
||
| 263 | |||
| 264 | public function getFieldValue($object, $fieldName) |
||
| 265 | { |
||
| 266 | if ($this->isVirtual() || null === $object) { |
||
| 267 | return; |
||
| 268 | } |
||
| 269 | |||
| 270 | $getters = []; |
||
| 271 | $parameters = []; |
||
| 272 | |||
| 273 | // prefer method name given in the code option |
||
| 274 | if ($this->getOption('code')) { |
||
| 275 | $getters[] = $this->getOption('code'); |
||
| 276 | } |
||
| 277 | // parameters for the method given in the code option |
||
| 278 | if ($this->getOption('parameters')) { |
||
| 279 | $parameters = $this->getOption('parameters'); |
||
| 280 | } |
||
| 281 | |||
| 282 | if (is_string($fieldName) && '' !== $fieldName) { |
||
| 283 | if ($this->hasCachedFieldGetter($object, $fieldName)) { |
||
| 284 | return $this->callCachedGetter($object, $fieldName, $parameters); |
||
| 285 | } |
||
| 286 | |||
| 287 | $camelizedFieldName = Inflector::classify($fieldName); |
||
| 288 | |||
| 289 | $getters[] = 'get'.$camelizedFieldName; |
||
| 290 | $getters[] = 'is'.$camelizedFieldName; |
||
| 291 | $getters[] = 'has'.$camelizedFieldName; |
||
| 292 | } |
||
| 293 | |||
| 294 | foreach ($getters as $getter) { |
||
| 295 | if (method_exists($object, $getter) && is_callable([$object, $getter])) { |
||
| 296 | $this->cacheFieldGetter($object, $fieldName, 'getter', $getter); |
||
| 297 | |||
| 298 | return call_user_func_array([$object, $getter], $parameters); |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | if (method_exists($object, '__call')) { |
||
| 303 | $this->cacheFieldGetter($object, $fieldName, 'call'); |
||
| 304 | |||
| 305 | return call_user_func_array([$object, '__call'], [$fieldName, $parameters]); |
||
| 306 | } |
||
| 307 | |||
| 308 | if (isset($object->{$fieldName})) { |
||
| 309 | $this->cacheFieldGetter($object, $fieldName, 'var'); |
||
| 310 | |||
| 311 | return $object->{$fieldName}; |
||
| 312 | } |
||
| 313 | |||
| 314 | throw new NoValueException(sprintf('Unable to retrieve the value of `%s`', $this->getName())); |
||
| 315 | } |
||
| 316 | |||
| 317 | public function setAdmin(AdminInterface $admin) |
||
| 318 | { |
||
| 319 | $this->admin = $admin; |
||
| 320 | } |
||
| 321 | |||
| 322 | public function getAdmin() |
||
| 323 | { |
||
| 324 | return $this->admin; |
||
| 325 | } |
||
| 326 | |||
| 327 | public function mergeOption($name, array $options = []) |
||
| 328 | { |
||
| 329 | if (!isset($this->options[$name])) { |
||
| 330 | $this->options[$name] = []; |
||
| 331 | } |
||
| 332 | |||
| 333 | if (!is_array($this->options[$name])) { |
||
| 334 | throw new \RuntimeException(sprintf('The key `%s` does not point to an array value', $name)); |
||
| 335 | } |
||
| 336 | |||
| 337 | $this->options[$name] = array_merge($this->options[$name], $options); |
||
| 338 | } |
||
| 339 | |||
| 340 | public function mergeOptions(array $options = []) |
||
| 341 | { |
||
| 342 | $this->setOptions(array_merge_recursive($this->options, $options)); |
||
| 343 | } |
||
| 344 | |||
| 345 | public function setMappingType($mappingType) |
||
| 346 | { |
||
| 347 | $this->mappingType = $mappingType; |
||
| 348 | } |
||
| 349 | |||
| 350 | public function getMappingType() |
||
| 351 | { |
||
| 352 | return $this->mappingType; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Camelize a string. |
||
| 357 | * |
||
| 358 | * NEXT_MAJOR: remove this method. |
||
| 359 | * |
||
| 360 | * @static |
||
| 361 | * |
||
| 362 | * @param string $property |
||
| 363 | * |
||
| 364 | * @return string |
||
| 365 | * |
||
| 366 | * @deprecated Deprecated since version 3.1. Use \Doctrine\Common\Inflector\Inflector::classify() instead |
||
| 367 | */ |
||
| 368 | public static function camelize($property) |
||
| 369 | { |
||
| 370 | @trigger_error( |
||
|
|
|||
| 371 | sprintf( |
||
| 372 | 'The %s method is deprecated since 3.1 and will be removed in 4.0. '. |
||
| 373 | 'Use \Doctrine\Common\Inflector\Inflector::classify() instead.', |
||
| 374 | __METHOD__ |
||
| 375 | ), |
||
| 376 | E_USER_DEPRECATED |
||
| 377 | ); |
||
| 378 | |||
| 379 | return Inflector::classify($property); |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Defines the help message. |
||
| 384 | * |
||
| 385 | * @param string $help |
||
| 386 | */ |
||
| 387 | public function setHelp($help) |
||
| 388 | { |
||
| 389 | $this->help = $help; |
||
| 390 | } |
||
| 391 | |||
| 392 | public function getHelp() |
||
| 393 | { |
||
| 394 | return $this->help; |
||
| 395 | } |
||
| 396 | |||
| 397 | public function getLabel() |
||
| 398 | { |
||
| 399 | return $this->getOption('label'); |
||
| 400 | } |
||
| 401 | |||
| 402 | public function isSortable() |
||
| 403 | { |
||
| 404 | return false !== $this->getOption('sortable', false); |
||
| 405 | } |
||
| 406 | |||
| 407 | public function getSortFieldMapping() |
||
| 408 | { |
||
| 409 | return $this->getOption('sort_field_mapping'); |
||
| 410 | } |
||
| 411 | |||
| 412 | public function getSortParentAssociationMapping() |
||
| 413 | { |
||
| 414 | return $this->getOption('sort_parent_association_mappings'); |
||
| 415 | } |
||
| 416 | |||
| 417 | public function getTranslationDomain() |
||
| 418 | { |
||
| 419 | return $this->getOption('translation_domain') ?: $this->getAdmin()->getTranslationDomain(); |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Return true if field is virtual. |
||
| 424 | * |
||
| 425 | * @return bool |
||
| 426 | */ |
||
| 427 | public function isVirtual() |
||
| 428 | { |
||
| 429 | return false !== $this->getOption('virtual_field', false); |
||
| 430 | } |
||
| 431 | |||
| 432 | private function getFieldGetterKey($object, $fieldName) |
||
| 433 | { |
||
| 434 | if (!is_string($fieldName)) { |
||
| 435 | return null; |
||
| 436 | } |
||
| 437 | if (!is_object($object)) { |
||
| 438 | return null; |
||
| 439 | } |
||
| 440 | $components = [get_class($object), $fieldName]; |
||
| 441 | $code = $this->getOption('code'); |
||
| 442 | if (is_string($code) && '' !== $code) { |
||
| 443 | $components[] = $code; |
||
| 444 | } |
||
| 445 | |||
| 446 | return implode('-', $components); |
||
| 447 | } |
||
| 448 | |||
| 449 | private function hasCachedFieldGetter($object, $fieldName) |
||
| 455 | |||
| 456 | private function callCachedGetter($object, $fieldName, array $parameters = []) |
||
| 473 | |||
| 474 | private function cacheFieldGetter($object, $fieldName, $method, $getter = null) |
||
| 486 | } |
||
| 487 |
If you suppress an error, we recommend checking for the error condition explicitly: