| Conditions | 11 |
| Paths | 4 |
| Total Lines | 98 |
| Code Lines | 62 |
| 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 |
||
| 72 | protected function resolveActionLinkConfiguration( |
||
| 73 | ActionConfiguration $actionConfiguration, |
||
| 74 | string $actionName, |
||
| 75 | array $actionLinkConfiguration = [] |
||
| 76 | ): array { |
||
| 77 | $translationPattern = $actionConfiguration |
||
| 78 | ->getAdminConfiguration() |
||
| 79 | ->getParameter('translation_pattern') |
||
| 80 | ; |
||
| 81 | |||
| 82 | $icon = ''; |
||
| 83 | $cssClass = ''; |
||
| 84 | $routeParameters = []; |
||
| 85 | |||
| 86 | if ('delete' === $actionName) { |
||
| 87 | $icon = 'remove'; |
||
| 88 | $cssClass = 'btn btn-danger btn-sm'; |
||
| 89 | $routeParameters = [ |
||
| 90 | 'id' => '', |
||
| 91 | ]; |
||
| 92 | } |
||
| 93 | if ('edit' === $actionName) { |
||
| 94 | $icon = 'pencil'; |
||
| 95 | $cssClass = 'btn btn-secondary btn-sm'; |
||
| 96 | } |
||
| 97 | |||
| 98 | $resolver = new OptionsResolver(); |
||
| 99 | $resolver |
||
| 100 | ->setDefaults([ |
||
| 101 | 'title' => StringUtils::getTranslationKey( |
||
| 102 | $translationPattern, |
||
| 103 | $actionConfiguration->getAdminName(), |
||
| 104 | $actionName |
||
| 105 | ), |
||
| 106 | 'icon' => $icon, |
||
| 107 | 'target' => '_self', |
||
| 108 | 'route' => '', |
||
| 109 | 'parameters' => $routeParameters, |
||
| 110 | 'url' => '', |
||
| 111 | 'text' => StringUtils::getTranslationKey( |
||
| 112 | $translationPattern, |
||
| 113 | $actionConfiguration->getAdminName(), |
||
| 114 | $actionName |
||
| 115 | ), |
||
| 116 | 'admin' => $actionConfiguration->getAdminName(), |
||
| 117 | 'action' => $actionName, |
||
| 118 | 'class' => $cssClass, |
||
| 119 | ]) |
||
| 120 | ->setAllowedTypes('route', 'string') |
||
| 121 | ->setAllowedTypes('parameters', 'array') |
||
| 122 | ->setAllowedTypes('url', 'string') |
||
| 123 | ->setAllowedValues('target', [ |
||
| 124 | '_self', |
||
| 125 | '_blank', |
||
| 126 | ]) |
||
| 127 | ->setNormalizer('route', function (Options $options, $value) use ($actionConfiguration) { |
||
| 128 | // route or url should be defined |
||
| 129 | if (!$value && !$options->offsetGet('url') && !$options->offsetGet('admin')) { |
||
| 130 | throw new InvalidOptionsException( |
||
| 131 | 'You must set either an url or a route for the property' |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | if ($options->offsetGet('admin')) { |
||
| 136 | $value = RoutingLoader::generateRouteName( |
||
| 137 | $options->offsetGet('admin'), |
||
| 138 | $options->offsetGet('action'), |
||
| 139 | $actionConfiguration->getAdminConfiguration()->getParameter('routing_name_pattern') |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | |||
| 143 | return $value; |
||
| 144 | }) |
||
| 145 | ->setNormalizer('admin', function (Options $options, $value) { |
||
| 146 | // if a Admin is defined, an Action should be defined too |
||
| 147 | if ($value && !$options->offsetGet('action')) { |
||
| 148 | throw new InvalidOptionsException( |
||
| 149 | 'An Action should be provided if an Admin is provided' |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | |||
| 153 | return $value; |
||
| 154 | }) |
||
| 155 | ->setNormalizer('parameters', function (Options $options, $values) { |
||
| 156 | $cleanedValues = []; |
||
| 157 | |||
| 158 | foreach ($values as $name => $method) { |
||
| 159 | if (null === $method) { |
||
| 160 | $method = $name; |
||
| 161 | } |
||
| 162 | $cleanedValues[$name] = $method; |
||
| 163 | } |
||
| 164 | |||
| 165 | return $cleanedValues; |
||
| 166 | }) |
||
| 167 | ; |
||
| 168 | |||
| 169 | return $resolver->resolve($actionLinkConfiguration); |
||
| 170 | } |
||
| 172 |