| Conditions | 14 |
| Paths | 16 |
| Total Lines | 100 |
| Code Lines | 68 |
| 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 |
||
| 179 | function getDistrictsDropdown(array $args) |
||
| 180 | { |
||
| 181 | $output = ''; |
||
| 182 | $args = array_merge([ |
||
| 183 | 'id' => '', |
||
| 184 | 'name' => '', |
||
| 185 | 'atts' => [], |
||
| 186 | 'selected' => [], |
||
| 187 | 'districts' => 'all', |
||
| 188 | 'group_by_cities' => true, |
||
| 189 | 'show_placeholder' => false, |
||
| 190 | 'placeholder_value' => '', |
||
| 191 | 'placeholder_text' => '', |
||
| 192 | ], $args); |
||
| 193 | |||
| 194 | $args['selected'] = (array) $args['selected']; |
||
| 195 | |||
| 196 | array_walk($args['selected'], function ($value) { |
||
| 197 | if ($value instanceof District) { |
||
| 198 | return $value->get('id'); |
||
| 199 | } |
||
| 200 | }); |
||
| 201 | |||
| 202 | $args['atts'] = array_merge((array) $args['atts'], [ |
||
| 203 | 'name' => $args['name'], |
||
| 204 | 'id' => $args['id'], |
||
| 205 | ]); |
||
| 206 | |||
| 207 | if (is_string($args['districts']) && 'all' === $args['districts']) { |
||
| 208 | $em = Main::getInstance()->getEntityManager(); |
||
| 209 | $districtRepository = $em->getRepository('Entities:District'); |
||
| 210 | $args['districts'] = (array) $districtRepository->findAll(); |
||
| 211 | } |
||
| 212 | |||
| 213 | $output .= sprintf('<select%s>', toAttributes($args['atts'])); |
||
| 214 | |||
| 215 | if ($args['show_placeholder']) { |
||
| 216 | $output .= sprintf( |
||
| 217 | '<option%s>%s</option>', |
||
| 218 | toAttributes(['value' => $args['placeholder_value']]), |
||
| 219 | escHTML($args['placeholder_text']) |
||
| 220 | ); |
||
| 221 | } |
||
| 222 | |||
| 223 | if (! $args['group_by_cities']) { |
||
| 224 | foreach ($args['districts'] as $district) { |
||
| 225 | $districtID = (int) $district->get('id'); |
||
| 226 | $output .= sprintf( |
||
| 227 | '<option%s>%s</option>', |
||
| 228 | toAttributes([ |
||
| 229 | 'value' => $districtID, |
||
| 230 | 'data-city-id' => $district->get('city')->get('id'), |
||
| 231 | 'selected' => in_array($districtID, $args['selected']), |
||
| 232 | ]), |
||
| 233 | escHTML($district->get('name')) |
||
| 234 | ); |
||
| 235 | } |
||
| 236 | } else { |
||
| 237 | $groups = []; |
||
| 238 | foreach ($args['districts'] as $district) { |
||
| 239 | $city = $district->get('city'); |
||
| 240 | $cityID = (int) $city->get('id'); |
||
| 241 | if (! isset($groups[$cityID])) { |
||
| 242 | $groups[$cityID] = [ |
||
| 243 | 'atts' => [ |
||
| 244 | 'label' => $city->get('name') |
||
| 245 | ], |
||
| 246 | 'options' => [] |
||
| 247 | ]; |
||
| 248 | } |
||
| 249 | $districtID = (int) $district->get('id'); |
||
| 250 | $groups[$cityID]['options'][$districtID] = [ |
||
| 251 | 'atts' => [ |
||
| 252 | 'value' => $districtID, |
||
| 253 | 'data-city-id' => $district->get('city')->get('id'), |
||
| 254 | 'selected' => in_array($districtID, $args['selected']), |
||
| 255 | ], |
||
| 256 | 'text' => $district->get('name'), |
||
| 257 | ]; |
||
| 258 | } |
||
| 259 | if (! empty($groups)) { |
||
| 260 | foreach ($groups as $group) { |
||
| 261 | $output .= sprintf('<optgroup%s>', toAttributes($group['atts'])); |
||
| 262 | if (! empty($group['options']) && is_array($group['options'])) { |
||
| 263 | foreach ($group['options'] as $option) { |
||
| 264 | $output .= sprintf( |
||
| 265 | '<option%s>%s</option>', |
||
| 266 | toAttributes($option['atts']), |
||
| 267 | escHTML($option['text']) |
||
| 268 | ); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | $output .= '</optgroup>'; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | $output .= '</select>'; |
||
| 277 | |||
| 278 | return $output; |
||
| 279 | } |
||
| 425 |