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 |
||
3 | namespace WebOfTalent\GridRows\Tests; |
||
4 | |||
5 | use PageController; |
||
6 | use SilverStripe\Dev\FunctionalTest; |
||
7 | use SilverStripe\ORM\DataObject; |
||
8 | use SilverStripe\Dev\TestOnly; |
||
9 | |||
10 | class GridRowsExtensionTest extends FunctionalTest |
||
11 | { |
||
12 | protected static $fixture_file = 'GridRowsExtensionTest.yml'; |
||
13 | |||
14 | protected $extraDataObjects = array( |
||
15 | 'GridRowItemTO' |
||
16 | ); |
||
17 | |||
18 | public function setUp() |
||
26 | |||
27 | |||
28 | public function testSplitDataListMethodDoesNotExist() |
||
47 | |||
48 | View Code Duplication | public function testSplitDataListFromModelIntoGridRows() |
|
63 | |||
64 | View Code Duplication | public function testSplitDataListFromControllerIntoGridRows() |
|
78 | |||
79 | /* |
||
80 | Check multiple number of columns with amounts from 1 to just over the |
||
81 | total number of grid items, namely 12 |
||
82 | */ |
||
83 | public function testSplitClassNameDataListIntoGridRows() |
||
102 | |||
103 | |||
104 | private function checkGrid($grid, $maxWidth, $amount) |
||
105 | { |
||
106 | $items = 0; |
||
107 | $rows = 0; |
||
108 | $widths = array(); |
||
109 | foreach ($grid->getIterator() as $row) { |
||
110 | $rows++; |
||
111 | $width = 0; |
||
112 | foreach ($row->Columns->getIterator() as $column) { |
||
113 | $items++; |
||
114 | $width++; |
||
115 | } |
||
116 | array_push($widths, $width); |
||
117 | } |
||
118 | |||
119 | // last value will be <= max width |
||
120 | $lastVal = array_pop($widths); |
||
121 | $this->assertLessThanOrEqual($maxWidth, $lastVal); |
||
122 | |||
123 | // All but the last row should equal the expected width, $maxWidth |
||
155 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.