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 ListBuilder 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 ListBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class ListBuilder extends BaseBuilder |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $excelActions = null; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $batchActions = null; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $scopeColumns = null; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $filterColumns = null; |
||
37 | |||
38 | /** |
||
39 | * (non-PHPdoc) |
||
40 | * @see Admingenerator\GeneratorBundle\Builder.BaseBuilder::getYamlKey() |
||
41 | */ |
||
42 | public function getYamlKey() |
||
46 | |||
47 | /** |
||
48 | * Retrieve the FQCN formType used by this builder |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | public function getFormType() |
||
61 | |||
62 | public function getFilterColumns() |
||
71 | |||
72 | protected function findFilterColumns() |
||
89 | |||
90 | protected function addFilterColumn(Column $column) |
||
94 | |||
95 | public function getFilterColumnsCredentials() |
||
111 | |||
112 | /** |
||
113 | * Find scopes parameters |
||
114 | */ |
||
115 | public function getScopes() |
||
119 | |||
120 | /** |
||
121 | * @return array |
||
122 | */ |
||
123 | public function getScopeColumns() |
||
132 | |||
133 | protected function findScopeColumns() |
||
142 | |||
143 | /** |
||
144 | * @return array Scopes display column names |
||
145 | */ |
||
146 | protected function getScopesDisplayColumns() |
||
163 | |||
164 | protected function addScopeColumn(Column $column) |
||
168 | |||
169 | /** |
||
170 | * Return a list of batch action from list.batch_actions |
||
171 | * @return array |
||
172 | */ |
||
173 | public function getBatchActions() |
||
182 | |||
183 | View Code Duplication | protected function setUserBatchActionConfiguration(Action $action) |
|
205 | |||
206 | protected function addBatchAction(Action $action) |
||
210 | |||
211 | View Code Duplication | protected function findBatchActions() |
|
232 | |||
233 | /** |
||
234 | * Return a list of actions from excel.export |
||
235 | * |
||
236 | * @return array |
||
237 | */ |
||
238 | public function getExcelActions() |
||
247 | |||
248 | protected function fillExportActions() |
||
264 | |||
265 | public function getExportParamsForKey($key, $name, $default) |
||
274 | } |
||
275 |
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.