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