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) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param FilterCondition $filterCondition |
||
| 134 | * @param string $token |
||
| 135 | * @param QueryBuilder $queryBuilder |
||
| 136 | * |
||
| 137 | * @throws ColumnNotFoundException |
||
| 138 | * @throws \Exception |
||
| 139 | * |
||
| 140 | * @return Expr|string |
||
| 141 | */ |
||
| 142 | protected function buildCondition(FilterCondition $filterCondition, $token, QueryBuilder $queryBuilder) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param FilterCondition $filterCondition |
||
| 196 | * @param QueryBuilder $queryBuilder |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | * @throws ColumnNotFoundException |
||
| 200 | */ |
||
| 201 | protected function buildUniqueToken(FilterCondition $filterCondition, QueryBuilder $queryBuilder) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Helper method: transforms a column identifier to a database field for use |
||
| 210 | * in filtering and sorting |
||
| 211 | * |
||
| 212 | * @param array|Field[] $fields |
||
| 213 | * @param string $dataSourceFieldUniqueName |
||
| 214 | * |
||
| 215 | * @throws ColumnNotFoundException |
||
| 216 | * @return mixed |
||
| 217 | */ |
||
| 218 | protected function getDatabaseFilterQueryFieldByDataSourceFieldUniqueName(array $fields, $dataSourceFieldUniqueName) |
||
| 234 | } |
||
| 235 |