Conditions | 9 |
Paths | 1 |
Total Lines | 73 |
Code Lines | 33 |
Lines | 12 |
Ratio | 16.44 % |
Tests | 0 |
CRAP Score | 90 |
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 | |||
29 | throw new InvalidOptionsException( |
||
30 | 'You should either defined an admin name, or route name or an url' |
||
31 | ); |
||
32 | } |
||
33 | |||
34 | return $adminName; |
||
35 | }) |
||
36 | ; |
||
37 | |||
38 | // if an admin name is set, an action name can provided. This action will be the menu link |
||
39 | $resolver |
||
40 | ->setDefault('action', null) |
||
41 | ->setNormalizer('action', function(Options $options, $action) { |
||
42 | |||
43 | // if an action name is provided, an admin name should be defined too |
||
44 | if ($action !== null && $options->offsetGet('admin') === null) { |
||
45 | throw new InvalidOptionsException( |
||
46 | 'You should provide an admin name for this action ' . $action |
||
47 | ); |
||
48 | } |
||
49 | |||
50 | // default to list action |
||
51 | if ($options->offsetGet('admin') !== null && $action === null) { |
||
52 | $action = 'list'; |
||
53 | } |
||
54 | |||
55 | return $action; |
||
56 | }) |
||
57 | ; |
||
58 | |||
59 | // a route can also be provided |
||
60 | $resolver |
||
61 | ->setDefault('route', null) |
||
62 | ->setDefault('parameters', []) |
||
63 | ->setAllowedTypes('parameters', 'array') |
||
64 | ; |
||
65 | |||
66 | // menu item displayed text |
||
67 | $resolver |
||
68 | ->setDefault('text', '') |
||
69 | ; |
||
70 | |||
71 | // menu item html attributes |
||
72 | $resolver |
||
73 | ->setDefault('attr', []) |
||
74 | View Code Duplication | ->setNormalizer('attr', function(Options $options, $attr) { |
|
1 ignored issue
–
show
|
|||
75 | |||
76 | if (!is_array($attr)) { |
||
77 | $attr = []; |
||
78 | } |
||
79 | |||
80 | if (empty($attr['id'])) { |
||
81 | $attr['id'] = uniqid('admin-menu-'); |
||
82 | } |
||
83 | |||
84 | return $attr; |
||
85 | }) |
||
86 | ; |
||
87 | |||
88 | $resolver->setDefault('icon', null); |
||
89 | } |
||
90 | } |
||
91 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.