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 |
||
9 | class WorksheetTest extends PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | public function testSetTitle() |
||
19 | |||
20 | View Code Duplication | public function setTitleInvalidProvider() |
|
27 | |||
28 | /** |
||
29 | * @param string $title |
||
30 | * @param string $expectMessage |
||
31 | * @dataProvider setTitleInvalidProvider |
||
32 | */ |
||
33 | View Code Duplication | public function testSetTitleInvalid($title, $expectMessage) |
|
34 | { |
||
35 | // First, test setting title with validation disabled -- should be successful |
||
36 | $worksheet = new Worksheet(); |
||
37 | $worksheet->setTitle($title, true, false); |
||
38 | |||
39 | // Next, test again with validation enabled -- this time we should fail |
||
40 | $worksheet = new Worksheet(); |
||
41 | $this->expectException(\Exception::class); |
||
42 | $this->expectExceptionMessage($expectMessage); |
||
43 | $worksheet->setTitle($title); |
||
44 | } |
||
45 | |||
46 | View Code Duplication | public function testSetTitleDuplicate() |
|
47 | { |
||
48 | // Create a Spreadsheet with three Worksheets (the first is created automatically) |
||
49 | $spreadsheet = new Spreadsheet(); |
||
50 | $spreadsheet->createSheet(); |
||
51 | $spreadsheet->createSheet(); |
||
52 | |||
53 | // Set unique title -- should be unchanged |
||
54 | $sheet = $spreadsheet->getSheet(0); |
||
55 | $sheet->setTitle('Test Title'); |
||
56 | $this->assertSame('Test Title', $sheet->getTitle()); |
||
57 | |||
58 | // Set duplicate title -- should have numeric suffix appended |
||
59 | $sheet = $spreadsheet->getSheet(1); |
||
60 | $sheet->setTitle('Test Title'); |
||
61 | $this->assertSame('Test Title 1', $sheet->getTitle()); |
||
62 | |||
63 | // Set duplicate title with validation disabled -- should be unchanged |
||
64 | $sheet = $spreadsheet->getSheet(2); |
||
65 | $sheet->setTitle('Test Title', true, false); |
||
66 | $this->assertSame('Test Title', $sheet->getTitle()); |
||
67 | } |
||
68 | |||
69 | public function testSetCodeName() |
||
77 | |||
78 | View Code Duplication | public function setCodeNameInvalidProvider() |
|
79 | { |
||
80 | return [ |
||
81 | [str_repeat('a', 32), 'Maximum 31 characters allowed in sheet code name.'], |
||
82 | ['invalid*code*name', 'Invalid character found in sheet code name'], |
||
83 | ]; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param string $codeName |
||
88 | * @param string $expectMessage |
||
89 | * @dataProvider setCodeNameInvalidProvider |
||
90 | */ |
||
91 | View Code Duplication | public function testSetCodeNameInvalid($codeName, $expectMessage) |
|
103 | |||
104 | View Code Duplication | public function testSetCodeNameDuplicate() |
|
105 | { |
||
106 | // Create a Spreadsheet with three Worksheets (the first is created automatically) |
||
126 | } |
||
127 |
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.