Conditions | 38 |
Paths | 19255 |
Total Lines | 178 |
Code Lines | 105 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
36 | public function process(ContainerBuilder $container): void |
||
37 | { |
||
38 | // check if translator service exist |
||
39 | if (!$container->has('translator')) { |
||
40 | throw new \RuntimeException('The "translator" service is not yet enabled. |
||
41 | It\'s required by SonataAdmin to display all labels properly. |
||
42 | |||
43 | To learn how to enable the translator service please visit: |
||
44 | http://symfony.com/doc/current/translation.html#configuration |
||
45 | '); |
||
46 | } |
||
47 | |||
48 | $parameterBag = $container->getParameterBag(); |
||
49 | $groupDefaults = $admins = $classes = []; |
||
50 | |||
51 | $pool = $container->getDefinition('sonata.admin.pool'); |
||
52 | |||
53 | foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $tags) { |
||
54 | foreach ($tags as $attributes) { |
||
55 | $definition = $container->getDefinition($id); |
||
56 | $parentDefinition = null; |
||
57 | |||
58 | // Temporary fix until we can support service locators |
||
59 | $definition->setPublic(true); |
||
60 | |||
61 | // NEXT_MAJOR: Remove check for DefinitionDecorator instance when dropping Symfony <3.3 support |
||
62 | if ($definition instanceof ChildDefinition || |
||
63 | (!class_exists(ChildDefinition::class) && $definition instanceof DefinitionDecorator)) { |
||
64 | $parentDefinition = $container->getDefinition($definition->getParent()); |
||
|
|||
65 | } |
||
66 | |||
67 | $this->replaceDefaultArguments([ |
||
68 | 0 => $id, |
||
69 | 2 => 'SonataAdminBundle:CRUD', |
||
70 | ], $definition, $parentDefinition); |
||
71 | $this->applyConfigurationFromAttribute($definition, $attributes); |
||
72 | $this->applyDefaults($container, $id, $attributes); |
||
73 | |||
74 | $arguments = $parentDefinition ? |
||
75 | array_merge($parentDefinition->getArguments(), $definition->getArguments()) : |
||
76 | $definition->getArguments(); |
||
77 | |||
78 | $admins[] = $id; |
||
79 | |||
80 | if (!isset($classes[$arguments[1]])) { |
||
81 | $classes[$arguments[1]] = []; |
||
82 | } |
||
83 | |||
84 | $classes[$arguments[1]][] = $id; |
||
85 | |||
86 | $showInDashboard = (bool) (isset($attributes['show_in_dashboard']) ? $parameterBag->resolveValue($attributes['show_in_dashboard']) : true); |
||
87 | if (!$showInDashboard) { |
||
88 | continue; |
||
89 | } |
||
90 | |||
91 | $resolvedGroupName = isset($attributes['group']) ? $parameterBag->resolveValue($attributes['group']) : 'default'; |
||
92 | $labelCatalogue = $attributes['label_catalogue'] ?? 'SonataAdminBundle'; |
||
93 | $icon = $attributes['icon'] ?? '<i class="fa fa-folder"></i>'; |
||
94 | $onTop = $attributes['on_top'] ?? false; |
||
95 | $keepOpen = $attributes['keep_open'] ?? false; |
||
96 | |||
97 | if (!isset($groupDefaults[$resolvedGroupName])) { |
||
98 | $groupDefaults[$resolvedGroupName] = [ |
||
99 | 'label' => $resolvedGroupName, |
||
100 | 'label_catalogue' => $labelCatalogue, |
||
101 | 'icon' => $icon, |
||
102 | 'roles' => [], |
||
103 | 'on_top' => false, |
||
104 | 'keep_open' => false, |
||
105 | ]; |
||
106 | } |
||
107 | |||
108 | $groupDefaults[$resolvedGroupName]['items'][] = [ |
||
109 | 'admin' => $id, |
||
110 | 'label' => !empty($attributes['label']) ? $attributes['label'] : '', |
||
111 | 'route' => '', |
||
112 | 'route_params' => [], |
||
113 | 'route_absolute' => false, |
||
114 | ]; |
||
115 | |||
116 | if (isset($groupDefaults[$resolvedGroupName]['on_top']) && $groupDefaults[$resolvedGroupName]['on_top'] |
||
117 | || $onTop && (count($groupDefaults[$resolvedGroupName]['items']) > 1)) { |
||
118 | throw new \RuntimeException('You can\'t use "on_top" option with multiple same name groups.'); |
||
119 | } |
||
120 | $groupDefaults[$resolvedGroupName]['on_top'] = $onTop; |
||
121 | |||
122 | $groupDefaults[$resolvedGroupName]['keep_open'] = $keepOpen; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | $dashboardGroupsSettings = $container->getParameter('sonata.admin.configuration.dashboard_groups'); |
||
127 | if (!empty($dashboardGroupsSettings)) { |
||
128 | $groups = $dashboardGroupsSettings; |
||
129 | |||
130 | foreach ($dashboardGroupsSettings as $groupName => $group) { |
||
131 | $resolvedGroupName = $parameterBag->resolveValue($groupName); |
||
132 | if (!isset($groupDefaults[$resolvedGroupName])) { |
||
133 | $groupDefaults[$resolvedGroupName] = [ |
||
134 | 'items' => [], |
||
135 | 'label' => $resolvedGroupName, |
||
136 | 'roles' => [], |
||
137 | 'on_top' => false, |
||
138 | 'keep_open' => false, |
||
139 | ]; |
||
140 | } |
||
141 | |||
142 | if (empty($group['items'])) { |
||
143 | $groups[$resolvedGroupName]['items'] = $groupDefaults[$resolvedGroupName]['items']; |
||
144 | } |
||
145 | |||
146 | if (empty($group['label'])) { |
||
147 | $groups[$resolvedGroupName]['label'] = $groupDefaults[$resolvedGroupName]['label']; |
||
148 | } |
||
149 | |||
150 | if (empty($group['label_catalogue'])) { |
||
151 | $groups[$resolvedGroupName]['label_catalogue'] = 'SonataAdminBundle'; |
||
152 | } |
||
153 | |||
154 | if (empty($group['icon'])) { |
||
155 | $groups[$resolvedGroupName]['icon'] = $groupDefaults[$resolvedGroupName]['icon']; |
||
156 | } |
||
157 | |||
158 | if (!empty($group['item_adds'])) { |
||
159 | $groups[$resolvedGroupName]['items'] = array_merge($groups[$resolvedGroupName]['items'], $group['item_adds']); |
||
160 | } |
||
161 | |||
162 | if (empty($group['roles'])) { |
||
163 | $groups[$resolvedGroupName]['roles'] = $groupDefaults[$resolvedGroupName]['roles']; |
||
164 | } |
||
165 | |||
166 | if (isset($groups[$resolvedGroupName]['on_top']) && !empty($group['on_top']) && $group['on_top'] |
||
167 | && (count($groups[$resolvedGroupName]['items']) > 1)) { |
||
168 | throw new \RuntimeException('You can\'t use "on_top" option with multiple same name groups.'); |
||
169 | } |
||
170 | if (empty($group['on_top'])) { |
||
171 | $groups[$resolvedGroupName]['on_top'] = $groupDefaults[$resolvedGroupName]['on_top']; |
||
172 | } |
||
173 | |||
174 | if (empty($group['keep_open'])) { |
||
175 | $groups[$resolvedGroupName]['keep_open'] = $groupDefaults[$resolvedGroupName]['keep_open']; |
||
176 | } |
||
177 | } |
||
178 | } elseif ($container->getParameter('sonata.admin.configuration.sort_admins')) { |
||
179 | $groups = $groupDefaults; |
||
180 | |||
181 | $elementSort = function (&$element): void { |
||
182 | usort( |
||
183 | $element['items'], |
||
184 | function ($a, $b) { |
||
185 | $a = !empty($a['label']) ? $a['label'] : $a['admin']; |
||
186 | $b = !empty($b['label']) ? $b['label'] : $b['admin']; |
||
187 | |||
188 | if ($a === $b) { |
||
189 | return 0; |
||
190 | } |
||
191 | |||
192 | return $a < $b ? -1 : 1; |
||
193 | } |
||
194 | ); |
||
195 | }; |
||
196 | |||
197 | /* |
||
198 | * 1) sort the groups by their index |
||
199 | * 2) sort the elements within each group by label/admin |
||
200 | */ |
||
201 | ksort($groups); |
||
202 | array_walk($groups, $elementSort); |
||
203 | } else { |
||
204 | $groups = $groupDefaults; |
||
205 | } |
||
206 | |||
207 | $pool->addMethodCall('setAdminServiceIds', [$admins]); |
||
208 | $pool->addMethodCall('setAdminGroups', [$groups]); |
||
209 | $pool->addMethodCall('setAdminClasses', [$classes]); |
||
210 | |||
211 | $routeLoader = $container->getDefinition('sonata.admin.route_loader'); |
||
212 | $routeLoader->replaceArgument(1, $admins); |
||
213 | } |
||
214 | |||
423 |
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: