Conditions | 20 |
Paths | 772 |
Total Lines | 95 |
Code Lines | 33 |
Lines | 7 |
Ratio | 7.37 % |
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 namespace Anomaly\Streams\Platform\Ui\ControlPanel\Component\Section; |
||
38 | public function normalize(ControlPanelBuilder $builder) |
||
39 | { |
||
40 | $sections = $builder->getSections(); |
||
41 | |||
42 | /* |
||
43 | * Move child sections into main array. |
||
44 | */ |
||
45 | foreach ($sections as $slug => &$section) { |
||
46 | if (isset($section['sections'])) { |
||
47 | foreach ($section['sections'] as $key => &$child) { |
||
48 | $child['parent'] = array_get($section, 'slug', $slug); |
||
49 | $child['slug'] = array_get($child, 'slug', $key); |
||
50 | |||
51 | $sections[$key] = $child; |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /* |
||
57 | * Loop over each section and make sense of the input |
||
58 | * provided for the given module. |
||
59 | */ |
||
60 | foreach ($sections as $slug => &$section) { |
||
61 | |||
62 | /* |
||
63 | * If the slug is not valid and the section |
||
64 | * is a string then use the section as the slug. |
||
65 | */ |
||
66 | if (is_numeric($slug) && is_string($section)) { |
||
67 | $section = [ |
||
68 | 'slug' => $section, |
||
69 | ]; |
||
70 | } |
||
71 | |||
72 | /* |
||
73 | * If the slug is a string and the title is not |
||
74 | * set then use the slug as the slug. |
||
75 | */ |
||
76 | if (is_string($slug) && !isset($section['slug'])) { |
||
77 | $section['slug'] = $slug; |
||
78 | } |
||
79 | |||
80 | /* |
||
81 | * Make sure we have attributes. |
||
82 | */ |
||
83 | $section['attributes'] = array_get($section, 'attributes', []); |
||
84 | |||
85 | /* |
||
86 | * Move the HREF into attributes. |
||
87 | */ |
||
88 | if (isset($section['href'])) { |
||
89 | $section['attributes']['href'] = array_pull($section, 'href'); |
||
90 | } |
||
91 | |||
92 | /* |
||
93 | * Move all data-* keys |
||
94 | * to attributes. |
||
95 | */ |
||
96 | foreach ($section as $attribute => $value) { |
||
97 | if (str_is('data-*', $attribute)) { |
||
98 | array_set($section, 'attributes.' . $attribute, array_pull($section, $attribute)); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | /* |
||
103 | * Move the data-href into the permalink. |
||
104 | * |
||
105 | * @deprecated as of v3.2 |
||
106 | */ |
||
107 | if (!isset($section['permalink']) && isset($section['attributes']['data-href'])) { |
||
108 | $section['permalink'] = array_pull($section, 'attributes.data-href'); |
||
109 | } |
||
110 | |||
111 | /* |
||
112 | * Make sure the HREF and permalink are absolute. |
||
113 | */ |
||
114 | if ( |
||
115 | isset($section['attributes']['href']) && |
||
116 | is_string($section['attributes']['href']) && |
||
117 | !starts_with($section['attributes']['href'], 'http') |
||
118 | ) { |
||
119 | $section['attributes']['href'] = url($section['attributes']['href']); |
||
120 | } |
||
121 | |||
122 | View Code Duplication | if ( |
|
|
|||
123 | isset($section['permalink']) && |
||
124 | is_string($section['permalink']) && |
||
125 | !starts_with($section['permalink'], 'http') |
||
126 | ) { |
||
127 | $section['permalink'] = url($section['permalink']); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | $builder->setSections(array_values($sections)); |
||
132 | } |
||
133 | } |
||
134 |
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.