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 |
||
54 | class Criteria implements CriteriaInterface, |
||
55 | ModelAwareInterface |
||
56 | { |
||
57 | |||
58 | use CursorAwareTrait, |
||
59 | DecoratableTrait, |
||
60 | LimitableTrait, |
||
61 | ModelAwareTrait, |
||
62 | SelectableTrait, |
||
63 | SortableTrait; |
||
64 | |||
65 | /** |
||
66 | * @since v1.0 |
||
67 | * @var array $operators supported operators lists |
||
68 | */ |
||
69 | public static $operators = [ |
||
70 | // Comparison |
||
71 | // Matches values that are equal to a specified value. |
||
72 | 'eq' => '$eq', |
||
73 | 'equals' => '$eq', |
||
74 | '==' => '$eq', |
||
75 | // Matches values that are greater than a specified value. |
||
76 | 'gt' => '$gt', |
||
77 | 'greater' => '$gt', |
||
78 | '>' => '$gt', |
||
79 | // Matches values that are greater than or equal to a specified value. |
||
80 | 'gte' => '$gte', |
||
81 | 'greatereq' => '$gte', |
||
82 | '>=' => '$gte', |
||
83 | // Matches values that are less than a specified value. |
||
84 | 'lt' => '$lt', |
||
85 | 'less' => '$lt', |
||
86 | '<' => '$lt', |
||
87 | // Matches values that are less than or equal to a specified value. |
||
88 | 'lte' => '$lte', |
||
89 | 'lesseq' => '$lte', |
||
90 | '<=' => '$lte', |
||
91 | // Matches all values that are not equal to a specified value. |
||
92 | 'ne' => '$ne', |
||
93 | 'noteq' => '$ne', |
||
94 | '!=' => '$ne', |
||
95 | '<>' => '$ne', |
||
96 | // Matches any of the values specified in an array. |
||
97 | 'in' => '$in', |
||
98 | // Matches none of the values specified in an array. |
||
99 | 'notin' => '$nin', |
||
100 | // Logical |
||
101 | // Joins query clauses with a logical OR returns all documents that match the conditions of either clause. |
||
102 | 'or' => '$or', |
||
103 | // Joins query clauses with a logical AND returns all documents that match the conditions of both clauses. |
||
104 | 'and' => '$and', |
||
105 | // Inverts the effect of a query expression and returns documents that do not match the query expression. |
||
106 | 'not' => '$not', |
||
107 | // Joins query clauses with a logical NOR returns all documents that fail to match both clauses. |
||
108 | 'nor' => '$nor', |
||
109 | // Element |
||
110 | // Matches documents that have the specified field. |
||
111 | 'exists' => '$exists', |
||
112 | 'notexists' => '$exists', |
||
113 | // Selects documents if a field is of the specified type. |
||
114 | 'type' => '$type', |
||
115 | // Evaluation |
||
116 | // Performs a modulo operation on the value of a field and selects documents with a specified result. |
||
117 | 'mod' => '$mod', |
||
118 | '%' => '$mod', |
||
119 | // Selects documents where values match a specified regular expression. |
||
120 | 'regex' => '$regex', |
||
121 | // Performs text search. |
||
122 | 'text' => '$text', |
||
123 | // Matches documents that satisfy a JavaScript expression. |
||
124 | 'where' => '$where', |
||
125 | // Geospatial |
||
126 | // Selects geometries within a bounding GeoJSON geometry. The `2dsphere` and `2d` indexes support $geoWithin. |
||
127 | 'geoWithin' => '$geoWithin', |
||
128 | // Selects geometries that intersect with a GeoJSON geometry. The `2dsphere` index supports $geoIntersects. |
||
129 | 'geoIntersects' => '$geoIntersects', |
||
130 | // Returns geospatial objects in proximity to a point. Requires a geospatial index. The `2dsphere` and `2d` indexes support $near. |
||
131 | 'near' => '$near', |
||
132 | // Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The `2dsphere` and `2d` indexes support $nearSphere. |
||
133 | 'nearSphere' => '$nearSphere', |
||
134 | // Array |
||
135 | // Matches arrays that contain all elements specified in the query. |
||
136 | 'all' => '$all', |
||
137 | // Selects documents if element in the array field matches all the specified $elemMatch conditions. |
||
138 | 'elemmatch' => '$elemMatch', |
||
139 | // Selects documents if the array field is a specified size. |
||
140 | 'size' => '$size', |
||
141 | // Comments |
||
142 | 'comment' => '$comment' |
||
143 | ]; |
||
144 | |||
145 | /** |
||
146 | * Sort Ascending |
||
147 | */ |
||
148 | const SortAsc = SortInterface::SortAsc; |
||
149 | |||
150 | /** |
||
151 | * Sort Descending |
||
152 | */ |
||
153 | const SortDesc = SortInterface::SortDesc; |
||
154 | |||
155 | /** |
||
156 | * Sort Ascending |
||
157 | * @deprecated since version 4.0.7 |
||
158 | */ |
||
159 | const SORT_ASC = SortInterface::SortAsc; |
||
160 | |||
161 | /** |
||
162 | * Sort Descending |
||
163 | * @deprecated since version 4.0.7 |
||
164 | */ |
||
165 | const SORT_DESC = SortInterface::SortDesc; |
||
166 | |||
167 | private $_conditions = []; |
||
168 | |||
169 | /** |
||
170 | * Raw conditions array |
||
171 | * @var mixed[] |
||
172 | */ |
||
173 | private $_rawConds = []; |
||
174 | private $_workingFields = []; |
||
175 | |||
176 | /** |
||
177 | * Constructor |
||
178 | * Example criteria: |
||
179 | * |
||
180 | * <PRE> |
||
181 | * 'criteria' = array( |
||
182 | * 'conditions'=>array( |
||
183 | * 'fieldName1'=>array('greater' => 0), |
||
184 | * 'fieldName2'=>array('>=' => 10), |
||
185 | * 'fieldName3'=>array('<' => 10), |
||
186 | * 'fieldName4'=>array('lessEq' => 10), |
||
187 | * 'fieldName5'=>array('notEq' => 10), |
||
188 | * 'fieldName6'=>array('in' => array(10, 9)), |
||
189 | * 'fieldName7'=>array('notIn' => array(10, 9)), |
||
190 | * 'fieldName8'=>array('all' => array(10, 9)), |
||
191 | * 'fieldName9'=>array('size' => 10), |
||
192 | * 'fieldName10'=>array('exists'), |
||
193 | * 'fieldName11'=>array('notExists'), |
||
194 | * 'fieldName12'=>array('mod' => array(10, 9)), |
||
195 | * 'fieldName13'=>array('==' => 1) |
||
196 | * ), |
||
197 | * 'select'=>array('fieldName', 'fieldName2'), |
||
198 | * 'limit'=>10, |
||
199 | * 'offset'=>20, |
||
200 | * 'sort'=>array('fieldName1'=>Criteria::SortAsc, 'fieldName2'=>Criteria::SortDesc), |
||
201 | * ); |
||
202 | * </PRE> |
||
203 | * @param mixed|CriteriaInterface|Conditions $criteria |
||
204 | * @param AnnotatedInterface|null Model to use for criteria decoration |
||
205 | * @since v1.0 |
||
206 | */ |
||
207 | 96 | public function __construct($criteria = null, AnnotatedInterface $model = null) |
|
286 | |||
287 | /** |
||
288 | * Merge with other criteria |
||
289 | * - Field list operators will be merged |
||
290 | * - Limit and offet will be overriden |
||
291 | * - Select fields list will be merged |
||
292 | * - Sort fields list will be merged |
||
293 | * @param null|array|CriteriaInterface $criteria |
||
294 | * @return CriteriaInterface |
||
295 | * @since v1.0 |
||
296 | */ |
||
297 | 89 | public function mergeWith($criteria) |
|
331 | |||
332 | 96 | private function _mergeConditions($source, $conditions) |
|
369 | |||
370 | /** |
||
371 | * If we have operator add it otherwise call parent implementation |
||
372 | * @since v1.0 |
||
373 | */ |
||
374 | 1 | public function __call($fieldName, $parameters) |
|
409 | |||
410 | /** |
||
411 | * @since v1.0.2 |
||
412 | */ |
||
413 | public function __get($name) |
||
418 | |||
419 | /** |
||
420 | * @since v1.0.2 |
||
421 | */ |
||
422 | 12 | public function __set($name, $value) |
|
429 | |||
430 | /** |
||
431 | * Return query array |
||
432 | * @return array query array |
||
433 | * @since v1.0 |
||
434 | */ |
||
435 | 96 | public function getConditions() |
|
444 | |||
445 | /** |
||
446 | * Set conditions |
||
447 | * @param array|Conditions $conditions |
||
448 | * @return Criteria |
||
449 | */ |
||
450 | public function setConditions($conditions) |
||
460 | |||
461 | /** |
||
462 | * Add condition |
||
463 | * If specified field already has a condition, values will be merged |
||
464 | * duplicates will be overriden by new values! |
||
465 | * |
||
466 | * NOTE: Should NOT be part of interface |
||
467 | * |
||
468 | * @param string $fieldName |
||
469 | * @param string $op operator |
||
470 | * @param mixed $value |
||
471 | * @since v1.0 |
||
472 | */ |
||
473 | 88 | public function addCond($fieldName, $op, $value) |
|
482 | |||
483 | /** |
||
484 | * Get condition |
||
485 | * If specified field already has a condition, values will be merged |
||
486 | * duplicates will be overridden by new values! |
||
487 | * @see getConditions |
||
488 | * @param string $fieldName |
||
489 | * @param string $op operator |
||
490 | * @param mixed $value |
||
491 | * @since v1.0 |
||
492 | */ |
||
493 | 88 | private function _makeCond($fieldName, $op, $value, $conditions = []) |
|
562 | |||
563 | } |
||
564 |