Conditions | 23 |
Paths | 1921 |
Total Lines | 113 |
Code Lines | 40 |
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 namespace Anomaly\Streams\Platform\Ui\ControlPanel\Component\Button; |
||
20 | public function normalize(ControlPanelBuilder $builder) |
||
21 | { |
||
22 | $buttons = $builder->getButtons(); |
||
23 | |||
24 | foreach ($buttons as $key => &$button) { |
||
25 | |||
26 | /* |
||
27 | * If the button is a string but the key |
||
28 | * is numeric then use the button as the |
||
29 | * button type. |
||
30 | */ |
||
31 | if (is_numeric($key) && is_string($button)) { |
||
32 | $button = [ |
||
33 | 'button' => $button, |
||
34 | ]; |
||
35 | } |
||
36 | |||
37 | /* |
||
38 | * If the button AND key are strings then |
||
39 | * use the key as the button and the |
||
40 | * button as the text parameters. |
||
41 | */ |
||
42 | if (!is_numeric($key) && is_string($button)) { |
||
43 | $button = [ |
||
44 | 'text' => $button, |
||
45 | 'button' => $key, |
||
46 | ]; |
||
47 | } |
||
48 | |||
49 | /* |
||
50 | * If the key is not numeric and the button |
||
51 | * is an array without the button key then |
||
52 | * use the key as the button's type. |
||
53 | */ |
||
54 | if (!is_numeric($key) && is_array($button) && !isset($button['button'])) { |
||
55 | $button['button'] = $key; |
||
56 | } |
||
57 | |||
58 | /* |
||
59 | * Make sure some default parameters exist. |
||
60 | */ |
||
61 | $button['attributes'] = array_get($button, 'attributes', []); |
||
62 | |||
63 | /* |
||
64 | * Move the HREF if any to the attributes. |
||
65 | */ |
||
66 | if (isset($button['href'])) { |
||
67 | array_set($button['attributes'], 'href', array_pull($button, 'href')); |
||
68 | } |
||
69 | |||
70 | /* |
||
71 | * Move the target if any to the attributes. |
||
72 | */ |
||
73 | if (isset($button['target'])) { |
||
74 | array_set($button['attributes'], 'target', array_pull($button, 'target')); |
||
75 | } |
||
76 | |||
77 | /* |
||
78 | * Move all data-* keys |
||
79 | * to attributes. |
||
80 | */ |
||
81 | foreach ($button as $attribute => $value) { |
||
82 | if (str_is('data-*', $attribute)) { |
||
83 | array_set($button, 'attributes.' . $attribute, array_pull($button, $attribute)); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | /* |
||
88 | * Make sure the HREF is absolute. |
||
89 | */ |
||
90 | if ( |
||
91 | isset($button['attributes']['href']) && |
||
92 | is_string($button['attributes']['href']) && |
||
93 | !starts_with($button['attributes']['href'], 'http') |
||
94 | ) { |
||
95 | $button['attributes']['href'] = url($button['attributes']['href']); |
||
96 | } |
||
97 | |||
98 | /* |
||
99 | * If we have a dropdown then |
||
100 | * process those real quick. |
||
101 | */ |
||
102 | if (isset($button['dropdown'])) { |
||
103 | foreach ($button['dropdown'] as $index => &$dropdown) { |
||
104 | if (is_string($dropdown)) { |
||
105 | $dropdown = [ |
||
106 | 'text' => $index, |
||
107 | 'href' => $dropdown, |
||
108 | ]; |
||
109 | } |
||
110 | |||
111 | // Make sure we have attributes. |
||
112 | $dropdown['attributes'] = array_get($dropdown, 'attributes', []); |
||
113 | |||
114 | // Move the HREF if any to the attributes. |
||
115 | if (isset($dropdown['href'])) { |
||
116 | array_set($dropdown['attributes'], 'href', array_pull($dropdown, 'href')); |
||
117 | } |
||
118 | |||
119 | // Make sure the HREF is absolute. |
||
120 | if ( |
||
121 | isset($dropdown['attributes']['href']) && |
||
122 | is_string($dropdown['attributes']['href']) && |
||
123 | !starts_with($dropdown['attributes']['href'], 'http') |
||
124 | ) { |
||
125 | $dropdown['attributes']['href'] = url($dropdown['attributes']['href']); |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | $builder->setButtons($buttons); |
||
132 | } |
||
133 | } |
||
134 |