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