| Conditions | 15 |
| Paths | 1 |
| Total Lines | 91 |
| 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 |
||
| 41 | public function configureOptions(OptionsResolver $resolver) |
||
| 42 | { |
||
| 43 | // user can defined an admin name |
||
| 44 | $resolver |
||
| 45 | ->setDefault('admin', null) |
||
| 46 | ->setDefault('action', null) |
||
| 47 | ->setDefault('route', null) |
||
| 48 | ->setDefault('url', null) |
||
| 49 | ->setDefault('parameters', []) |
||
| 50 | ->setDefault('text', '') |
||
| 51 | ->setDefault('attr', []) |
||
| 52 | ->setDefault('items', []) |
||
| 53 | ->setDefault('icon', null) |
||
| 54 | ->setDefault('link_css_class', 'nav-link') |
||
| 55 | ->setNormalizer('admin', function(Options $options, $adminName) { |
||
| 56 | |||
| 57 | // user has to defined either an admin name and an action name, or a route name with optional |
||
| 58 | // parameters, or an url |
||
| 59 | if ($adminName === null |
||
| 60 | && $options->offsetGet('route') === null |
||
| 61 | && $options->offsetGet('url') === null |
||
| 62 | ) { |
||
| 63 | |||
| 64 | throw new InvalidOptionsException( |
||
| 65 | 'You should either defined an admin name, or route name or an uri' |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | |||
| 69 | return $adminName; |
||
| 70 | }) |
||
| 71 | // if an admin name is set, an action name can provided. This action will be the menu link |
||
| 72 | ->setNormalizer('action', function(Options $options, $action) { |
||
| 73 | |||
| 74 | // if an action name is provided, an admin name should be defined too |
||
| 75 | if ($action !== null && $options->offsetGet('admin') === null) { |
||
| 76 | throw new InvalidOptionsException( |
||
| 77 | 'You should provide an admin name for this action '.$action |
||
| 78 | ); |
||
| 79 | } |
||
| 80 | |||
| 81 | // default to list action |
||
| 82 | if ($options->offsetGet('admin') !== null && $action === null) { |
||
| 83 | $action = 'list'; |
||
| 84 | } |
||
| 85 | |||
| 86 | return $action; |
||
| 87 | }) |
||
| 88 | ->setAllowedTypes('parameters', 'array') |
||
| 89 | ->setNormalizer('attr', function(Options $options, $attr) { |
||
| 90 | |||
| 91 | if (!is_array($attr)) { |
||
| 92 | $attr = []; |
||
| 93 | } |
||
| 94 | |||
| 95 | if (empty($attr['id'])) { |
||
| 96 | $attr['id'] = uniqid('admin-menu-'); |
||
| 97 | } |
||
| 98 | |||
| 99 | if ('horizontal' === $this->position && !key_exists('class', $attr)) { |
||
| 100 | $attr['class'] = 'nav-item'; |
||
| 101 | } |
||
| 102 | |||
| 103 | return $attr; |
||
| 104 | }) |
||
| 105 | ->setNormalizer('items', function(Options $options, $items) { |
||
| 106 | if (!is_array($items)) { |
||
| 107 | $items = []; |
||
| 108 | } |
||
| 109 | $resolver = new OptionsResolver(); |
||
| 110 | $resolvedItems = []; |
||
| 111 | |||
| 112 | foreach ($items as $name => $item) { |
||
| 113 | $itemConfiguration = new MenuItemConfiguration($name, $this->position); |
||
| 114 | $itemConfiguration->configureOptions($resolver); |
||
| 115 | $itemConfiguration->setParameters($resolver->resolve($item)); |
||
| 116 | |||
| 117 | $resolvedItems[$name] = $itemConfiguration; |
||
| 118 | } |
||
| 119 | |||
| 120 | return $resolvedItems; |
||
| 121 | }) |
||
| 122 | ->setNormalizer('text', function (Options $options, $text) { |
||
| 123 | if (!$text) { |
||
| 124 | // TODO use translation pattern key instead |
||
| 125 | $text = ucfirst($this->name); |
||
| 126 | } |
||
| 127 | |||
| 128 | return $text; |
||
| 129 | }) |
||
| 130 | ; |
||
| 131 | } |
||
| 132 | } |
||
| 133 |