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 | View Code Duplication | class AccordionPanelTest extends SapphireTest |
|
|
|||
4 | { |
||
5 | /** |
||
6 | * @var string |
||
7 | */ |
||
8 | protected static $fixture_file = 'dynamic-blocks/tests/Fixtures.yml'; |
||
9 | |||
10 | /** |
||
11 | * |
||
12 | */ |
||
13 | public function testGetCMSFields() |
||
14 | { |
||
15 | $object = $this->objFromFixture('AccordionPanel', 'one'); |
||
16 | $fields = $object->getCMSFields(); |
||
17 | $this->assertInstanceOf('FieldList', $fields); |
||
18 | $this->assertNull($fields->dataFieldByName('SortOrder')); |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * |
||
23 | */ |
||
24 | public function testValidateTitle() |
||
25 | { |
||
26 | $object = $this->objFromFixture('AccordionPanel', 'one'); |
||
27 | $object->Title = ''; |
||
28 | $this->setExpectedException('ValidationException'); |
||
29 | $object->write(); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * |
||
34 | */ |
||
35 | public function testCanView() |
||
43 | |||
44 | /** |
||
45 | * |
||
46 | */ |
||
47 | public function testCanEdit() |
||
55 | |||
56 | /** |
||
57 | * |
||
58 | */ |
||
59 | public function testCanDelete() |
||
67 | |||
68 | /** |
||
69 | * |
||
70 | */ |
||
71 | public function testCanCreate() |
||
79 | } |
||
80 |
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.