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 IOFactory |
||
8 | { |
||
9 | /** |
||
10 | * Search locations. |
||
11 | * |
||
12 | * @var array |
||
13 | * @static |
||
14 | */ |
||
15 | private static $searchLocations = [ |
||
16 | ['type' => 'IWriter', 'path' => 'PhpSpreadsheet/Writer/{0}.php', 'class' => '\\PhpOffice\\PhpSpreadsheet\\Writer\\{0}'], |
||
17 | ['type' => 'IReader', 'path' => 'PhpSpreadsheet/Reader/{0}.php', 'class' => '\\PhpOffice\\PhpSpreadsheet\\Reader\\{0}'], |
||
18 | ]; |
||
19 | |||
20 | /** |
||
21 | * Autoresolve classes. |
||
22 | * |
||
23 | * @var array |
||
24 | * @static |
||
25 | */ |
||
26 | private static $autoResolveClasses = [ |
||
27 | 'Xlsx', |
||
28 | 'Xls', |
||
29 | 'Xml', |
||
30 | 'Ods', |
||
31 | 'Slk', |
||
32 | 'Gnumeric', |
||
33 | 'Html', |
||
34 | 'Csv', |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * Private constructor for IOFactory. |
||
39 | */ |
||
40 | private function __construct() |
||
43 | |||
44 | /** |
||
45 | * Get search locations. |
||
46 | * |
||
47 | * @static |
||
48 | * |
||
49 | * @return array |
||
50 | */ |
||
51 | public static function getSearchLocations() |
||
55 | |||
56 | /** |
||
57 | * Set search locations. |
||
58 | * |
||
59 | * @static |
||
60 | * |
||
61 | * @param array $value |
||
62 | * |
||
63 | * @throws Reader\Exception |
||
64 | */ |
||
65 | public static function setSearchLocations(array $value) |
||
69 | |||
70 | /** |
||
71 | * Add search location. |
||
72 | * |
||
73 | * @static |
||
74 | * |
||
75 | * @param string $type Example: IWriter |
||
76 | * @param string $location Example: PhpSpreadsheet/Writer/{0}.php |
||
77 | * @param string $classname Example: Writer\{0} |
||
78 | */ |
||
79 | public static function addSearchLocation($type, $location, $classname) |
||
83 | |||
84 | /** |
||
85 | * Create Writer\IWriter. |
||
86 | * |
||
87 | * @static |
||
88 | * |
||
89 | * @param Spreadsheet $spreadsheet |
||
90 | * @param string $writerType Example: Xlsx |
||
91 | * |
||
92 | * @throws Writer\Exception |
||
93 | * |
||
94 | * @return Writer\IWriter |
||
95 | */ |
||
96 | 60 | View Code Duplication | public static function createWriter(Spreadsheet $spreadsheet, $writerType) |
116 | |||
117 | /** |
||
118 | * Create Reader\IReader. |
||
119 | * |
||
120 | * @static |
||
121 | * |
||
122 | * @param string $readerType Example: Xlsx |
||
123 | * |
||
124 | * @throws Reader\Exception |
||
125 | * |
||
126 | * @return Reader\IReader |
||
127 | */ |
||
128 | 46 | View Code Duplication | public static function createReader($readerType) |
148 | |||
149 | /** |
||
150 | * Loads Spreadsheet from file using automatic Reader\IReader resolution. |
||
151 | * |
||
152 | * @static |
||
153 | * |
||
154 | * @param string $pFilename The name of the spreadsheet file |
||
155 | * |
||
156 | * @throws Reader\Exception |
||
157 | * |
||
158 | * @return Spreadsheet |
||
159 | */ |
||
160 | 11 | public static function load($pFilename) |
|
166 | |||
167 | /** |
||
168 | * Identify file type using automatic Reader\IReader resolution. |
||
169 | * |
||
170 | * @static |
||
171 | * |
||
172 | * @param string $pFilename The name of the spreadsheet file to identify |
||
173 | * |
||
174 | * @throws Reader\Exception |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | 10 | public static function identify($pFilename) |
|
187 | |||
188 | /** |
||
189 | * Create Reader\IReader for file using automatic Reader\IReader resolution. |
||
190 | * |
||
191 | * @static |
||
192 | * |
||
193 | * @param string $pFilename The name of the spreadsheet file |
||
194 | * |
||
195 | * @throws Reader\Exception |
||
196 | * |
||
197 | * @return Reader\IReader |
||
198 | */ |
||
199 | 21 | public static function createReaderForFile($pFilename) |
|
275 | } |
||
276 |
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.