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 |
||
| 59 | abstract class BaseFieldDescription implements FieldDescriptionInterface |
||
| 60 | { |
||
| 61 | /** |
||
| 62 | * @var string the field name |
||
| 63 | */ |
||
| 64 | protected $name; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string the type |
||
| 68 | */ |
||
| 69 | protected $type; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string|int the original mapping type |
||
| 73 | */ |
||
| 74 | protected $mappingType; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string the field name (of the form) |
||
| 78 | */ |
||
| 79 | protected $fieldName; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var array the ORM association mapping |
||
| 83 | */ |
||
| 84 | protected $associationMapping = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array the ORM field information |
||
| 88 | */ |
||
| 89 | protected $fieldMapping = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var array the ORM parent mapping association |
||
| 93 | */ |
||
| 94 | protected $parentAssociationMappings = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var string|null the template name |
||
| 98 | */ |
||
| 99 | protected $template; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var array<string, mixed> the option collection |
||
| 103 | */ |
||
| 104 | protected $options = []; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var AdminInterface|null the parent Admin instance |
||
| 108 | */ |
||
| 109 | protected $parent; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var AdminInterface|null the related admin instance |
||
| 113 | */ |
||
| 114 | protected $admin; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var AdminInterface|null the associated admin class if the object is associated to another entity |
||
| 118 | */ |
||
| 119 | protected $associationAdmin; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var string the help message to display |
||
| 123 | */ |
||
| 124 | protected $help; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var array[] cached object field getters |
||
| 128 | */ |
||
| 129 | private static $fieldGetters = []; |
||
| 130 | |||
| 131 | public function setFieldName(?string $fieldName): void |
||
| 132 | { |
||
| 133 | $this->fieldName = $fieldName; |
||
| 134 | } |
||
| 135 | |||
| 136 | public function getFieldName(): ?string |
||
| 137 | { |
||
| 138 | return $this->fieldName; |
||
| 139 | } |
||
| 140 | |||
| 141 | public function setName(?string $name): void |
||
| 142 | { |
||
| 143 | $this->name = $name; |
||
| 144 | |||
| 145 | if (!$this->getFieldName()) { |
||
| 146 | $this->setFieldName(substr(strrchr('.'.$name, '.'), 1)); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | public function getName(): ?string |
||
| 151 | { |
||
| 152 | return $this->name; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function getOption(string $name, $default = null) |
||
| 156 | { |
||
| 157 | return isset($this->options[$name]) ? $this->options[$name] : $default; |
||
| 158 | } |
||
| 159 | |||
| 160 | public function setOption(string $name, $value): void |
||
| 161 | { |
||
| 162 | $this->options[$name] = $value; |
||
| 163 | } |
||
| 164 | |||
| 165 | public function setOptions(array $options): void |
||
| 166 | { |
||
| 167 | // set the type if provided |
||
| 168 | if (isset($options['type'])) { |
||
| 169 | $this->setType($options['type']); |
||
| 170 | unset($options['type']); |
||
| 171 | } |
||
| 172 | |||
| 173 | // remove property value |
||
| 174 | if (isset($options['template'])) { |
||
| 175 | $this->setTemplate($options['template']); |
||
| 176 | unset($options['template']); |
||
| 177 | } |
||
| 178 | |||
| 179 | // set default placeholder |
||
| 180 | if (!isset($options['placeholder'])) { |
||
| 181 | $options['placeholder'] = 'short_object_description_placeholder'; |
||
| 182 | } |
||
| 183 | |||
| 184 | if (!isset($options['link_parameters'])) { |
||
| 185 | $options['link_parameters'] = []; |
||
| 186 | } |
||
| 187 | |||
| 188 | $this->options = $options; |
||
| 189 | } |
||
| 190 | |||
| 191 | public function getOptions(): array |
||
| 192 | { |
||
| 193 | return $this->options; |
||
| 194 | } |
||
| 195 | |||
| 196 | public function setTemplate(?string $template): void |
||
| 197 | { |
||
| 198 | $this->template = $template; |
||
| 199 | } |
||
| 200 | |||
| 201 | public function getTemplate(): ?string |
||
| 202 | { |
||
| 203 | return $this->template; |
||
| 204 | } |
||
| 205 | |||
| 206 | public function setType(?string $type): void |
||
| 207 | { |
||
| 208 | $this->type = $type; |
||
| 209 | } |
||
| 210 | |||
| 211 | public function getType(): ?string |
||
| 212 | { |
||
| 213 | return $this->type; |
||
| 214 | } |
||
| 215 | |||
| 216 | public function setParent(AdminInterface $parent): void |
||
| 217 | { |
||
| 218 | $this->parent = $parent; |
||
| 219 | } |
||
| 220 | |||
| 221 | public function getParent(): AdminInterface |
||
| 222 | { |
||
| 223 | if (!$this->hasParent()) { |
||
| 224 | throw new \LogicException(sprintf('%s has no parent.', static::class)); |
||
| 225 | } |
||
| 226 | |||
| 227 | return $this->parent; |
||
| 228 | } |
||
| 229 | |||
| 230 | public function hasParent(): bool |
||
| 231 | { |
||
| 232 | return null !== $this->parent; |
||
| 233 | } |
||
| 234 | |||
| 235 | public function getAssociationMapping(): array |
||
| 239 | |||
| 240 | public function getFieldMapping(): array |
||
| 244 | |||
| 245 | public function getParentAssociationMappings(): array |
||
| 249 | |||
| 250 | public function setAssociationAdmin(AdminInterface $associationAdmin): void |
||
| 255 | |||
| 256 | public function getAssociationAdmin(): AdminInterface |
||
| 257 | { |
||
| 258 | if (!$this->hasAssociationAdmin()) { |
||
| 259 | throw new \LogicException(sprintf('%s has no association admin.', static::class)); |
||
| 260 | } |
||
| 261 | |||
| 262 | return $this->associationAdmin; |
||
| 263 | } |
||
| 264 | |||
| 265 | public function hasAssociationAdmin(): bool |
||
| 266 | { |
||
| 267 | return null !== $this->associationAdmin; |
||
| 268 | } |
||
| 269 | |||
| 270 | public function getFieldValue(?object $object, ?string $fieldName) |
||
| 271 | { |
||
| 272 | if ($this->isVirtual() || null === $object) { |
||
| 327 | |||
| 328 | public function setAdmin(AdminInterface $admin): void |
||
| 332 | |||
| 333 | public function getAdmin(): AdminInterface |
||
| 341 | |||
| 342 | public function hasAdmin(): bool |
||
| 346 | |||
| 347 | public function mergeOption(string $name, array $options = []): void |
||
| 359 | |||
| 360 | public function mergeOptions(array $options = []): void |
||
| 364 | |||
| 365 | public function setMappingType($mappingType): void |
||
| 369 | |||
| 370 | public function getMappingType() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @return string|false|null |
||
| 377 | */ |
||
| 378 | public function getLabel() |
||
| 382 | |||
| 383 | public function isSortable(): bool |
||
| 387 | |||
| 388 | public function getSortFieldMapping(): array |
||
| 392 | |||
| 393 | public function getSortParentAssociationMapping(): array |
||
| 397 | |||
| 398 | public function getTranslationDomain(): string |
||
| 402 | |||
| 403 | public function isVirtual(): bool |
||
| 407 | |||
| 408 | private function getFieldGetterKey($object, ?string $fieldName): ?string |
||
| 426 | |||
| 427 | private function hasCachedFieldGetter($object, string $fieldName): bool |
||
| 433 | |||
| 434 | private function callCachedGetter($object, string $fieldName, array $parameters = []) |
||
| 448 | |||
| 449 | private function cacheFieldGetter($object, ?string $fieldName, string $method, ?string $getter = null): void |
||
| 461 | } |
||
| 462 |