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