Complex classes like SonataAdminExtension 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 SonataAdminExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | final class SonataAdminExtension extends AbstractExtension |
||
41 | { |
||
42 | // @todo: there are more locales which are not supported by moment and they need to be translated/normalized/canonicalized here |
||
43 | public const MOMENT_UNSUPPORTED_LOCALES = [ |
||
44 | 'de' => ['de', 'de-at'], |
||
45 | 'es' => ['es', 'es-do'], |
||
46 | 'nl' => ['nl', 'nl-be'], |
||
47 | 'fr' => ['fr', 'fr-ca', 'fr-ch'], |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * @var TranslatorInterface |
||
52 | */ |
||
53 | private $translator; |
||
54 | |||
55 | /** |
||
56 | * @var Pool |
||
57 | */ |
||
58 | private $pool; |
||
59 | |||
60 | /** |
||
61 | * @var LoggerInterface |
||
62 | */ |
||
63 | private $logger; |
||
64 | |||
65 | /** |
||
66 | * @var string[] |
||
67 | */ |
||
68 | private $xEditableTypeMapping = []; |
||
69 | |||
70 | /** |
||
71 | * @var ContainerInterface |
||
72 | */ |
||
73 | private $templateRegistries; |
||
74 | |||
75 | /** |
||
76 | * @var AuthorizationCheckerInterface |
||
77 | */ |
||
78 | private $securityChecker; |
||
79 | |||
80 | public function __construct( |
||
93 | |||
94 | public function getFilters() |
||
139 | |||
140 | public function getFunctions() |
||
148 | |||
149 | public function getName() |
||
153 | |||
154 | /** |
||
155 | * render a list element from the FieldDescription. |
||
156 | * |
||
157 | * @param object $object |
||
158 | * @param array $params |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | public function renderListElement( |
||
181 | |||
182 | /** |
||
183 | * render a view element. |
||
184 | * |
||
185 | * @param object $object |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function renderViewElement( |
||
190 | Environment $environment, |
||
191 | FieldDescriptionInterface $fieldDescription, |
||
192 | $object |
||
193 | ) { |
||
194 | $template = $this->getTemplate( |
||
195 | $fieldDescription, |
||
196 | '@SonataAdmin/CRUD/base_show_field.html.twig', |
||
197 | $environment |
||
198 | ); |
||
199 | |||
200 | return $this->render($fieldDescription, $template, [ |
||
201 | 'field_description' => $fieldDescription, |
||
202 | 'object' => $object, |
||
203 | 'value' => $fieldDescription->getValue($object), |
||
204 | 'admin' => $fieldDescription->getAdmin(), |
||
205 | ], $environment); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * render a compared view element. |
||
210 | * |
||
211 | * @param mixed $baseObject |
||
212 | * @param mixed $compareObject |
||
213 | * |
||
214 | * @return string |
||
215 | */ |
||
216 | public function renderViewElementCompare( |
||
217 | Environment $environment, |
||
218 | FieldDescriptionInterface $fieldDescription, |
||
219 | $baseObject, |
||
220 | $compareObject |
||
221 | ) { |
||
222 | $template = $this->getTemplate( |
||
223 | $fieldDescription, |
||
224 | '@SonataAdmin/CRUD/base_show_field.html.twig', |
||
225 | $environment |
||
226 | ); |
||
227 | |||
228 | $baseValue = $fieldDescription->getValue($baseObject); |
||
229 | $compareValue = $fieldDescription->getValue($compareObject); |
||
230 | |||
231 | $baseValueOutput = $template->render([ |
||
232 | 'admin' => $fieldDescription->getAdmin(), |
||
233 | 'field_description' => $fieldDescription, |
||
234 | 'value' => $baseValue, |
||
235 | 'object' => $baseObject, |
||
236 | ]); |
||
237 | |||
238 | $compareValueOutput = $template->render([ |
||
239 | 'field_description' => $fieldDescription, |
||
240 | 'admin' => $fieldDescription->getAdmin(), |
||
241 | 'value' => $compareValue, |
||
242 | 'object' => $compareObject, |
||
243 | ]); |
||
244 | |||
245 | // Compare the rendered output of both objects by using the (possibly) overridden field block |
||
246 | $isDiff = $baseValueOutput !== $compareValueOutput; |
||
247 | |||
248 | return $this->render($fieldDescription, $template, [ |
||
249 | 'field_description' => $fieldDescription, |
||
250 | 'value' => $baseValue, |
||
251 | 'value_compare' => $compareValue, |
||
252 | 'is_diff' => $isDiff, |
||
253 | 'admin' => $fieldDescription->getAdmin(), |
||
254 | 'object' => $baseObject, |
||
255 | ], $environment); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @param mixed $element |
||
260 | * |
||
261 | * @throws \RuntimeException |
||
262 | * |
||
263 | * @return mixed |
||
264 | */ |
||
265 | public function renderRelationElement($element, FieldDescriptionInterface $fieldDescription) |
||
266 | { |
||
267 | if (!\is_object($element)) { |
||
268 | return $element; |
||
269 | } |
||
270 | |||
271 | $propertyPath = $fieldDescription->getOption('associated_property'); |
||
272 | |||
273 | if (null === $propertyPath) { |
||
274 | // For BC kept associated_tostring option behavior |
||
275 | $method = $fieldDescription->getOption('associated_tostring'); |
||
276 | |||
277 | if ($method) { |
||
278 | @trigger_error( |
||
|
|||
279 | 'Option "associated_tostring" is deprecated since version 2.3 and will be removed in 4.0. Use "associated_property" instead.', |
||
280 | E_USER_DEPRECATED |
||
281 | ); |
||
282 | } else { |
||
283 | $method = '__toString'; |
||
284 | } |
||
285 | |||
286 | if (!method_exists($element, $method)) { |
||
287 | throw new \RuntimeException(sprintf( |
||
288 | 'You must define an `associated_property` option or create a `%s::__toString` method' |
||
289 | .' to the field option %s from service %s is ', |
||
290 | \get_class($element), |
||
291 | $fieldDescription->getName(), |
||
292 | $fieldDescription->getAdmin()->getCode() |
||
293 | )); |
||
294 | } |
||
295 | |||
296 | return $element->$method(); |
||
297 | } |
||
298 | |||
299 | if (\is_callable($propertyPath)) { |
||
300 | return $propertyPath($element); |
||
301 | } |
||
302 | |||
303 | return $this->pool->getPropertyAccessor()->getValue($element, $propertyPath); |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Get the identifiers as a string that is safe to use in a url. |
||
308 | * |
||
309 | * @param object $model |
||
310 | * |
||
311 | * @return string string representation of the id that is safe to use in a url |
||
312 | */ |
||
313 | public function getUrlSafeIdentifier($model, ?AdminInterface $admin = null) |
||
314 | { |
||
315 | if (null === $admin) { |
||
316 | $class = ClassUtils::getClass($model); |
||
317 | if (!$this->pool->hasAdminByClass($class)) { |
||
318 | throw new \InvalidArgumentException('You must pass an admin.'); |
||
319 | } |
||
320 | |||
321 | $admin = $this->pool->getAdminByClass($class); |
||
322 | } |
||
323 | |||
324 | return $admin->getUrlSafeIdentifier($model); |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * @param string[] $xEditableTypeMapping |
||
329 | */ |
||
330 | public function setXEditableTypeMapping($xEditableTypeMapping): void |
||
331 | { |
||
332 | $this->xEditableTypeMapping = $xEditableTypeMapping; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @return string|bool |
||
337 | */ |
||
338 | public function getXEditableType($type) |
||
339 | { |
||
340 | return isset($this->xEditableTypeMapping[$type]) ? $this->xEditableTypeMapping[$type] : false; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Return xEditable choices based on the field description choices options & catalogue options. |
||
345 | * With the following choice options: |
||
346 | * ['Status1' => 'Alias1', 'Status2' => 'Alias2'] |
||
347 | * The method will return: |
||
348 | * [['value' => 'Status1', 'text' => 'Alias1'], ['value' => 'Status2', 'text' => 'Alias2']]. |
||
349 | * |
||
350 | * @return array |
||
351 | */ |
||
352 | public function getXEditableChoices(FieldDescriptionInterface $fieldDescription) |
||
353 | { |
||
354 | $choices = $fieldDescription->getOption('choices', []); |
||
355 | $catalogue = $fieldDescription->getOption('catalogue'); |
||
356 | $xEditableChoices = []; |
||
357 | if (!empty($choices)) { |
||
358 | reset($choices); |
||
359 | $first = current($choices); |
||
360 | // the choices are already in the right format |
||
361 | if (\is_array($first) && \array_key_exists('value', $first) && \array_key_exists('text', $first)) { |
||
362 | $xEditableChoices = $choices; |
||
363 | } else { |
||
364 | foreach ($choices as $value => $text) { |
||
365 | if ($catalogue) { |
||
366 | $text = $this->translator->trans($text, [], $catalogue); |
||
367 | } |
||
368 | |||
369 | $xEditableChoices[] = [ |
||
370 | 'value' => $value, |
||
371 | 'text' => $text, |
||
372 | ]; |
||
373 | } |
||
374 | } |
||
375 | } |
||
376 | |||
377 | if (false === $fieldDescription->getOption('required', true) |
||
378 | && false === $fieldDescription->getOption('multiple', false) |
||
379 | ) { |
||
380 | $xEditableChoices = array_merge([[ |
||
381 | 'value' => '', |
||
382 | 'text' => '', |
||
383 | ]], $xEditableChoices); |
||
384 | } |
||
385 | |||
386 | return $xEditableChoices; |
||
387 | } |
||
388 | |||
389 | /* |
||
390 | * Returns a canonicalized locale for "moment" NPM library, |
||
391 | * or `null` if the locale's language is "en", which doesn't require localization. |
||
392 | * |
||
393 | * @return string|null |
||
394 | */ |
||
395 | public function getCanonicalizedLocaleForMoment(array $context) |
||
412 | |||
413 | /** |
||
414 | * Returns a canonicalized locale for "select2" NPM library, |
||
415 | * or `null` if the locale's language is "en", which doesn't require localization. |
||
416 | * |
||
417 | * @return string|null |
||
418 | */ |
||
419 | public function getCanonicalizedLocaleForSelect2(array $context) |
||
446 | |||
447 | /** |
||
448 | * @param string|array $role |
||
449 | * @param object|null $object |
||
450 | * @param string|null $field |
||
451 | * |
||
452 | * @return bool |
||
453 | */ |
||
454 | public function isGrantedAffirmative($role, $object = null, $field = null) |
||
480 | |||
481 | /** |
||
482 | * return the value related to FieldDescription, if the associated object does no |
||
483 | * exists => a temporary one is created. |
||
484 | * |
||
485 | * @param object $object |
||
486 | * |
||
487 | * @throws \RuntimeException |
||
488 | * |
||
489 | * @return mixed |
||
490 | */ |
||
491 | private function getValueFromFieldDescription( |
||
512 | |||
513 | /** |
||
514 | * Get template. |
||
515 | * |
||
516 | * @param string $defaultTemplate |
||
517 | * |
||
518 | * @return TemplateWrapper |
||
519 | */ |
||
520 | private function getTemplate( |
||
550 | |||
551 | private function render( |
||
583 | |||
584 | /** |
||
585 | * @throws ServiceCircularReferenceException |
||
586 | * @throws ServiceNotFoundException |
||
587 | */ |
||
588 | private function getTemplateRegistry(string $adminCode): TemplateRegistryInterface |
||
599 | } |
||
600 |
If you suppress an error, we recommend checking for the error condition explicitly: