Conditions | 14 |
Paths | 37 |
Total Lines | 85 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Tests | 27 |
CRAP Score | 33.0778 |
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 |
||
171 | 8 | public function move($column, $rank) { |
|
172 | $options = [ |
||
173 | 8 | 'type' => 'object', |
|
174 | 8 | 'subtype' => 'widget', |
|
175 | 8 | 'container_guid' => $this->container_guid, |
|
176 | 'limit' => false, |
||
177 | 'private_setting_name_value_pairs' => [ |
||
178 | 8 | ['name' => 'context', 'value' => $this->getContext()], |
|
179 | 8 | ['name' => 'column', 'value' => $column] |
|
180 | ] |
||
181 | ]; |
||
182 | 8 | $widgets = elgg_get_entities($options); |
|
183 | 8 | if (!$widgets) { |
|
184 | 8 | $this->column = (int) $column; |
|
1 ignored issue
–
show
|
|||
185 | 8 | $this->order = 0; |
|
1 ignored issue
–
show
|
|||
186 | 8 | return; |
|
187 | } |
||
188 | |||
189 | 4 | usort($widgets, function($a, $b) {return (int) $a->order > (int) $b->order; |
|
190 | |||
191 | 4 | }); |
|
192 | |||
193 | // remove widgets from inactive plugins |
||
194 | 4 | $widget_types = elgg_get_widget_types([ |
|
195 | 4 | 'context' => $this->context, |
|
196 | 4 | 'container' => $this->getContainerEntity(), |
|
197 | ]); |
||
198 | 4 | $inactive_widgets = []; |
|
199 | 4 | foreach ($widgets as $index => $widget) { |
|
200 | 4 | if (!array_key_exists($widget->handler, $widget_types)) { |
|
201 | $inactive_widgets[] = $widget; |
||
202 | 4 | unset($widgets[$index]); |
|
203 | } |
||
204 | } |
||
205 | |||
206 | 4 | $bottom_rank = count($widgets); |
|
207 | 4 | if ($column == $this->column) { |
|
208 | $bottom_rank--; |
||
209 | } |
||
210 | |||
211 | 4 | if ($rank == 0) { |
|
212 | // top of the column |
||
213 | $this->order = reset($widgets)->order - 10; |
||
214 | 4 | } elseif ($rank == $bottom_rank) { |
|
215 | // bottom of the column of active widgets |
||
216 | 4 | $this->order = end($widgets)->order + 10; |
|
217 | } else { |
||
218 | // reorder widgets |
||
219 | |||
220 | // remove the widget that's being moved from the array |
||
221 | foreach ($widgets as $index => $widget) { |
||
222 | if ($widget->guid == $this->guid) { |
||
223 | unset($widgets[$index]); |
||
224 | } |
||
225 | } |
||
226 | |||
227 | // split the array in two and recombine with the moved widget in middle |
||
228 | $before = array_slice($widgets, 0, $rank); |
||
229 | array_push($before, $this); |
||
230 | $after = array_slice($widgets, $rank); |
||
231 | $widgets = array_merge($before, $after); |
||
232 | ksort($widgets); |
||
233 | $order = 0; |
||
234 | foreach ($widgets as $widget) { |
||
235 | $widget->order = $order; |
||
236 | $order += 10; |
||
237 | } |
||
238 | } |
||
239 | |||
240 | // put inactive widgets at the bottom |
||
241 | 4 | if ($inactive_widgets) { |
|
242 | $bottom = 0; |
||
243 | foreach ($widgets as $widget) { |
||
244 | if ($widget->order > $bottom) { |
||
245 | $bottom = $widget->order; |
||
246 | } |
||
247 | } |
||
248 | $bottom += 10; |
||
249 | foreach ($inactive_widgets as $widget) { |
||
250 | $widget->order = $bottom; |
||
251 | $bottom += 10; |
||
252 | } |
||
253 | } |
||
254 | |||
255 | 4 | $this->column = $column; |
|
256 | 4 | } |
|
303 |