Complex classes like AddDependencyCallsCompilerPass 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 AddDependencyCallsCompilerPass, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | final class AddDependencyCallsCompilerPass implements CompilerPassInterface |
||
33 | { |
||
34 | public function process(ContainerBuilder $container): void |
||
35 | { |
||
36 | // check if translator service exist |
||
37 | if (!$container->has('translator')) { |
||
38 | throw new \RuntimeException('The "translator" service is not yet enabled. |
||
39 | It\'s required by SonataAdmin to display all labels properly. |
||
40 | |||
41 | To learn how to enable the translator service please visit: |
||
42 | http://symfony.com/doc/current/translation.html#configuration |
||
43 | '); |
||
44 | } |
||
45 | |||
46 | $parameterBag = $container->getParameterBag(); |
||
47 | $groupDefaults = $admins = $classes = []; |
||
48 | |||
49 | $pool = $container->getDefinition('sonata.admin.pool'); |
||
50 | |||
51 | foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $tags) { |
||
52 | foreach ($tags as $attributes) { |
||
53 | $definition = $container->getDefinition($id); |
||
54 | $parentDefinition = null; |
||
55 | |||
56 | // Temporary fix until we can support service locators |
||
57 | $definition->setPublic(true); |
||
58 | |||
59 | // NEXT_MAJOR: Remove check for DefinitionDecorator instance when dropping Symfony <3.3 support |
||
60 | if ($definition instanceof ChildDefinition || |
||
61 | (!class_exists(ChildDefinition::class) && $definition instanceof DefinitionDecorator)) { |
||
62 | $parentDefinition = $container->getDefinition($definition->getParent()); |
||
|
|||
63 | } |
||
64 | |||
65 | $this->replaceDefaultArguments([ |
||
66 | 0 => $id, |
||
67 | 2 => 'SonataAdminBundle:CRUD', |
||
68 | ], $definition, $parentDefinition); |
||
69 | $this->applyConfigurationFromAttribute($definition, $attributes); |
||
70 | $this->applyDefaults($container, $id, $attributes); |
||
71 | |||
72 | $arguments = $parentDefinition ? |
||
73 | array_merge($parentDefinition->getArguments(), $definition->getArguments()) : |
||
74 | $definition->getArguments(); |
||
75 | |||
76 | $admins[] = $id; |
||
77 | |||
78 | if (!isset($classes[$arguments[1]])) { |
||
79 | $classes[$arguments[1]] = []; |
||
80 | } |
||
81 | |||
82 | $classes[$arguments[1]][] = $id; |
||
83 | |||
84 | $showInDashboard = (bool) (isset($attributes['show_in_dashboard']) ? $parameterBag->resolveValue($attributes['show_in_dashboard']) : true); |
||
85 | if (!$showInDashboard) { |
||
86 | continue; |
||
87 | } |
||
88 | |||
89 | $resolvedGroupName = isset($attributes['group']) ? $parameterBag->resolveValue($attributes['group']) : 'default'; |
||
90 | $labelCatalogue = $attributes['label_catalogue'] ?? 'SonataAdminBundle'; |
||
91 | $icon = $attributes['icon'] ?? '<i class="fa fa-folder"></i>'; |
||
92 | $onTop = $attributes['on_top'] ?? false; |
||
93 | $keepOpen = $attributes['keep_open'] ?? false; |
||
94 | |||
95 | if (!isset($groupDefaults[$resolvedGroupName])) { |
||
96 | $groupDefaults[$resolvedGroupName] = [ |
||
97 | 'label' => $resolvedGroupName, |
||
98 | 'label_catalogue' => $labelCatalogue, |
||
99 | 'icon' => $icon, |
||
100 | 'roles' => [], |
||
101 | 'on_top' => false, |
||
102 | 'keep_open' => false, |
||
103 | ]; |
||
104 | } |
||
105 | |||
106 | $groupDefaults[$resolvedGroupName]['items'][] = [ |
||
107 | 'admin' => $id, |
||
108 | 'label' => !empty($attributes['label']) ? $attributes['label'] : '', |
||
109 | 'route' => '', |
||
110 | 'route_params' => [], |
||
111 | 'route_absolute' => false, |
||
112 | ]; |
||
113 | |||
114 | if (isset($groupDefaults[$resolvedGroupName]['on_top']) && $groupDefaults[$resolvedGroupName]['on_top'] |
||
115 | || $onTop && (count($groupDefaults[$resolvedGroupName]['items']) > 1)) { |
||
116 | throw new \RuntimeException('You can\'t use "on_top" option with multiple same name groups.'); |
||
117 | } |
||
118 | $groupDefaults[$resolvedGroupName]['on_top'] = $onTop; |
||
119 | |||
120 | $groupDefaults[$resolvedGroupName]['keep_open'] = $keepOpen; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | $dashboardGroupsSettings = $container->getParameter('sonata.admin.configuration.dashboard_groups'); |
||
125 | if (!empty($dashboardGroupsSettings)) { |
||
126 | $groups = $dashboardGroupsSettings; |
||
127 | |||
128 | foreach ($dashboardGroupsSettings as $groupName => $group) { |
||
129 | $resolvedGroupName = $parameterBag->resolveValue($groupName); |
||
130 | if (!isset($groupDefaults[$resolvedGroupName])) { |
||
131 | $groupDefaults[$resolvedGroupName] = [ |
||
132 | 'items' => [], |
||
133 | 'label' => $resolvedGroupName, |
||
134 | 'roles' => [], |
||
135 | 'on_top' => false, |
||
136 | 'keep_open' => false, |
||
137 | ]; |
||
138 | } |
||
139 | |||
140 | if (empty($group['items'])) { |
||
141 | $groups[$resolvedGroupName]['items'] = $groupDefaults[$resolvedGroupName]['items']; |
||
142 | } |
||
143 | |||
144 | if (empty($group['label'])) { |
||
145 | $groups[$resolvedGroupName]['label'] = $groupDefaults[$resolvedGroupName]['label']; |
||
146 | } |
||
147 | |||
148 | if (empty($group['label_catalogue'])) { |
||
149 | $groups[$resolvedGroupName]['label_catalogue'] = 'SonataAdminBundle'; |
||
150 | } |
||
151 | |||
152 | if (empty($group['icon'])) { |
||
153 | $groups[$resolvedGroupName]['icon'] = $groupDefaults[$resolvedGroupName]['icon']; |
||
154 | } |
||
155 | |||
156 | if (!empty($group['item_adds'])) { |
||
157 | $groups[$resolvedGroupName]['items'] = array_merge($groups[$resolvedGroupName]['items'], $group['item_adds']); |
||
158 | } |
||
159 | |||
160 | if (empty($group['roles'])) { |
||
161 | $groups[$resolvedGroupName]['roles'] = $groupDefaults[$resolvedGroupName]['roles']; |
||
162 | } |
||
163 | |||
164 | if (isset($groups[$resolvedGroupName]['on_top']) && !empty($group['on_top']) && $group['on_top'] |
||
165 | && (count($groups[$resolvedGroupName]['items']) > 1)) { |
||
166 | throw new \RuntimeException('You can\'t use "on_top" option with multiple same name groups.'); |
||
167 | } |
||
168 | if (empty($group['on_top'])) { |
||
169 | $groups[$resolvedGroupName]['on_top'] = $groupDefaults[$resolvedGroupName]['on_top']; |
||
170 | } |
||
171 | |||
172 | if (empty($group['keep_open'])) { |
||
173 | $groups[$resolvedGroupName]['keep_open'] = $groupDefaults[$resolvedGroupName]['keep_open']; |
||
174 | } |
||
175 | } |
||
176 | } elseif ($container->getParameter('sonata.admin.configuration.sort_admins')) { |
||
177 | $groups = $groupDefaults; |
||
178 | |||
179 | $elementSort = function (&$element): void { |
||
180 | usort( |
||
181 | $element['items'], |
||
182 | function ($a, $b) { |
||
183 | $a = !empty($a['label']) ? $a['label'] : $a['admin']; |
||
184 | $b = !empty($b['label']) ? $b['label'] : $b['admin']; |
||
185 | |||
186 | if ($a === $b) { |
||
187 | return 0; |
||
188 | } |
||
189 | |||
190 | return $a < $b ? -1 : 1; |
||
191 | } |
||
192 | ); |
||
193 | }; |
||
194 | |||
195 | /* |
||
196 | * 1) sort the groups by their index |
||
197 | * 2) sort the elements within each group by label/admin |
||
198 | */ |
||
199 | ksort($groups); |
||
200 | array_walk($groups, $elementSort); |
||
201 | } else { |
||
202 | $groups = $groupDefaults; |
||
203 | } |
||
204 | |||
205 | $pool->addMethodCall('setAdminServiceIds', [$admins]); |
||
206 | $pool->addMethodCall('setAdminGroups', [$groups]); |
||
207 | $pool->addMethodCall('setAdminClasses', [$classes]); |
||
208 | |||
209 | $routeLoader = $container->getDefinition('sonata.admin.route_loader'); |
||
210 | $routeLoader->replaceArgument(1, $admins); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * This method read the attribute keys and configure admin class to use the related dependency. |
||
215 | */ |
||
216 | public function applyConfigurationFromAttribute(Definition $definition, array $attributes): void |
||
243 | |||
244 | /** |
||
245 | * Apply the default values required by the AdminInterface to the Admin service definition. |
||
246 | * |
||
247 | * @param string $serviceId |
||
248 | * |
||
249 | * @return Definition |
||
250 | */ |
||
251 | public function applyDefaults(ContainerBuilder $container, $serviceId, array $attributes = []) |
||
354 | |||
355 | public function fixTemplates( |
||
411 | |||
412 | /** |
||
413 | * Replace the empty arguments required by the Admin service definition. |
||
414 | */ |
||
415 | private function replaceDefaultArguments( |
||
434 | } |
||
435 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: