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 |
||
24 | class ExcelExport extends RenderableComponent |
||
25 | { |
||
26 | const NAME = 'excel_export'; |
||
27 | const INPUT_PARAM = 'xls'; |
||
28 | const DEFAULT_ROWS_LIMIT = 5000; |
||
29 | |||
30 | protected $template = '*.components.excel_export'; |
||
31 | protected $name = ExcelExport::NAME; |
||
32 | protected $render_section = RenderableRegistry::SECTION_END; |
||
33 | protected $rows_limit = self::DEFAULT_ROWS_LIMIT; |
||
34 | protected $extension = 'xls'; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $output; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $fileName; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $sheetName; |
||
50 | |||
51 | protected $ignored_columns = []; |
||
52 | |||
53 | protected $is_hidden_columns_exported = false; |
||
54 | |||
55 | protected $on_file_create; |
||
56 | |||
57 | protected $on_sheet_create; |
||
58 | |||
59 | /** |
||
60 | * @param Grid $grid |
||
61 | * @return null|void |
||
62 | */ |
||
63 | View Code Duplication | public function initialize(Grid $grid) |
|
75 | |||
76 | /** |
||
77 | * Sets name of exported file. |
||
78 | * |
||
79 | * @param string $name |
||
80 | * @return $this |
||
81 | */ |
||
82 | public function setFileName($name) |
||
87 | |||
88 | /** |
||
89 | * Returns name of exported file. |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | public function getFileName() |
||
97 | |||
98 | /** |
||
99 | * @param string $name |
||
100 | * @return $this |
||
101 | */ |
||
102 | public function setSheetName($name) |
||
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getSheetName() |
||
115 | |||
116 | /** |
||
117 | * @param string $name |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function setExtension($name) |
||
125 | |||
126 | /** |
||
127 | * @return string |
||
128 | */ |
||
129 | public function getExtension() |
||
133 | |||
134 | /** |
||
135 | * @return int |
||
136 | */ |
||
137 | public function getRowsLimit() |
||
141 | |||
142 | /** |
||
143 | * @param int $limit |
||
144 | * |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function setRowsLimit($limit) |
||
152 | |||
153 | protected function resetPagination(DataProvider $provider) |
||
159 | |||
160 | /** |
||
161 | * @param FieldConfig $column |
||
162 | * @return bool |
||
163 | */ |
||
164 | protected function isColumnExported(FieldConfig $column) |
||
169 | |||
170 | /** |
||
171 | * @internal |
||
172 | * @return array |
||
173 | */ |
||
174 | public function getData() |
||
199 | |||
200 | protected function renderExcel() |
||
208 | |||
209 | protected function escapeString($str) |
||
213 | |||
214 | protected function getHeaderRow() |
||
224 | |||
225 | /** |
||
226 | * @return string[] |
||
227 | */ |
||
228 | public function getIgnoredColumns() |
||
232 | |||
233 | /** |
||
234 | * @param string[] $ignoredColumns |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function setIgnoredColumns(array $ignoredColumns) |
||
242 | |||
243 | /** |
||
244 | * @return boolean |
||
245 | */ |
||
246 | public function isHiddenColumnsExported() |
||
250 | |||
251 | /** |
||
252 | * @param bool $isHiddenColumnsExported |
||
253 | * @return $this |
||
254 | */ |
||
255 | public function setHiddenColumnsExported($isHiddenColumnsExported) |
||
260 | |||
261 | /** |
||
262 | * @return mixed |
||
263 | */ |
||
264 | public function getOnFileCreate() |
||
273 | |||
274 | /** |
||
275 | * @param callable $onFileCreate |
||
276 | * |
||
277 | * @return $this |
||
278 | */ |
||
279 | public function setOnFileCreate($onFileCreate) |
||
284 | |||
285 | /** |
||
286 | * @return callable |
||
287 | */ |
||
288 | public function getOnSheetCreate() |
||
297 | |||
298 | /** |
||
299 | * @param callable $onSheetCreate |
||
300 | * |
||
301 | * @return $this |
||
302 | */ |
||
303 | public function setOnSheetCreate($onSheetCreate) |
||
308 | } |
||
309 |
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.