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 |
||
| 41 | final class SonataAdminExtension extends AbstractExtension |
||
| 42 | { |
||
| 43 | // @todo: there are more locales which are not supported by moment and they need to be translated/normalized/canonicalized here |
||
| 44 | public const MOMENT_UNSUPPORTED_LOCALES = [ |
||
| 45 | 'de' => ['de', 'de-at'], |
||
| 46 | 'es' => ['es', 'es-do'], |
||
| 47 | 'nl' => ['nl', 'nl-be'], |
||
| 48 | 'fr' => ['fr', 'fr-ca', 'fr-ch'], |
||
| 49 | ]; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var TranslatorInterface|null |
||
| 53 | */ |
||
| 54 | protected $translator; |
||
| 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( |
||
| 81 | Pool $pool, |
||
| 82 | ?LoggerInterface $logger = null, |
||
| 83 | TranslatorInterface $translator, |
||
| 84 | ?ContainerInterface $templateRegistries = null, |
||
| 85 | ?AuthorizationCheckerInterface $securityChecker = null |
||
| 86 | ) { |
||
| 87 | $this->pool = $pool; |
||
| 88 | $this->logger = $logger; |
||
| 89 | $this->translator = $translator; |
||
| 90 | $this->templateRegistries = $templateRegistries; |
||
| 91 | $this->securityChecker = $securityChecker; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function getFilters() |
||
| 95 | { |
||
| 96 | return [ |
||
| 97 | new TwigFilter( |
||
| 98 | 'render_list_element', |
||
| 99 | [$this, 'renderListElement'], |
||
| 100 | [ |
||
| 101 | 'is_safe' => ['html'], |
||
| 102 | 'needs_environment' => true, |
||
| 103 | ] |
||
| 104 | ), |
||
| 105 | new TwigFilter( |
||
| 106 | 'render_view_element', |
||
| 107 | [$this, 'renderViewElement'], |
||
| 108 | [ |
||
| 109 | 'is_safe' => ['html'], |
||
| 110 | 'needs_environment' => true, |
||
| 111 | ] |
||
| 112 | ), |
||
| 113 | new TwigFilter( |
||
| 114 | 'render_view_element_compare', |
||
| 115 | [$this, 'renderViewElementCompare'], |
||
| 116 | [ |
||
| 117 | 'is_safe' => ['html'], |
||
| 118 | 'needs_environment' => true, |
||
| 119 | ] |
||
| 120 | ), |
||
| 121 | new TwigFilter( |
||
| 122 | 'render_relation_element', |
||
| 123 | [$this, 'renderRelationElement'] |
||
| 124 | ), |
||
| 125 | new TwigFilter( |
||
| 126 | 'sonata_urlsafeid', |
||
| 127 | [$this, 'getUrlSafeIdentifier'] |
||
| 128 | ), |
||
| 129 | new TwigFilter( |
||
| 130 | 'sonata_xeditable_type', |
||
| 131 | [$this, 'getXEditableType'] |
||
| 132 | ), |
||
| 133 | new TwigFilter( |
||
| 134 | 'sonata_xeditable_choices', |
||
| 135 | [$this, 'getXEditableChoices'] |
||
| 136 | ), |
||
| 137 | ]; |
||
| 138 | } |
||
| 139 | |||
| 140 | public function getFunctions() |
||
| 141 | { |
||
| 142 | return [ |
||
| 143 | new TwigFunction('canonicalize_locale_for_moment', [$this, 'getCanonicalizedLocaleForMoment'], ['needs_context' => true]), |
||
| 144 | new TwigFunction('canonicalize_locale_for_select2', [$this, 'getCanonicalizedLocaleForSelect2'], ['needs_context' => true]), |
||
| 145 | new TwigFunction('is_granted_affirmative', [$this, 'isGrantedAffirmative']), |
||
| 146 | ]; |
||
| 147 | } |
||
| 148 | |||
| 149 | public function getName() |
||
| 150 | { |
||
| 151 | return 'sonata_admin'; |
||
| 152 | } |
||
| 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( |
||
| 163 | Environment $environment, |
||
| 164 | $object, |
||
| 165 | FieldDescriptionInterface $fieldDescription, |
||
| 166 | $params = [] |
||
| 167 | ) { |
||
| 168 | $template = $this->getTemplate( |
||
| 169 | $fieldDescription, |
||
| 170 | $this->getTemplateRegistry($fieldDescription->getAdmin()->getCode())->getTemplate('base_list_field'), |
||
| 171 | $environment |
||
| 172 | ); |
||
| 173 | |||
| 174 | return $this->render($fieldDescription, $template, array_merge($params, [ |
||
| 175 | 'admin' => $fieldDescription->getAdmin(), |
||
| 176 | 'object' => $object, |
||
| 177 | 'value' => $this->getValueFromFieldDescription($object, $fieldDescription), |
||
| 178 | 'field_description' => $fieldDescription, |
||
| 179 | ]), $environment); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * render a view element. |
||
| 184 | * |
||
| 185 | * @param object $object |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function renderViewElement( |
||
| 213 | |||
| 214 | /** |
||
| 215 | * render a compared view element. |
||
| 216 | * |
||
| 217 | * @param mixed $baseObject |
||
| 218 | * @param mixed $compareObject |
||
| 219 | * |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | public function renderViewElementCompare( |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param mixed $element |
||
| 272 | * |
||
| 273 | * @throws \RuntimeException |
||
| 274 | * |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | public function renderRelationElement($element, FieldDescriptionInterface $fieldDescription) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Get the identifiers as a string that is safe to use in a url. |
||
| 321 | * |
||
| 322 | * @param object $model |
||
| 323 | * |
||
| 324 | * @return string string representation of the id that is safe to use in a url |
||
| 325 | */ |
||
| 326 | public function getUrlSafeIdentifier($model, ?AdminInterface $admin = null) |
||
| 327 | { |
||
| 328 | if (null === $admin) { |
||
| 329 | $admin = $this->pool->getAdminByClass(ClassUtils::getClass($model)); |
||
| 330 | } |
||
| 331 | |||
| 332 | return $admin->getUrlSafeIdentifier($model); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param string[] $xEditableTypeMapping |
||
| 337 | */ |
||
| 338 | public function setXEditableTypeMapping($xEditableTypeMapping): void |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return string|bool |
||
| 345 | */ |
||
| 346 | public function getXEditableType($type) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Return xEditable choices based on the field description choices options & catalogue options. |
||
| 353 | * With the following choice options: |
||
| 354 | * ['Status1' => 'Alias1', 'Status2' => 'Alias2'] |
||
| 355 | * The method will return: |
||
| 356 | * [['value' => 'Status1', 'text' => 'Alias1'], ['value' => 'Status2', 'text' => 'Alias2']]. |
||
| 357 | * |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | public function getXEditableChoices(FieldDescriptionInterface $fieldDescription) |
||
| 401 | |||
| 402 | /* |
||
| 403 | * Returns a canonicalized locale for "moment" NPM library, |
||
| 404 | * or `null` if the locale's language is "en", which doesn't require localization. |
||
| 405 | * |
||
| 406 | * @return string|null |
||
| 407 | */ |
||
| 408 | public function getCanonicalizedLocaleForMoment(array $context) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Returns a canonicalized locale for "select2" NPM library, |
||
| 428 | * or `null` if the locale's language is "en", which doesn't require localization. |
||
| 429 | * |
||
| 430 | * @return string|null |
||
| 431 | */ |
||
| 432 | public function getCanonicalizedLocaleForSelect2(array $context) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param string|array $role |
||
| 462 | * @param object|null $object |
||
| 463 | * @param string|null $field |
||
| 464 | * |
||
| 465 | * @return bool |
||
| 466 | */ |
||
| 467 | public function isGrantedAffirmative($role, $object = null, $field = null) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * return the value related to FieldDescription, if the associated object does no |
||
| 496 | * exists => a temporary one is created. |
||
| 497 | * |
||
| 498 | * @param object $object |
||
| 499 | * |
||
| 500 | * @throws \RuntimeException |
||
| 501 | * |
||
| 502 | * @return mixed |
||
| 503 | */ |
||
| 504 | private function getValueFromFieldDescription( |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Get template. |
||
| 528 | * |
||
| 529 | * @param string $defaultTemplate |
||
| 530 | * |
||
| 531 | * @return TemplateWrapper |
||
| 532 | */ |
||
| 533 | private function getTemplate( |
||
| 567 | |||
| 568 | private function render( |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @throws ServiceCircularReferenceException |
||
| 603 | * @throws ServiceNotFoundException |
||
| 604 | */ |
||
| 605 | private function getTemplateRegistry(string $adminCode): TemplateRegistryInterface |
||
| 616 | } |
||
| 617 |
If you suppress an error, we recommend checking for the error condition explicitly: