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 */ |
||
27 | class GridViewWidget extends Widget |
||
28 | { |
||
29 | /** @var int $page Current page on table */ |
||
30 | public $page = 0; |
||
31 | /** @var int $limit Limit current rows */ |
||
32 | public $limit = 10; |
||
33 | /** @var bool $filters Usage filters */ |
||
34 | public $filters = true; |
||
35 | /** @var string $template Template render */ |
||
36 | public $template = '{counter}{table}{pager}'; |
||
37 | /** @var string $templateTable Template table render */ |
||
38 | public $templateTable = '{headers}{filters}{rows}'; |
||
39 | /** @var string $textCounter text for before counter */ |
||
40 | public $counterText = 'Sum: '; |
||
41 | /** @var string $emptyText text to render if rows not found */ |
||
42 | public $emptyText = 'Elements not found'; |
||
43 | /** @var array $attributesEmpty Attributes for empty text */ |
||
44 | public $attributesEmpty = []; |
||
45 | /** @var array $attributes attributes for table */ |
||
46 | public $attributes = []; |
||
47 | /** @var array $attributesCounter attributes for counter */ |
||
48 | public $attributesCounter = []; |
||
49 | /** @var array $attributesHeading attributes for heading */ |
||
50 | public $attributesHeading = []; |
||
51 | /** @var array $attributesFilter attributes for filter row */ |
||
52 | public $attributesFilter = []; |
||
53 | /** @var array $attributesFilterForm attributes for filter form */ |
||
54 | public $attributesFilterForm = []; |
||
55 | /** @var array $tableConfig table configuration */ |
||
56 | public $tableConfig = []; |
||
57 | /** @var array $paginationConfig parameters for PaginationWidget */ |
||
58 | public $paginationConfig = []; |
||
59 | |||
60 | /** @var array $rows Rows from data */ |
||
61 | protected $rows; |
||
62 | /** @var array $fields Fields of data */ |
||
63 | protected $fields = []; |
||
64 | /** @var int $rowsCount Count rows */ |
||
65 | protected $rowsCount = 0; |
||
66 | /** @var int $totalCount Total count data */ |
||
67 | protected $totalCount = 0; |
||
68 | /** @var string $filterPrefix prefix for filter name */ |
||
69 | protected $filterPrefix; |
||
70 | |||
71 | |||
72 | /** |
||
73 | * Re-declare widget constructor |
||
74 | * |
||
75 | * @access public |
||
76 | * |
||
77 | * @param array $args arguments |
||
78 | * |
||
79 | * @result void |
||
80 | * @throws Exception |
||
81 | */ |
||
82 | public function __construct(array $args = []) |
||
136 | |||
137 | /** |
||
138 | * Initialize widget |
||
139 | * |
||
140 | * @access public |
||
141 | * |
||
142 | * @result void |
||
143 | */ |
||
144 | public function init() |
||
164 | |||
165 | /** |
||
166 | * Running widget |
||
167 | * |
||
168 | * @access public |
||
169 | * |
||
170 | * @return string |
||
171 | * @throws Exception |
||
172 | */ |
||
173 | public function run() |
||
188 | |||
189 | /** |
||
190 | * Get counter |
||
191 | * |
||
192 | * @access protected |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | protected function getCounter() |
||
201 | |||
202 | /** |
||
203 | * Get pager |
||
204 | * |
||
205 | * @access protected |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | View Code Duplication | protected function getPager() |
|
220 | |||
221 | /** |
||
222 | * Get table |
||
223 | * |
||
224 | * @access protected |
||
225 | * |
||
226 | * @return string |
||
227 | * @throws Exception |
||
228 | */ |
||
229 | protected function getTable() |
||
239 | |||
240 | /** |
||
241 | * Render heading |
||
242 | * |
||
243 | * @access protected |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | protected function renderHeading() |
||
266 | |||
267 | /** |
||
268 | * Render filters |
||
269 | * |
||
270 | * @access protected |
||
271 | * |
||
272 | * @return null|string |
||
273 | * @throws Exception |
||
274 | */ |
||
275 | protected function renderFilters() |
||
306 | |||
307 | /** |
||
308 | * Render rows |
||
309 | * |
||
310 | * @access protected |
||
311 | * |
||
312 | * @return null|string |
||
313 | */ |
||
314 | protected function renderRows() |
||
347 | } |
||
348 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: