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 PartialFieldSubmissionTest extends SapphireTest |
||
13 | { |
||
14 | /** |
||
15 | * @var bool |
||
16 | */ |
||
17 | protected $usesDatabase = true; |
||
18 | |||
19 | /** |
||
20 | * @var PartialFieldSubmission |
||
21 | */ |
||
22 | protected $field; |
||
23 | |||
24 | public function testCanView() |
||
25 | { |
||
26 | $this->assertTrue($this->field->canView()); |
||
27 | |||
28 | $member = DefaultAdminService::singleton()->findOrCreateAdmin('[email protected]'); |
||
29 | $this->logInAs($member); |
||
30 | |||
31 | $this->assertTrue($this->field->canView($member)); |
||
32 | } |
||
33 | |||
34 | View Code Duplication | public function testCanCreate() |
|
44 | |||
45 | View Code Duplication | public function testCanEdit() |
|
55 | |||
56 | View Code Duplication | public function testCanDelete() |
|
66 | |||
67 | protected function setUp() |
||
83 | |||
84 | protected function tearDown() |
||
88 | } |
||
89 |
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.