| Conditions | 12 |
| Paths | 1 |
| Total Lines | 93 |
| Code Lines | 48 |
| Lines | 30 |
| Ratio | 32.26 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 17 | public function configureOptions(OptionsResolver $resolver) |
||
| 18 | { |
||
| 19 | // user can defined an admin name |
||
| 20 | $resolver |
||
| 21 | ->setDefault('admin', null) |
||
| 22 | ->setNormalizer('admin', function(Options $options, $adminName) { |
||
| 23 | |||
| 24 | // user has to defined either an admin name and an action name, or a route name with optional |
||
| 25 | // parameters, or an url |
||
| 26 | if ($adminName === null |
||
| 27 | && $options->offsetGet('route') === null |
||
| 28 | && $options->offsetGet('url') === null |
||
| 29 | ) { |
||
| 30 | |||
| 31 | throw new InvalidOptionsException( |
||
| 32 | 'You should either defined an admin name, or route name or an uri' |
||
| 33 | ); |
||
| 34 | } |
||
| 35 | |||
| 36 | return $adminName; |
||
| 37 | }); |
||
| 38 | |||
| 39 | // if an admin name is set, an action name can provided. This action will be the menu link |
||
| 40 | $resolver |
||
| 41 | ->setDefault('action', null) |
||
| 42 | ->setNormalizer('action', function(Options $options, $action) { |
||
| 43 | |||
| 44 | // if an action name is provided, an admin name should be defined too |
||
| 45 | if ($action !== null && $options->offsetGet('admin') === null) { |
||
| 46 | throw new InvalidOptionsException( |
||
| 47 | 'You should provide an admin name for this action '.$action |
||
| 48 | ); |
||
| 49 | } |
||
| 50 | |||
| 51 | // default to list action |
||
| 52 | if ($options->offsetGet('admin') !== null && $action === null) { |
||
| 53 | $action = 'list'; |
||
| 54 | } |
||
| 55 | |||
| 56 | return $action; |
||
| 57 | }); |
||
| 58 | |||
| 59 | // a route can also be provided |
||
| 60 | $resolver |
||
| 61 | ->setDefault('route', null) |
||
| 62 | ->setDefault('url', null) |
||
| 63 | ->setDefault('parameters', []) |
||
| 64 | ->setAllowedTypes('parameters', 'array'); |
||
| 65 | |||
| 66 | // menu item displayed text |
||
| 67 | $resolver |
||
| 68 | ->setDefault('text', ''); |
||
| 69 | |||
| 70 | // menu item html attributes |
||
| 71 | $resolver |
||
| 72 | ->setDefault('attr', []) |
||
| 73 | View Code Duplication | ->setNormalizer('attr', function(Options $options, $attr) { |
|
| 74 | |||
| 75 | if (!is_array($attr)) { |
||
| 76 | $attr = []; |
||
| 77 | } |
||
| 78 | |||
| 79 | if (empty($attr['id'])) { |
||
| 80 | $attr['id'] = uniqid('admin-menu-'); |
||
| 81 | } |
||
| 82 | |||
| 83 | return $attr; |
||
| 84 | }); |
||
| 85 | |||
| 86 | // menu sub item |
||
| 87 | $resolver |
||
| 88 | ->setDefault('items', []) |
||
| 89 | View Code Duplication | ->setNormalizer('items', function(Options $options, $items) { |
|
| 90 | |||
| 91 | if (!is_array($items)) { |
||
| 92 | $items = []; |
||
| 93 | } |
||
| 94 | $resolver = new OptionsResolver(); |
||
| 95 | $resolvedItems = []; |
||
| 96 | |||
| 97 | foreach ($items as $name => $item) { |
||
| 98 | $itemConfiguration = new MenuItemConfiguration(); |
||
| 99 | $itemConfiguration->configureOptions($resolver); |
||
| 100 | $itemConfiguration->setParameters($resolver->resolve($item)); |
||
| 101 | |||
| 102 | $resolvedItems[$name] = $itemConfiguration; |
||
| 103 | } |
||
| 104 | |||
| 105 | return $resolvedItems; |
||
| 106 | }); |
||
| 107 | |||
| 108 | $resolver->setDefault('icon', null); |
||
| 109 | } |
||
| 110 | } |
||
| 111 |