Complex classes like Filterer 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 Filterer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Filterer |
||
16 | { |
||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | private $uniqueNameToQueryFieldMap; |
||
21 | |||
22 | /** |
||
23 | * Apply the filters to the query builders |
||
24 | * |
||
25 | * @param QueryBuilder $queryBuilder |
||
26 | * @param Filter $filterDefinition |
||
27 | * @param array $uniqueNameToQueryFieldMap |
||
28 | * |
||
29 | * @return QueryBuilder |
||
30 | * @throws \Exception |
||
31 | */ |
||
32 | public function filter(QueryBuilder $queryBuilder, Filter $filterDefinition, $uniqueNameToQueryFieldMap) |
||
42 | |||
43 | /** |
||
44 | * Builds a filter group and, recursively, constructs a tree of conditions for the filters |
||
45 | * |
||
46 | * @param QueryBuilder $queryBuilder |
||
47 | * @param Filter $filterDefinition |
||
48 | * |
||
49 | * @throws \Exception |
||
50 | * @return Andx|Orx |
||
51 | */ |
||
52 | protected function buildFilterGroup(QueryBuilder $queryBuilder, Filter $filterDefinition) |
||
69 | |||
70 | /** |
||
71 | * @param Composite $expressions |
||
72 | * @param Filter $filterDefinition |
||
73 | * @param QueryBuilder $queryBuilder |
||
74 | * |
||
75 | * @throws \Exception |
||
76 | */ |
||
77 | protected function addExpressionsForFilter(Composite $expressions, Filter $filterDefinition, QueryBuilder $queryBuilder) |
||
88 | |||
89 | /** |
||
90 | * @param Composite $expressions |
||
91 | * @param FilterCondition $filterCondition |
||
92 | * @param QueryBuilder $queryBuilder |
||
93 | * |
||
94 | * @throws ColumnNotFoundException |
||
95 | * @throws \Exception |
||
96 | */ |
||
97 | protected function addExpressionsForFilterCondition(Composite $expressions, FilterCondition $filterCondition, QueryBuilder $queryBuilder) |
||
132 | |||
133 | /** |
||
134 | * @param FilterCondition $filterCondition |
||
135 | * @param string $token |
||
136 | * @param QueryBuilder $queryBuilder |
||
137 | * |
||
138 | * @throws ColumnNotFoundException |
||
139 | * @throws \Exception |
||
140 | * |
||
141 | * @return Expr|string |
||
142 | */ |
||
143 | protected function buildCondition(FilterCondition $filterCondition, $token, QueryBuilder $queryBuilder) |
||
210 | |||
211 | /** |
||
212 | * @param FilterCondition $filterCondition |
||
213 | * @param QueryBuilder $queryBuilder |
||
214 | * |
||
215 | * @return string |
||
216 | * @throws ColumnNotFoundException |
||
217 | */ |
||
218 | protected function buildUniqueToken(FilterCondition $filterCondition, QueryBuilder $queryBuilder) |
||
224 | |||
225 | /** |
||
226 | * Helper method: transforms a column identifier to a database field for use |
||
227 | * in filtering and sorting |
||
228 | * |
||
229 | * @param array|Field[] $fields |
||
230 | * @param string $dataSourceFieldUniqueName |
||
231 | * |
||
232 | * @throws ColumnNotFoundException |
||
233 | * @return mixed |
||
234 | */ |
||
235 | protected function getDatabaseFilterQueryFieldByDataSourceFieldUniqueName(array $fields, $dataSourceFieldUniqueName) |
||
251 | |||
252 | /** |
||
253 | * @param string $method |
||
254 | * |
||
255 | * @return bool |
||
256 | */ |
||
257 | private function isNullFiltering($method) |
||
265 | |||
266 | /** |
||
267 | * @param string $method |
||
268 | * |
||
269 | * @return bool |
||
270 | */ |
||
271 | private function isNotNullFiltering($method) |
||
279 | } |
||
280 |