Complex classes like Builder 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 Builder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Builder |
||
22 | { |
||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $columnAliases = array(); |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $columnField = 'data'; // or 'name' |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $indexColumn = '*'; |
||
37 | |||
38 | /** |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $returnCollection = false; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected $caseInsensitive = false; |
||
47 | |||
48 | /** |
||
49 | * @var QueryBuilder|ORMQueryBuilder |
||
50 | */ |
||
51 | protected $queryBuilder; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $requestParams; |
||
57 | |||
58 | /** |
||
59 | * @return array |
||
60 | */ |
||
61 | public function getData() |
||
97 | |||
98 | /** |
||
99 | * @return QueryBuilder|ORMQueryBuilder |
||
100 | */ |
||
101 | public function getFilteredQuery() |
||
174 | |||
175 | /** |
||
176 | * @return int |
||
177 | */ |
||
178 | public function getRecordsFiltered() |
||
193 | |||
194 | /** |
||
195 | * @return int |
||
196 | */ |
||
197 | public function getRecordsTotal() |
||
212 | |||
213 | /** |
||
214 | * @return array |
||
215 | */ |
||
216 | public function getResponse() |
||
225 | |||
226 | /** |
||
227 | * @param string $indexColumn |
||
228 | * @return static |
||
229 | */ |
||
230 | public function withIndexColumn($indexColumn) |
||
235 | |||
236 | /** |
||
237 | * @param array $columnAliases |
||
238 | * @return static |
||
239 | */ |
||
240 | public function withColumnAliases($columnAliases) |
||
245 | |||
246 | /** |
||
247 | * @param array $returnObjectCollection |
||
248 | * @return static |
||
249 | */ |
||
250 | public function withReturnCollection($returnCollection) |
||
255 | |||
256 | /** |
||
257 | * @param bool $caseInsensitive |
||
258 | * @return static |
||
259 | */ |
||
260 | public function withCaseInsensitive($caseInsensitive) |
||
265 | |||
266 | /** |
||
267 | * @param string $columnField |
||
268 | * @return static |
||
269 | */ |
||
270 | public function withColumnField($columnField) |
||
275 | |||
276 | /** |
||
277 | * @param QueryBuilder|ORMQueryBuilder $queryBuilder |
||
278 | * @return static |
||
279 | */ |
||
280 | public function withQueryBuilder($queryBuilder) |
||
285 | |||
286 | /** |
||
287 | * @param array $requestParams |
||
288 | * @return static |
||
289 | */ |
||
290 | public function withRequestParams($requestParams) |
||
295 | } |
||
296 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.