Conditions | 20 |
Paths | 1 |
Total Lines | 107 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
39 | public function configureOptions(OptionsResolver $resolver) |
||
40 | { |
||
41 | // user can defined an admin name |
||
42 | $resolver |
||
43 | ->setDefault('admin', null) |
||
44 | ->setDefault('action', null) |
||
45 | ->setDefault('route', null) |
||
46 | ->setDefault('url', null) |
||
47 | ->setDefault('parameters', []) |
||
48 | ->setDefault('text', '') |
||
49 | ->setDefault('attr', []) |
||
50 | ->setDefault('link_attr', [ |
||
51 | 'class' => 'nav-link', |
||
52 | ]) |
||
53 | ->setDefault('items', []) |
||
54 | ->setDefault('icon', null) |
||
55 | ->setDefault('link_css_class', 'nav-link') |
||
56 | ->setNormalizer('admin', function (Options $options, $adminName) { |
||
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 (null === $adminName |
||
60 | && null === $options->offsetGet('route') |
||
61 | && null === $options->offsetGet('url') |
||
62 | ) { |
||
63 | throw new InvalidOptionsException('You should either defined an admin name, or route name or an uri'); |
||
64 | } |
||
65 | |||
66 | return $adminName; |
||
67 | }) |
||
68 | // if an admin name is set, an action name can provided. This action will be the menu link |
||
69 | ->setNormalizer('action', function (Options $options, $action) { |
||
70 | // if an action name is provided, an admin name should be defined too |
||
71 | if (null !== $action && null === $options->offsetGet('admin')) { |
||
72 | throw new InvalidOptionsException('You should provide an admin name for this action '.$action); |
||
73 | } |
||
74 | |||
75 | // default to list action |
||
76 | if (null !== $options->offsetGet('admin') && null === $action) { |
||
77 | $action = 'list'; |
||
78 | } |
||
79 | |||
80 | return $action; |
||
81 | }) |
||
82 | ->setAllowedTypes('parameters', 'array') |
||
83 | ->setNormalizer('attr', function (Options $options, $attr) { |
||
84 | if (!is_array($attr)) { |
||
85 | $attr = []; |
||
86 | } |
||
87 | |||
88 | if (empty($attr['id'])) { |
||
89 | $attr['id'] = uniqid('admin-menu-'); |
||
90 | } |
||
91 | |||
92 | if ('horizontal' === $this->position && !key_exists('class', $attr)) { |
||
93 | $attr['class'] = 'nav-item'; |
||
94 | } |
||
95 | |||
96 | return $attr; |
||
97 | }) |
||
98 | ->setNormalizer('link_attr', function (Options $options, $value) { |
||
99 | if (!is_array($value)) { |
||
100 | $value = []; |
||
101 | } |
||
102 | |||
103 | if (!key_exists('class', $value)) { |
||
104 | $value['class'] = 'nav-link'; |
||
105 | } |
||
106 | |||
107 | return $value; |
||
108 | }) |
||
109 | ->setNormalizer('items', function (Options $options, $items) { |
||
110 | if (!is_array($items)) { |
||
111 | $items = []; |
||
112 | } |
||
113 | $resolver = new OptionsResolver(); |
||
114 | $resolvedItems = []; |
||
115 | |||
116 | foreach ($items as $name => $item) { |
||
117 | $itemConfiguration = new MenuItemConfiguration($name, $this->position); |
||
118 | $itemConfiguration->configureOptions($resolver); |
||
119 | $itemConfiguration->setParameters($resolver->resolve($item)); |
||
120 | |||
121 | $resolvedItems[$name] = $itemConfiguration; |
||
122 | } |
||
123 | |||
124 | return $resolvedItems; |
||
125 | }) |
||
126 | ->setNormalizer('text', function (Options $options, $text) { |
||
127 | if ($text) { |
||
128 | return $text; |
||
129 | } |
||
130 | |||
131 | if ($options->offsetGet('admin')) { |
||
132 | $text = ucfirst($options->offsetGet('admin')); |
||
133 | |||
134 | if (StringUtils::endsWith($text, 'y')) { |
||
135 | $text = substr($text, 0, strlen($text) - 1).'ie'; |
||
136 | } |
||
137 | |||
138 | if (!StringUtils::endsWith($text, 's')) { |
||
139 | $text .= 's'; |
||
140 | } |
||
141 | |||
142 | return $text; |
||
143 | } |
||
144 | |||
145 | return ucfirst($this->name); |
||
146 | }) |
||
166 |