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 AttachmentsOptionsTest extends TestCase |
||
13 | { |
||
14 | /** |
||
15 | * @var AttachmentsOptions |
||
16 | */ |
||
17 | private $attachmentsOptions; |
||
18 | |||
19 | public function setUp() |
||
20 | { |
||
21 | $this->attachmentsOptions = new AttachmentsOptions(); |
||
22 | } |
||
23 | |||
24 | public function testDefaultAttachmentsOptionsValues() |
||
25 | { |
||
26 | $this->assertTrue(is_array($this->attachmentsOptions->getFiles())); |
||
27 | $this->assertCount(0, $this->attachmentsOptions->getFiles()); |
||
28 | $this->assertTrue(is_array($this->attachmentsOptions->getDir())); |
||
29 | $this->assertArrayHasKey('recursive', $this->attachmentsOptions->getDir()); |
||
30 | $this->assertArrayHasKey('iterate', $this->attachmentsOptions->getDir()); |
||
31 | $this->assertArrayHasKey('path', $this->attachmentsOptions->getDir()); |
||
32 | |||
33 | $dir = $this->attachmentsOptions->getDir(); |
||
34 | $this->assertEquals(AttachmentsOptions::DEFAULT_PATH, $dir['path']); |
||
35 | $this->assertEquals(AttachmentsOptions::DEFAULT_ITERATE, $dir['iterate']); |
||
36 | $this->assertEquals(AttachmentsOptions::DEFAULT_RECURSIVE, $dir['recursive']); |
||
37 | } |
||
38 | |||
39 | View Code Duplication | public function testDireHasDefaultKeys() |
|
|
|||
40 | { |
||
41 | $this->attachmentsOptions->setDir(['recursive' => true]); |
||
42 | $this->assertArrayHasKey('recursive', $this->attachmentsOptions->getDir()); |
||
43 | $this->assertArrayHasKey('iterate', $this->attachmentsOptions->getDir()); |
||
44 | $this->assertArrayHasKey('path', $this->attachmentsOptions->getDir()); |
||
45 | $dir = $this->attachmentsOptions->getDir(); |
||
46 | $this->assertEquals(AttachmentsOptions::DEFAULT_PATH, $dir['path']); |
||
47 | $this->assertEquals(AttachmentsOptions::DEFAULT_ITERATE, $dir['iterate']); |
||
48 | $this->assertEquals(true, $dir['recursive']); |
||
49 | } |
||
50 | |||
51 | public function testTotalFiles() |
||
67 | |||
68 | View Code Duplication | public function testDirNormalization() |
|
69 | { |
||
70 | $this->attachmentsOptions->setDir([]); |
||
80 | } |
||
81 |
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.