Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 13 | class WidgetMapManagerTest extends \PHPUnit_Framework_TestCase |
||
| 14 | { |
||
| 15 | private $prophet; |
||
|
|
|||
| 16 | |||
| 17 | public function testMove() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param integer[] $order |
||
| 51 | * @param Page $view |
||
| 52 | * @param WidgetMapManager $manager |
||
| 53 | * @param WidgetMapBuilder $builder |
||
| 54 | */ |
||
| 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 | |||
| 134 | /** |
||
| 135 | * @param integer $id |
||
| 136 | * @param null|WidgetMap $parent |
||
| 137 | */ |
||
| 138 | View Code Duplication | protected function newWidgetMap($id, $parent, $position, View $view, Widget $widget) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param integer $id |
||
| 156 | */ |
||
| 157 | protected function newWidget($id) |
||
| 164 | |||
| 165 | protected function setup() |
||
| 169 | |||
| 170 | protected function tearDown() |
||
| 174 | } |
||
| 175 |