Complex classes like Criteria 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 Criteria, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
51 | class Criteria implements CriteriaInterface |
||
52 | { |
||
53 | |||
54 | use CursorAwareTrait, |
||
55 | DecoratableTrait, |
||
56 | LimitableTrait, |
||
57 | SelectableTrait, |
||
58 | SortableTrait; |
||
59 | |||
60 | /** |
||
61 | * @since v1.0 |
||
62 | * @var array $operators supported operators lists |
||
63 | */ |
||
64 | public static $operators = [ |
||
65 | // Comparison |
||
66 | // Matches values that are equal to a specified value. |
||
67 | 'eq' => '$eq', |
||
68 | 'equals' => '$eq', |
||
69 | '==' => '$eq', |
||
70 | // Matches values that are greater than a specified value. |
||
71 | 'gt' => '$gt', |
||
72 | 'greater' => '$gt', |
||
73 | '>' => '$gt', |
||
74 | // Matches values that are greater than or equal to a specified value. |
||
75 | 'gte' => '$gte', |
||
76 | 'greatereq' => '$gte', |
||
77 | '>=' => '$gte', |
||
78 | // Matches values that are less than a specified value. |
||
79 | 'lt' => '$lt', |
||
80 | 'less' => '$lt', |
||
81 | '<' => '$lt', |
||
82 | // Matches values that are less than or equal to a specified value. |
||
83 | 'lte' => '$lte', |
||
84 | 'lesseq' => '$lte', |
||
85 | '<=' => '$lte', |
||
86 | // Matches all values that are not equal to a specified value. |
||
87 | 'ne' => '$ne', |
||
88 | 'noteq' => '$ne', |
||
89 | '!=' => '$ne', |
||
90 | '<>' => '$ne', |
||
91 | // Matches any of the values specified in an array. |
||
92 | 'in' => '$in', |
||
93 | // Matches none of the values specified in an array. |
||
94 | 'notin' => '$nin', |
||
95 | // Logical |
||
96 | // Joins query clauses with a logical OR returns all documents that match the conditions of either clause. |
||
97 | 'or' => '$or', |
||
98 | // Joins query clauses with a logical AND returns all documents that match the conditions of both clauses. |
||
99 | 'and' => '$and', |
||
100 | // Inverts the effect of a query expression and returns documents that do not match the query expression. |
||
101 | 'not' => '$not', |
||
102 | // Joins query clauses with a logical NOR returns all documents that fail to match both clauses. |
||
103 | 'nor' => '$nor', |
||
104 | // Element |
||
105 | // Matches documents that have the specified field. |
||
106 | 'exists' => '$exists', |
||
107 | 'notexists' => '$exists', |
||
108 | // Selects documents if a field is of the specified type. |
||
109 | 'type' => '$type', |
||
110 | // Evaluation |
||
111 | // Performs a modulo operation on the value of a field and selects documents with a specified result. |
||
112 | 'mod' => '$mod', |
||
113 | '%' => '$mod', |
||
114 | // Selects documents where values match a specified regular expression. |
||
115 | 'regex' => '$regex', |
||
116 | // Performs text search. |
||
117 | 'text' => '$text', |
||
118 | // Matches documents that satisfy a JavaScript expression. |
||
119 | 'where' => '$where', |
||
120 | // Geospatial |
||
121 | // Selects geometries within a bounding GeoJSON geometry. The `2dsphere` and `2d` indexes support $geoWithin. |
||
122 | 'geoWithin' => '$geoWithin', |
||
123 | // Selects geometries that intersect with a GeoJSON geometry. The `2dsphere` index supports $geoIntersects. |
||
124 | 'geoIntersects' => '$geoIntersects', |
||
125 | // Returns geospatial objects in proximity to a point. Requires a geospatial index. The `2dsphere` and `2d` indexes support $near. |
||
126 | 'near' => '$near', |
||
127 | // Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The `2dsphere` and `2d` indexes support $nearSphere. |
||
128 | 'nearSphere' => '$nearSphere', |
||
129 | // Array |
||
130 | // Matches arrays that contain all elements specified in the query. |
||
131 | 'all' => '$all', |
||
132 | // Selects documents if element in the array field matches all the specified $elemMatch conditions. |
||
133 | 'elemmatch' => '$elemMatch', |
||
134 | // Selects documents if the array field is a specified size. |
||
135 | 'size' => '$size', |
||
136 | // Comments |
||
137 | 'comment' => '$comment' |
||
138 | ]; |
||
139 | |||
140 | /** |
||
141 | * Sort Ascending |
||
142 | */ |
||
143 | const SortAsc = SortInterface::SortAsc; |
||
144 | |||
145 | /** |
||
146 | * Sort Descending |
||
147 | */ |
||
148 | const SortDesc = SortInterface::SortDesc; |
||
149 | |||
150 | /** |
||
151 | * Sort Ascending |
||
152 | * @deprecated since version 4.0.7 |
||
153 | */ |
||
154 | const SORT_ASC = SortInterface::SortAsc; |
||
155 | |||
156 | /** |
||
157 | * Sort Descending |
||
158 | * @deprecated since version 4.0.7 |
||
159 | */ |
||
160 | const SORT_DESC = SortInterface::SortDesc; |
||
161 | |||
162 | private $_conditions = []; |
||
163 | |||
164 | /** |
||
165 | * Raw conditions array |
||
166 | * @var mixed[] |
||
167 | */ |
||
168 | private $_rawConds = []; |
||
169 | private $_workingFields = []; |
||
170 | |||
171 | /** |
||
172 | * Constructor |
||
173 | * Example criteria: |
||
174 | * |
||
175 | * <PRE> |
||
176 | * 'criteria' = array( |
||
177 | * 'conditions'=>array( |
||
178 | * 'fieldName1'=>array('greater' => 0), |
||
179 | * 'fieldName2'=>array('>=' => 10), |
||
180 | * 'fieldName3'=>array('<' => 10), |
||
181 | * 'fieldName4'=>array('lessEq' => 10), |
||
182 | * 'fieldName5'=>array('notEq' => 10), |
||
183 | * 'fieldName6'=>array('in' => array(10, 9)), |
||
184 | * 'fieldName7'=>array('notIn' => array(10, 9)), |
||
185 | * 'fieldName8'=>array('all' => array(10, 9)), |
||
186 | * 'fieldName9'=>array('size' => 10), |
||
187 | * 'fieldName10'=>array('exists'), |
||
188 | * 'fieldName11'=>array('notExists'), |
||
189 | * 'fieldName12'=>array('mod' => array(10, 9)), |
||
190 | * 'fieldName13'=>array('==' => 1) |
||
191 | * ), |
||
192 | * 'select'=>array('fieldName', 'fieldName2'), |
||
193 | * 'limit'=>10, |
||
194 | * 'offset'=>20, |
||
195 | * 'sort'=>array('fieldName1'=>Criteria::SortAsc, 'fieldName2'=>Criteria::SortDesc), |
||
196 | * ); |
||
197 | * </PRE> |
||
198 | * @param mixed|CriteriaInterface|Conditions $criteria |
||
199 | * @param AnnotatedInterface|null Model to use for criteria decoration |
||
200 | * @since v1.0 |
||
201 | */ |
||
202 | 88 | public function __construct($criteria = null, AnnotatedInterface $model = null) |
|
263 | |||
264 | /** |
||
265 | * Merge with other criteria |
||
266 | * - Field list operators will be merged |
||
267 | * - Limit and offet will be overriden |
||
268 | * - Select fields list will be merged |
||
269 | * - Sort fields list will be merged |
||
270 | * @param array|CriteriaInterface $criteria |
||
271 | * @return CriteriaInterface |
||
272 | * @since v1.0 |
||
273 | */ |
||
274 | 81 | public function mergeWith($criteria) |
|
308 | |||
309 | 88 | private function _mergeConditions($source, $conditions) |
|
346 | |||
347 | /** |
||
348 | * If we have operator add it otherwise call parent implementation |
||
349 | * @since v1.0 |
||
350 | */ |
||
351 | 1 | public function __call($fieldName, $parameters) |
|
386 | |||
387 | /** |
||
388 | * @since v1.0.2 |
||
389 | */ |
||
390 | public function __get($name) |
||
395 | |||
396 | /** |
||
397 | * @since v1.0.2 |
||
398 | */ |
||
399 | 12 | public function __set($name, $value) |
|
406 | |||
407 | /** |
||
408 | * Return query array |
||
409 | * @return array query array |
||
410 | * @since v1.0 |
||
411 | */ |
||
412 | 88 | public function getConditions() |
|
421 | |||
422 | /** |
||
423 | * Set conditions |
||
424 | * @param array|Conditions $conditions |
||
425 | * @return Criteria |
||
426 | */ |
||
427 | public function setConditions($conditions) |
||
437 | |||
438 | /** |
||
439 | * Add condition |
||
440 | * If specified field already has a condition, values will be merged |
||
441 | * duplicates will be overriden by new values! |
||
442 | * |
||
443 | * NOTE: Should NOT be part of interface |
||
444 | * |
||
445 | * @param string $fieldName |
||
446 | * @param string $op operator |
||
447 | * @param mixed $value |
||
448 | * @since v1.0 |
||
449 | */ |
||
450 | 83 | public function addCond($fieldName, $op, $value) |
|
459 | |||
460 | /** |
||
461 | * Get condition |
||
462 | * If specified field already has a condition, values will be merged |
||
463 | * duplicates will be overridden by new values! |
||
464 | * @see getConditions |
||
465 | * @param string $fieldName |
||
466 | * @param string $op operator |
||
467 | * @param mixed $value |
||
468 | * @since v1.0 |
||
469 | */ |
||
470 | 83 | private function _makeCond($fieldName, $op, $value, $conditions = []) |
|
539 | |||
540 | } |
||
541 |