| Conditions | 12 |
| Paths | 3 |
| Total Lines | 78 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 1 | 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 |
||
| 55 | protected function moveWidgetMap($builtWidgetMap, $order, $view, $manager, $builder) |
||
| 56 | { |
||
| 57 | $sortedWidget = [ |
||
| 58 | 'parentWidgetMap' => null, |
||
| 59 | 'position' => null, |
||
| 60 | 'slot' => 'content', |
||
| 61 | 'widgetMap' => null, |
||
| 62 | ]; |
||
| 63 | |||
| 64 | for ($i = 1; $i <= 1000; $i++) { |
||
| 65 | $buildSortedWidget = function ($builtWidgetMap) use (&$order, &$buildSortedWidget, $view) { |
||
| 66 | |||
| 67 | $sortedWidget['widgetMap'] = $builtWidgetMap['content'][array_rand($builtWidgetMap['content'])]; |
||
| 68 | $availablePositions = []; |
||
| 69 | $positions = [WidgetMap::POSITION_AFTER, WidgetMap::POSITION_BEFORE]; |
||
| 70 | $shuffled = $builtWidgetMap['content']; |
||
| 71 | shuffle($shuffled); |
||
| 72 | foreach ($shuffled as $widgetMap) { |
||
| 73 | if ($widgetMap->getId() !== $sortedWidget['widgetMap']->getId()) { |
||
| 74 | foreach ($positions as $position) { |
||
| 75 | if (!$widgetMap->hasChild($position, $view)) { |
||
| 76 | $availablePositions[] = [ |
||
| 77 | 'parentWidgetMap' => $widgetMap, |
||
| 78 | 'position' => $position, |
||
| 79 | ]; |
||
| 80 | if (array_rand([0, 1]) === 0) { |
||
| 81 | break; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | $randomPosition = $availablePositions[array_rand($availablePositions)]; |
||
| 89 | $offset = array_search( |
||
| 90 | $randomPosition['parentWidgetMap']->getWidget()->getId(), |
||
| 91 | $order |
||
| 92 | ) + ($randomPosition['position'] == WidgetMap::POSITION_AFTER ? 1 : 0); |
||
| 93 | if (!empty($order[$offset]) && $order[$offset] == $sortedWidget['widgetMap']->getId()) { |
||
| 94 | return $buildSortedWidget($builtWidgetMap); |
||
| 95 | } |
||
| 96 | |||
| 97 | $sortedWidget = array_merge($sortedWidget, $randomPosition); |
||
| 98 | |||
| 99 | $order[array_search($sortedWidget['widgetMap']->getWidget()->getId(), $order)] = null; |
||
| 100 | $offset = array_search( |
||
| 101 | $sortedWidget['parentWidgetMap']->getWidget()->getId(), |
||
| 102 | $order |
||
| 103 | ) + ($sortedWidget['position'] == WidgetMap::POSITION_AFTER ? 1 : 0); |
||
| 104 | array_splice($order, $offset, 0, $sortedWidget['widgetMap']->getWidget()->getId()); |
||
| 105 | |||
| 106 | unset($order[array_search(null, $order)]); |
||
| 107 | |||
| 108 | $order = array_values($order); |
||
| 109 | $sortedWidget['widgetMap'] = $sortedWidget['widgetMap']->getId(); |
||
| 110 | $sortedWidget['parentWidgetMap'] = $sortedWidget['parentWidgetMap']->getId(); |
||
| 111 | |||
| 112 | return $sortedWidget; |
||
| 113 | |||
| 114 | }; |
||
| 115 | |||
| 116 | $sortedWidget = array_merge($sortedWidget, $buildSortedWidget($builtWidgetMap)); |
||
| 117 | |||
| 118 | $manager->move($view, $sortedWidget); |
||
| 119 | $newBuiltWidgetMap = $builder->build($view); |
||
| 120 | |||
| 121 | $newOrder = []; |
||
| 122 | foreach ($newBuiltWidgetMap['content'] as $newWidgetMap) { |
||
| 123 | $newOrder[] = $newWidgetMap->getWidget()->getId(); |
||
| 124 | } |
||
| 125 | |||
| 126 | $this->assertEquals($order, $newOrder, |
||
| 127 | sprintf("move widget %s %s widget %s didn't worked at iteration %s", |
||
| 128 | $sortedWidget['widgetMap'], $sortedWidget['position'], $sortedWidget['parentWidgetMap'], $i)); |
||
| 129 | |||
| 130 | $builtWidgetMap = $newBuiltWidgetMap; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 175 |