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 |
||
38 | final class SonataAdminExtension extends AbstractExtension |
||
39 | { |
||
40 | // @todo: there are more locales which are not supported by moment and they need to be translated/normalized/canonicalized here |
||
41 | public const MOMENT_UNSUPPORTED_LOCALES = [ |
||
42 | 'de' => ['de', 'de-at'], |
||
43 | 'es' => ['es', 'es-do'], |
||
44 | 'nl' => ['nl', 'nl-be'], |
||
45 | 'fr' => ['fr', 'fr-ca', 'fr-ch'], |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * @var TranslatorInterface |
||
50 | */ |
||
51 | private $translator; |
||
52 | |||
53 | /** |
||
54 | * @var Pool |
||
55 | */ |
||
56 | private $pool; |
||
57 | |||
58 | /** |
||
59 | * @var string[] |
||
60 | */ |
||
61 | private $xEditableTypeMapping = []; |
||
62 | |||
63 | /** |
||
64 | * @var ContainerInterface |
||
65 | */ |
||
66 | private $templateRegistries; |
||
67 | |||
68 | /** |
||
69 | * @var AuthorizationCheckerInterface |
||
70 | */ |
||
71 | private $securityChecker; |
||
72 | |||
73 | public function __construct( |
||
74 | Pool $pool, |
||
75 | TranslatorInterface $translator, |
||
76 | ?ContainerInterface $templateRegistries = null, |
||
77 | ?AuthorizationCheckerInterface $securityChecker = null |
||
78 | ) { |
||
79 | $this->pool = $pool; |
||
80 | $this->translator = $translator; |
||
81 | $this->templateRegistries = $templateRegistries; |
||
82 | $this->securityChecker = $securityChecker; |
||
83 | } |
||
84 | |||
85 | public function getFilters() |
||
86 | { |
||
87 | return [ |
||
88 | new TwigFilter( |
||
89 | 'render_list_element', |
||
90 | [$this, 'renderListElement'], |
||
91 | [ |
||
92 | 'is_safe' => ['html'], |
||
93 | 'needs_environment' => true, |
||
94 | ] |
||
95 | ), |
||
96 | new TwigFilter( |
||
97 | 'render_view_element', |
||
98 | [$this, 'renderViewElement'], |
||
99 | [ |
||
100 | 'is_safe' => ['html'], |
||
101 | 'needs_environment' => true, |
||
102 | ] |
||
103 | ), |
||
104 | new TwigFilter( |
||
105 | 'render_view_element_compare', |
||
106 | [$this, 'renderViewElementCompare'], |
||
107 | [ |
||
108 | 'is_safe' => ['html'], |
||
109 | 'needs_environment' => true, |
||
110 | ] |
||
111 | ), |
||
112 | new TwigFilter( |
||
113 | 'render_relation_element', |
||
114 | [$this, 'renderRelationElement'] |
||
115 | ), |
||
116 | new TwigFilter( |
||
117 | 'sonata_urlsafeid', |
||
118 | [$this, 'getUrlSafeIdentifier'] |
||
119 | ), |
||
120 | new TwigFilter( |
||
121 | 'sonata_xeditable_type', |
||
122 | [$this, 'getXEditableType'] |
||
123 | ), |
||
124 | new TwigFilter( |
||
125 | 'sonata_xeditable_choices', |
||
126 | [$this, 'getXEditableChoices'] |
||
127 | ), |
||
128 | ]; |
||
129 | } |
||
130 | |||
131 | public function getFunctions() |
||
132 | { |
||
133 | return [ |
||
134 | new TwigFunction('canonicalize_locale_for_moment', [$this, 'getCanonicalizedLocaleForMoment'], ['needs_context' => true]), |
||
135 | new TwigFunction('canonicalize_locale_for_select2', [$this, 'getCanonicalizedLocaleForSelect2'], ['needs_context' => true]), |
||
136 | new TwigFunction('is_granted_affirmative', [$this, 'isGrantedAffirmative']), |
||
137 | ]; |
||
138 | } |
||
139 | |||
140 | public function getName() |
||
141 | { |
||
142 | return 'sonata_admin'; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * render a list element from the FieldDescription. |
||
147 | * |
||
148 | * @param object|array $listElement |
||
149 | * @param array $params |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | public function renderListElement( |
||
154 | Environment $environment, |
||
155 | $listElement, |
||
156 | FieldDescriptionInterface $fieldDescription, |
||
157 | $params = [] |
||
158 | ) { |
||
159 | $template = $this->getTemplate( |
||
160 | $fieldDescription, |
||
161 | $this->getTemplateRegistry($fieldDescription->getAdmin()->getCode())->getTemplate('base_list_field'), |
||
162 | $environment |
||
163 | ); |
||
164 | |||
165 | [$object, $value] = $this->getObjectAndValueFromListElement($listElement, $fieldDescription); |
||
166 | |||
167 | return $this->render($fieldDescription, $template, array_merge($params, [ |
||
168 | 'admin' => $fieldDescription->getAdmin(), |
||
169 | 'object' => $object, |
||
170 | 'value' => $value, |
||
171 | 'field_description' => $fieldDescription, |
||
172 | ]), $environment); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * render a view element. |
||
177 | * |
||
178 | * @param object $object |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | public function renderViewElement( |
||
200 | |||
201 | /** |
||
202 | * render a compared view element. |
||
203 | * |
||
204 | * @param mixed $baseObject |
||
205 | * @param mixed $compareObject |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | public function renderViewElementCompare( |
||
250 | |||
251 | /** |
||
252 | * @param mixed $element |
||
253 | * |
||
254 | * @throws \RuntimeException |
||
255 | * |
||
256 | * @return mixed |
||
257 | */ |
||
258 | public function renderRelationElement($element, FieldDescriptionInterface $fieldDescription) |
||
288 | |||
289 | /** |
||
290 | * Get the identifiers as a string that is safe to use in a url. |
||
291 | * |
||
292 | * @param object $model |
||
293 | * |
||
294 | * @return string string representation of the id that is safe to use in a url |
||
295 | */ |
||
296 | public function getUrlSafeIdentifier($model, ?AdminInterface $admin = null) |
||
309 | |||
310 | /** |
||
311 | * @param string[] $xEditableTypeMapping |
||
312 | */ |
||
313 | public function setXEditableTypeMapping($xEditableTypeMapping): void |
||
317 | |||
318 | /** |
||
319 | * @return string|bool |
||
320 | */ |
||
321 | public function getXEditableType($type) |
||
325 | |||
326 | /** |
||
327 | * Return xEditable choices based on the field description choices options & catalogue options. |
||
328 | * With the following choice options: |
||
329 | * ['Status1' => 'Alias1', 'Status2' => 'Alias2'] |
||
330 | * The method will return: |
||
331 | * [['value' => 'Status1', 'text' => 'Alias1'], ['value' => 'Status2', 'text' => 'Alias2']]. |
||
332 | * |
||
333 | * @return array |
||
334 | */ |
||
335 | public function getXEditableChoices(FieldDescriptionInterface $fieldDescription) |
||
371 | |||
372 | /* |
||
373 | * Returns a canonicalized locale for "moment" NPM library, |
||
374 | * or `null` if the locale's language is "en", which doesn't require localization. |
||
375 | * |
||
376 | * @return string|null |
||
377 | */ |
||
378 | public function getCanonicalizedLocaleForMoment(array $context) |
||
395 | |||
396 | /** |
||
397 | * Returns a canonicalized locale for "select2" NPM library, |
||
398 | * or `null` if the locale's language is "en", which doesn't require localization. |
||
399 | * |
||
400 | * @return string|null |
||
401 | */ |
||
402 | public function getCanonicalizedLocaleForSelect2(array $context) |
||
429 | |||
430 | /** |
||
431 | * @param string|array $role |
||
432 | * @param object|null $object |
||
433 | * @param string|null $field |
||
434 | * |
||
435 | * @return bool |
||
436 | */ |
||
437 | public function isGrantedAffirmative($role, $object = null, $field = null) |
||
463 | |||
464 | /** |
||
465 | * return the value related to FieldDescription, if the associated object does no |
||
466 | * exists => a temporary one is created. |
||
467 | * |
||
468 | * @param object $object |
||
469 | * |
||
470 | * @throws \RuntimeException |
||
471 | * |
||
472 | * @return mixed |
||
473 | */ |
||
474 | private function getValueFromFieldDescription( |
||
495 | |||
496 | /** |
||
497 | * Get template. |
||
498 | * |
||
499 | * @param string $defaultTemplate |
||
500 | * |
||
501 | * @return TemplateWrapper |
||
502 | */ |
||
503 | private function getTemplate( |
||
512 | |||
513 | private function render( |
||
545 | |||
546 | /** |
||
547 | * @throws ServiceCircularReferenceException |
||
548 | * @throws ServiceNotFoundException |
||
549 | */ |
||
550 | private function getTemplateRegistry(string $adminCode): TemplateRegistryInterface |
||
561 | |||
562 | /** |
||
563 | * Extracts the object and requested value from the $listElement. |
||
564 | * |
||
565 | * @param object|array $listElement |
||
566 | * |
||
567 | * @throws \TypeError when $listElement is not an object or an array with an object on offset 0 |
||
568 | * |
||
569 | * @return array An array containing object and value |
||
570 | */ |
||
571 | private function getObjectAndValueFromListElement( |
||
595 | } |
||
596 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.