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 |
||
12 | class SlideImageTest extends SapphireTest |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected static $fixture_file = 'flexslider/tests/fixtures.yml'; |
||
18 | |||
19 | /** |
||
20 | * |
||
21 | */ |
||
22 | public function testGetCMSFields() |
||
23 | { |
||
24 | $object = new SlideImage(); |
||
25 | $fieldset = $object->getCMSFields(); |
||
26 | $this->assertInstanceOf(FieldList::class, $fieldset); |
||
27 | $this->assertNotNull($fieldset->dataFieldByName('Name')); |
||
28 | $this->assertNotNull($fieldset->dataFieldByName('Image')); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * |
||
33 | */ |
||
34 | View Code Duplication | public function testValidateName() |
|
41 | |||
42 | /** |
||
43 | * |
||
44 | */ |
||
45 | View Code Duplication | public function testValidateImage() |
|
52 | |||
53 | /** |
||
54 | * |
||
55 | */ |
||
56 | public function testProvidePermissions() |
||
66 | |||
67 | /** |
||
68 | * |
||
69 | */ |
||
70 | View Code Duplication | public function testCanCreate() |
|
82 | |||
83 | /** |
||
84 | * |
||
85 | */ |
||
86 | View Code Duplication | public function testCanEdit() |
|
98 | |||
99 | /** |
||
100 | * |
||
101 | */ |
||
102 | View Code Duplication | public function testCanDelete() |
|
114 | |||
115 | /** |
||
116 | * |
||
117 | */ |
||
118 | View Code Duplication | public function testCanView() |
|
130 | |||
131 | /** |
||
132 | * |
||
133 | */ |
||
134 | public function testImageSizeLimit() |
||
143 | } |
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.