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 |
||
7 | class Workbook |
||
8 | { |
||
9 | /** |
||
10 | * @var int[] |
||
11 | */ |
||
12 | private $printTitleStarts; |
||
13 | |||
14 | /** |
||
15 | * @var int[] |
||
16 | */ |
||
17 | private $printTitleEnds; |
||
18 | |||
19 | /** |
||
20 | * Set the range of rows to repeat when printing the excel file. |
||
21 | * $startRow=1 and $endRow=1 will repeat the first row only. |
||
22 | * |
||
23 | * @param int $startRow |
||
24 | * @param int $endRow |
||
25 | * @param string $sheetName |
||
26 | */ |
||
27 | 3 | public function setPrintTitleRange($startRow, $endRow, $sheetName) |
|
34 | |||
35 | /** |
||
36 | * Get final/combined XML for workbook.xml file. |
||
37 | * |
||
38 | * @param string[] $sheetNames |
||
39 | * @return string |
||
40 | */ |
||
41 | 7 | public function getWorkbookXml(array $sheetNames) |
|
48 | |||
49 | /** |
||
50 | * Get XML for sheet relations of the workbook. |
||
51 | * |
||
52 | * @param string[] $sheetNames |
||
53 | * @return string |
||
54 | */ |
||
55 | 4 | View Code Duplication | public function getWorkbookRelsXml($sheetNames) |
64 | |||
65 | /** |
||
66 | * Generate xml with all sheet names that should be linked to the workbook. |
||
67 | * |
||
68 | * @param string[] $sheetNames |
||
69 | * @return string |
||
70 | */ |
||
71 | 7 | View Code Duplication | private function getWorkbookSheetsXml($sheetNames) |
80 | |||
81 | /** |
||
82 | * Generate xml used for repeatable headers when printing of exporting to PDF, |
||
83 | * or empty string if none are set. |
||
84 | * |
||
85 | * @param string[] $sheetNames |
||
86 | * @return string |
||
87 | */ |
||
88 | 7 | private function getDefinedNamesXml(array $sheetNames) |
|
101 | |||
102 | /** |
||
103 | * $localSheetId is the only one that has to start from 0 instead of 1. |
||
104 | * |
||
105 | * @param string $sheetName |
||
106 | * @param int $localSheetId |
||
107 | * @return string |
||
108 | */ |
||
109 | 2 | private function createSingleDefinedNameXml($sheetName, $localSheetId) |
|
120 | } |
||
121 |
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.