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 array[] cached object field getters |
||
123 | */ |
||
124 | private static $fieldGetters = []; |
||
125 | |||
126 | public function setFieldName(?string $fieldName): void |
||
127 | { |
||
128 | $this->fieldName = $fieldName; |
||
129 | } |
||
130 | |||
131 | public function getFieldName(): ?string |
||
132 | { |
||
133 | return $this->fieldName; |
||
134 | } |
||
135 | |||
136 | public function setName(?string $name): void |
||
137 | { |
||
138 | $this->name = $name; |
||
139 | |||
140 | if (!$this->getFieldName()) { |
||
141 | $this->setFieldName(substr(strrchr('.'.$name, '.'), 1)); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | public function getName(): ?string |
||
146 | { |
||
147 | return $this->name; |
||
148 | } |
||
149 | |||
150 | public function getOption(string $name, $default = null) |
||
151 | { |
||
152 | return isset($this->options[$name]) ? $this->options[$name] : $default; |
||
153 | } |
||
154 | |||
155 | public function setOption(string $name, $value): void |
||
156 | { |
||
157 | $this->options[$name] = $value; |
||
158 | } |
||
159 | |||
160 | public function setOptions(array $options): void |
||
161 | { |
||
162 | // set the type if provided |
||
163 | if (isset($options['type'])) { |
||
164 | $this->setType($options['type']); |
||
165 | unset($options['type']); |
||
166 | } |
||
167 | |||
168 | // remove property value |
||
169 | if (isset($options['template'])) { |
||
170 | $this->setTemplate($options['template']); |
||
171 | unset($options['template']); |
||
172 | } |
||
173 | |||
174 | // set default placeholder |
||
175 | if (!isset($options['placeholder'])) { |
||
176 | $options['placeholder'] = 'short_object_description_placeholder'; |
||
177 | } |
||
178 | |||
179 | if (!isset($options['link_parameters'])) { |
||
180 | $options['link_parameters'] = []; |
||
181 | } |
||
182 | |||
183 | $this->options = $options; |
||
184 | } |
||
185 | |||
186 | public function getOptions(): array |
||
187 | { |
||
188 | return $this->options; |
||
189 | } |
||
190 | |||
191 | public function setTemplate(?string $template): void |
||
192 | { |
||
193 | $this->template = $template; |
||
194 | } |
||
195 | |||
196 | public function getTemplate(): ?string |
||
197 | { |
||
198 | return $this->template; |
||
199 | } |
||
200 | |||
201 | public function setType(?string $type): void |
||
202 | { |
||
203 | $this->type = $type; |
||
204 | } |
||
205 | |||
206 | public function getType(): ?string |
||
207 | { |
||
208 | return $this->type; |
||
209 | } |
||
210 | |||
211 | public function setParent(AdminInterface $parent): void |
||
212 | { |
||
213 | $this->parent = $parent; |
||
214 | } |
||
215 | |||
216 | public function getParent(): AdminInterface |
||
217 | { |
||
218 | if (!$this->hasParent()) { |
||
219 | throw new \LogicException(sprintf('%s has no parent.', static::class)); |
||
220 | } |
||
221 | |||
222 | return $this->parent; |
||
223 | } |
||
224 | |||
225 | public function hasParent(): bool |
||
226 | { |
||
227 | return null !== $this->parent; |
||
228 | } |
||
229 | |||
230 | public function getAssociationMapping(): array |
||
231 | { |
||
232 | return $this->associationMapping; |
||
233 | } |
||
234 | |||
235 | public function getFieldMapping(): array |
||
236 | { |
||
237 | return $this->fieldMapping; |
||
238 | } |
||
239 | |||
240 | public function getParentAssociationMappings(): array |
||
241 | { |
||
242 | return $this->parentAssociationMappings; |
||
243 | } |
||
244 | |||
245 | public function setAssociationAdmin(AdminInterface $associationAdmin): void |
||
246 | { |
||
247 | $this->associationAdmin = $associationAdmin; |
||
248 | $this->associationAdmin->setParentFieldDescription($this); |
||
249 | } |
||
250 | |||
251 | public function getAssociationAdmin(): AdminInterface |
||
252 | { |
||
253 | if (!$this->hasAssociationAdmin()) { |
||
254 | throw new \LogicException(sprintf('%s has no association admin.', static::class)); |
||
255 | } |
||
256 | |||
257 | return $this->associationAdmin; |
||
258 | } |
||
259 | |||
260 | public function hasAssociationAdmin(): bool |
||
261 | { |
||
262 | return null !== $this->associationAdmin; |
||
263 | } |
||
264 | |||
265 | public function getFieldValue(?object $object, ?string $fieldName) |
||
266 | { |
||
267 | if ($this->isVirtual() || null === $object) { |
||
268 | return null; |
||
269 | } |
||
270 | |||
271 | $getters = []; |
||
272 | $parameters = []; |
||
273 | |||
274 | // prefer method name given in the code option |
||
275 | if ($this->getOption('code')) { |
||
276 | $getters[] = $this->getOption('code'); |
||
277 | } |
||
278 | // parameters for the method given in the code option |
||
279 | if ($this->getOption('parameters')) { |
||
280 | $parameters = $this->getOption('parameters'); |
||
281 | } |
||
282 | |||
283 | if (\is_string($fieldName) && '' !== $fieldName) { |
||
284 | if ($this->hasCachedFieldGetter($object, $fieldName)) { |
||
285 | return $this->callCachedGetter($object, $fieldName, $parameters); |
||
286 | } |
||
287 | |||
288 | $camelizedFieldName = InflectorFactory::create()->build()->classify($fieldName); |
||
289 | |||
290 | $getters[] = sprintf('get%s', $camelizedFieldName); |
||
291 | $getters[] = sprintf('is%s', $camelizedFieldName); |
||
292 | $getters[] = sprintf('has%s', $camelizedFieldName); |
||
293 | } |
||
294 | |||
295 | foreach ($getters as $getter) { |
||
296 | if (method_exists($object, $getter) && \is_callable([$object, $getter])) { |
||
297 | $this->cacheFieldGetter($object, $fieldName, 'getter', $getter); |
||
298 | |||
299 | return $object->$getter(...$parameters); |
||
300 | } |
||
301 | } |
||
302 | |||
303 | if (method_exists($object, '__call')) { |
||
304 | $this->cacheFieldGetter($object, $fieldName, 'call'); |
||
305 | |||
306 | return $object->$fieldName(...$parameters); |
||
307 | } |
||
308 | |||
309 | if (isset($object->{$fieldName})) { |
||
310 | $this->cacheFieldGetter($object, $fieldName, 'var'); |
||
311 | |||
312 | return $object->{$fieldName}; |
||
313 | } |
||
314 | |||
315 | throw new NoValueException(sprintf( |
||
316 | 'Neither the property "%s" nor one of the methods "%s()" exist and have public access in class "%s".', |
||
317 | $this->getName(), |
||
318 | implode('()", "', $getters), |
||
319 | \get_class($object) |
||
320 | )); |
||
321 | } |
||
322 | |||
323 | public function setAdmin(AdminInterface $admin): void |
||
324 | { |
||
325 | $this->admin = $admin; |
||
326 | } |
||
327 | |||
328 | public function getAdmin(): AdminInterface |
||
329 | { |
||
330 | if (!$this->hasAdmin()) { |
||
331 | throw new \LogicException(sprintf('%s has no admin.', static::class)); |
||
332 | } |
||
333 | |||
334 | return $this->admin; |
||
335 | } |
||
336 | |||
337 | public function hasAdmin(): bool |
||
338 | { |
||
339 | return null !== $this->admin; |
||
340 | } |
||
341 | |||
342 | public function mergeOption(string $name, array $options = []): void |
||
343 | { |
||
344 | if (!isset($this->options[$name])) { |
||
345 | $this->options[$name] = []; |
||
346 | } |
||
347 | |||
348 | if (!\is_array($this->options[$name])) { |
||
349 | throw new \RuntimeException(sprintf('The key `%s` does not point to an array value', $name)); |
||
350 | } |
||
351 | |||
352 | $this->options[$name] = array_merge($this->options[$name], $options); |
||
353 | } |
||
354 | |||
355 | public function mergeOptions(array $options = []): void |
||
356 | { |
||
357 | $this->setOptions(array_merge_recursive($this->options, $options)); |
||
358 | } |
||
359 | |||
360 | public function setMappingType($mappingType): void |
||
361 | { |
||
362 | $this->mappingType = $mappingType; |
||
363 | } |
||
364 | |||
365 | public function getMappingType() |
||
366 | { |
||
367 | return $this->mappingType; |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * @return string|false|null |
||
372 | */ |
||
373 | public function getLabel() |
||
374 | { |
||
375 | return $this->getOption('label'); |
||
376 | } |
||
377 | |||
378 | public function isSortable(): bool |
||
379 | { |
||
380 | return false !== $this->getOption('sortable', false); |
||
381 | } |
||
382 | |||
383 | public function getSortFieldMapping(): array |
||
384 | { |
||
385 | return $this->getOption('sort_field_mapping'); |
||
386 | } |
||
387 | |||
388 | public function getSortParentAssociationMapping(): array |
||
389 | { |
||
390 | return $this->getOption('sort_parent_association_mappings'); |
||
391 | } |
||
392 | |||
393 | public function getTranslationDomain(): string |
||
394 | { |
||
395 | return $this->getOption('translation_domain') ?: $this->getAdmin()->getTranslationDomain(); |
||
396 | } |
||
397 | |||
398 | public function isVirtual(): bool |
||
399 | { |
||
400 | return false !== $this->getOption('virtual_field', false); |
||
401 | } |
||
402 | |||
403 | private function getFieldGetterKey(object $object, ?string $fieldName): ?string |
||
404 | { |
||
405 | if (!\is_string($fieldName)) { |
||
406 | return null; |
||
407 | } |
||
408 | |||
409 | $components = [\get_class($object), $fieldName]; |
||
410 | |||
411 | $code = $this->getOption('code'); |
||
412 | if (\is_string($code) && '' !== $code) { |
||
413 | $components[] = $code; |
||
414 | } |
||
415 | |||
416 | return implode('-', $components); |
||
417 | } |
||
418 | |||
419 | private function hasCachedFieldGetter(object $object, string $fieldName): bool |
||
425 | |||
426 | private function callCachedGetter(object $object, string $fieldName, array $parameters = []) |
||
440 | |||
441 | private function cacheFieldGetter(object $object, ?string $fieldName, string $method, ?string $getter = null): void |
||
453 | } |
||
454 |