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:
Complex classes like GridViewWidget often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GridViewWidget, and based on these observations, apply Extract Interface, too.
1 | <?php /** MicroGridViewWidget */ |
||
25 | class GridViewWidget extends Widget |
||
26 | { |
||
27 | /** @var int $page Current page on table */ |
||
28 | public $page = 0; |
||
29 | /** @var int $limit Limit current rows */ |
||
30 | public $limit = 10; |
||
31 | /** @var bool $filters Usage filters */ |
||
32 | public $filters = true; |
||
33 | /** @var string $template Template render */ |
||
34 | public $template = '{counter}{table}{pager}'; |
||
35 | /** @var string $templateTable Template table render */ |
||
36 | public $templateTable = '{headers}{filters}{rows}'; |
||
37 | /** @var string $textCounter text for before counter */ |
||
38 | public $counterText = 'Sum: '; |
||
39 | /** @var string $emptyText text to render if rows not found */ |
||
40 | public $emptyText = 'Elements not found'; |
||
41 | /** @var array $attributesEmpty Attributes for empty text */ |
||
42 | public $attributesEmpty = []; |
||
43 | /** @var array $attributes attributes for table */ |
||
44 | public $attributes = []; |
||
45 | /** @var array $attributesCounter attributes for counter */ |
||
46 | public $attributesCounter = []; |
||
47 | /** @var array $attributesHeading attributes for heading */ |
||
48 | public $attributesHeading = []; |
||
49 | /** @var array $attributesFilter attributes for filter row */ |
||
50 | public $attributesFilter = []; |
||
51 | /** @var array $attributesFilterForm attributes for filter form */ |
||
52 | public $attributesFilterForm = []; |
||
53 | /** @var array $tableConfig table configuration */ |
||
54 | public $tableConfig = []; |
||
55 | /** @var array $paginationConfig parameters for PaginationWidget */ |
||
56 | public $paginationConfig = []; |
||
57 | |||
58 | /** @var array $rows Rows from data */ |
||
59 | protected $rows; |
||
60 | /** @var array $fields Fields of data */ |
||
61 | protected $fields = []; |
||
62 | /** @var int $rowsCount Count rows */ |
||
63 | protected $rowsCount = 0; |
||
64 | /** @var int $totalCount Total count data */ |
||
65 | protected $totalCount = 0; |
||
66 | /** @var string $filterPrefix prefix for filter name */ |
||
67 | protected $filterPrefix; |
||
68 | |||
69 | |||
70 | /** |
||
71 | * Re-declare widget constructor |
||
72 | * |
||
73 | * @access public |
||
74 | * |
||
75 | * @param array $args arguments |
||
76 | * |
||
77 | * @result void |
||
78 | * @throws Exception |
||
79 | */ |
||
80 | public function __construct(array $args = []) |
||
129 | |||
130 | /** |
||
131 | * Initialize widget |
||
132 | * |
||
133 | * @access public |
||
134 | * |
||
135 | * @result void |
||
136 | */ |
||
137 | public function init() |
||
157 | |||
158 | /** |
||
159 | * Running widget |
||
160 | * |
||
161 | * @access public |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | public function run() |
||
180 | |||
181 | /** |
||
182 | * Get counter |
||
183 | * |
||
184 | * @access protected |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | protected function getCounter() |
||
193 | |||
194 | /** |
||
195 | * Get pager |
||
196 | * |
||
197 | * @access protected |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | View Code Duplication | protected function getPager() |
|
212 | |||
213 | /** |
||
214 | * Get table |
||
215 | * |
||
216 | * @access protected |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | protected function getTable() |
||
230 | |||
231 | /** |
||
232 | * Render heading |
||
233 | * |
||
234 | * @access protected |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | protected function renderHeading() |
||
257 | |||
258 | /** |
||
259 | * Render filters |
||
260 | * |
||
261 | * @access protected |
||
262 | * |
||
263 | * @return null|string |
||
264 | */ |
||
265 | protected function renderFilters() |
||
295 | |||
296 | /** |
||
297 | * Render rows |
||
298 | * |
||
299 | * @access protected |
||
300 | * |
||
301 | * @return null|string |
||
302 | */ |
||
303 | protected function renderRows() |
||
336 | } |
||
337 |
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.