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 |
||
57 | class Criteria implements CriteriaInterface, |
||
58 | ModelAwareInterface |
||
59 | { |
||
60 | |||
61 | use CursorAwareTrait, |
||
62 | DecoratableTrait, |
||
63 | LimitableTrait, |
||
64 | ModelAwareTrait, |
||
65 | SelectableTrait, |
||
66 | SortableTrait; |
||
67 | |||
68 | /** |
||
69 | * @since v1.0 |
||
70 | * @var array $operators supported operators lists |
||
71 | */ |
||
72 | public static $operators = [ |
||
73 | // Comparison |
||
74 | // Matches values that are equal to a specified value. |
||
75 | 'eq' => '$eq', |
||
76 | 'equals' => '$eq', |
||
77 | '==' => '$eq', |
||
78 | // Matches values that are greater than a specified value. |
||
79 | 'gt' => '$gt', |
||
80 | 'greater' => '$gt', |
||
81 | '>' => '$gt', |
||
82 | // Matches values that are greater than or equal to a specified value. |
||
83 | 'gte' => '$gte', |
||
84 | 'greatereq' => '$gte', |
||
85 | '>=' => '$gte', |
||
86 | // Matches values that are less than a specified value. |
||
87 | 'lt' => '$lt', |
||
88 | 'less' => '$lt', |
||
89 | '<' => '$lt', |
||
90 | // Matches values that are less than or equal to a specified value. |
||
91 | 'lte' => '$lte', |
||
92 | 'lesseq' => '$lte', |
||
93 | '<=' => '$lte', |
||
94 | // Matches all values that are not equal to a specified value. |
||
95 | 'ne' => '$ne', |
||
96 | 'noteq' => '$ne', |
||
97 | '!=' => '$ne', |
||
98 | '<>' => '$ne', |
||
99 | // Matches any of the values specified in an array. |
||
100 | 'in' => '$in', |
||
101 | // Matches none of the values specified in an array. |
||
102 | 'notin' => '$nin', |
||
103 | // Logical |
||
104 | // Joins query clauses with a logical OR returns all documents that match the conditions of either clause. |
||
105 | 'or' => '$or', |
||
106 | // Joins query clauses with a logical AND returns all documents that match the conditions of both clauses. |
||
107 | 'and' => '$and', |
||
108 | // Inverts the effect of a query expression and returns documents that do not match the query expression. |
||
109 | 'not' => '$not', |
||
110 | // Joins query clauses with a logical NOR returns all documents that fail to match both clauses. |
||
111 | 'nor' => '$nor', |
||
112 | // Element |
||
113 | // Matches documents that have the specified field. |
||
114 | 'exists' => '$exists', |
||
115 | 'notexists' => '$exists', |
||
116 | // Selects documents if a field is of the specified type. |
||
117 | 'type' => '$type', |
||
118 | // Evaluation |
||
119 | // Performs a modulo operation on the value of a field and selects documents with a specified result. |
||
120 | 'mod' => '$mod', |
||
121 | '%' => '$mod', |
||
122 | // Selects documents where values match a specified regular expression. |
||
123 | 'regex' => '$regex', |
||
124 | // Performs text search. |
||
125 | 'text' => '$text', |
||
126 | // Matches documents that satisfy a JavaScript expression. |
||
127 | 'where' => '$where', |
||
128 | // Geospatial |
||
129 | // Selects geometries within a bounding GeoJSON geometry. The `2dsphere` and `2d` indexes support $geoWithin. |
||
130 | 'geoWithin' => '$geoWithin', |
||
131 | // Selects geometries that intersect with a GeoJSON geometry. The `2dsphere` index supports $geoIntersects. |
||
132 | 'geoIntersects' => '$geoIntersects', |
||
133 | // Returns geospatial objects in proximity to a point. Requires a geospatial index. The `2dsphere` and `2d` indexes support $near. |
||
134 | 'near' => '$near', |
||
135 | // Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The `2dsphere` and `2d` indexes support $nearSphere. |
||
136 | 'nearSphere' => '$nearSphere', |
||
137 | // Array |
||
138 | // Matches arrays that contain all elements specified in the query. |
||
139 | 'all' => '$all', |
||
140 | // Selects documents if element in the array field matches all the specified $elemMatch conditions. |
||
141 | 'elemmatch' => '$elemMatch', |
||
142 | // Selects documents if the array field is a specified size. |
||
143 | 'size' => '$size', |
||
144 | // Comments |
||
145 | 'comment' => '$comment' |
||
146 | ]; |
||
147 | |||
148 | /** |
||
149 | * Sort Ascending |
||
150 | */ |
||
151 | const SortAsc = SortInterface::SortAsc; |
||
152 | |||
153 | /** |
||
154 | * Sort Descending |
||
155 | */ |
||
156 | const SortDesc = SortInterface::SortDesc; |
||
157 | |||
158 | /** |
||
159 | * Sort Ascending |
||
160 | * @deprecated since version 4.0.7 |
||
161 | */ |
||
162 | const SORT_ASC = SortInterface::SortAsc; |
||
163 | |||
164 | /** |
||
165 | * Sort Descending |
||
166 | * @deprecated since version 4.0.7 |
||
167 | */ |
||
168 | const SORT_DESC = SortInterface::SortDesc; |
||
169 | |||
170 | private $_conditions = []; |
||
171 | |||
172 | /** |
||
173 | * Raw conditions array |
||
174 | * @var mixed[] |
||
175 | */ |
||
176 | private $_rawConds = []; |
||
177 | |||
178 | /** |
||
179 | * Currently used fields list. This is |
||
180 | * used to allow chained criteria creation. |
||
181 | * |
||
182 | * Example: |
||
183 | * |
||
184 | * ``` |
||
185 | * $criteria->address->city->street->number = 666 |
||
186 | * ``` |
||
187 | * |
||
188 | * Will result in conditions: |
||
189 | * |
||
190 | * ``` |
||
191 | * [ |
||
192 | * 'address.city.street.number' = 666 |
||
193 | * ] |
||
194 | * ``` |
||
195 | * |
||
196 | * @var array |
||
197 | */ |
||
198 | private $_workingFields = []; |
||
199 | |||
200 | /** |
||
201 | * Constructor |
||
202 | * Example criteria: |
||
203 | * |
||
204 | * <pre> |
||
205 | * $criteria = new Criteria( |
||
206 | * [ |
||
207 | * 'conditions'=> [ |
||
208 | * 'fieldName1' => ['greater' => 0], |
||
209 | * 'fieldName2' => ['>=' => 10], |
||
210 | * 'fieldName3' => ['<' => 10], |
||
211 | * 'fieldName4' => ['lessEq' => 10], |
||
212 | * 'fieldName5' => ['notEq' => 10], |
||
213 | * 'fieldName6' => ['in' => [10, 9]], |
||
214 | * 'fieldName7' => ['notIn' => [10, 9]], |
||
215 | * 'fieldName8' => ['all' => [10, 9]], |
||
216 | * 'fieldName9' => ['size' => 10], |
||
217 | * 'fieldName10' => ['exists'], |
||
218 | * 'fieldName11' => ['notExists'], |
||
219 | * 'fieldName12' => ['mod' => [10, 9]], |
||
220 | * 'fieldName13' => ['==' => 1] |
||
221 | * ], |
||
222 | * 'select' => [ |
||
223 | * 'fieldName', |
||
224 | * 'fieldName2' |
||
225 | * ], |
||
226 | * 'limit' => 10, |
||
227 | * 'offset' => 20, |
||
228 | * 'sort'=>[ |
||
229 | * 'fieldName1' => Criteria::SortAsc, |
||
230 | * 'fieldName2' => Criteria::SortDesc, |
||
231 | * ] |
||
232 | * ] |
||
233 | * ); |
||
234 | * </pre> |
||
235 | * @param mixed|CriteriaInterface|Conditions $criteria |
||
236 | * @param AnnotatedInterface|null Model to use for criteria decoration |
||
237 | * @throws Exception |
||
238 | */ |
||
239 | 140 | public function __construct($criteria = null, AnnotatedInterface $model = null) |
|
325 | |||
326 | /** |
||
327 | * Merge with other criteria |
||
328 | * - Field list operators will be merged |
||
329 | * - Limit and offset will be overridden |
||
330 | * - Select fields list will be merged |
||
331 | * - Sort fields list will be merged |
||
332 | * @param null|array|CriteriaInterface $criteria |
||
333 | * @return $this |
||
334 | * @throws Exception |
||
335 | */ |
||
336 | 135 | public function mergeWith($criteria) |
|
407 | |||
408 | /** |
||
409 | * Internal method for merging `_conditions` with `getConditions` call result. |
||
410 | * @param $source |
||
411 | * @param $conditions |
||
412 | * @return mixed Merged conditions array |
||
413 | */ |
||
414 | 140 | private function _mergeConditions($source, $conditions) |
|
415 | { |
||
416 | 140 | $opTable = array_values(self::$operators); |
|
417 | 140 | foreach ($conditions as $fieldName => $conds) |
|
418 | { |
||
419 | if ( |
||
420 | 130 | is_array($conds) && |
|
421 | 130 | count(array_diff(array_keys($conds), $opTable)) == 0 |
|
422 | ) |
||
423 | { |
||
424 | 13 | if (isset($source[$fieldName]) && is_array($source[$fieldName])) |
|
425 | { |
||
426 | 2 | foreach ($source[$fieldName] as $operator => $value) |
|
427 | { |
||
428 | 2 | if (!in_array($operator, $opTable)) |
|
429 | { |
||
430 | unset($source[$fieldName][$operator]); |
||
431 | } |
||
432 | } |
||
433 | } |
||
434 | else |
||
435 | { |
||
436 | 13 | $source[$fieldName] = []; |
|
437 | } |
||
438 | |||
439 | 13 | foreach ($conds as $operator => $value) |
|
440 | { |
||
441 | 13 | $source[$fieldName][$operator] = $value; |
|
442 | } |
||
443 | } |
||
444 | else |
||
445 | { |
||
446 | 130 | $source[$fieldName] = $conds; |
|
447 | } |
||
448 | } |
||
449 | 140 | return $source; |
|
450 | } |
||
451 | |||
452 | /** |
||
453 | * By-call-syntax criteria handler |
||
454 | * |
||
455 | * @param $fieldName |
||
456 | * @param mixed $parameters |
||
457 | * @return $this |
||
458 | */ |
||
459 | 1 | public function __call($fieldName, $parameters) |
|
460 | { |
||
461 | 1 | $operatorName = self::$operators['eq']; |
|
462 | |||
463 | // Call with operator and value. Set |
||
464 | // first param to be operator. |
||
465 | 1 | if (array_key_exists(0, $parameters) && array_key_exists(1, $parameters)) |
|
466 | { |
||
467 | 1 | $operatorName = strtolower($parameters[0]); |
|
468 | } |
||
469 | |||
470 | // Call without operator, use value only |
||
471 | 1 | if (array_key_exists(0, $parameters) && !array_key_exists(1, $parameters)) |
|
472 | { |
||
473 | $value = $parameters[0]; |
||
474 | } |
||
475 | |||
476 | // Call with operator and value, use second param as value |
||
477 | 1 | if (array_key_exists(1, $parameters)) |
|
478 | { |
||
479 | 1 | $value = $parameters[1]; |
|
480 | } |
||
481 | |||
482 | // ??? |
||
483 | 1 | if (is_numeric($operatorName)) |
|
484 | { |
||
485 | $operatorName = strtolower(trim($value)); |
||
486 | $value = (strtolower(trim($value)) === 'exists') ? true : false; |
||
487 | } |
||
488 | |||
489 | 1 | if (!in_array($operatorName, array_keys(self::$operators))) |
|
490 | { |
||
491 | throw new UnexpectedValueException("Unknown operator: `$operatorName` on field `$fieldName`"); |
||
492 | } |
||
493 | |||
494 | /** |
||
495 | * Support for syntax: |
||
496 | * |
||
497 | * ``` |
||
498 | * $criteria->fieldOne->subField('op', 'value') |
||
499 | * ``` |
||
500 | */ |
||
501 | |||
502 | 1 | array_push($this->_workingFields, $fieldName); |
|
503 | 1 | $fieldName = implode('.', $this->_workingFields); |
|
504 | 1 | $this->_workingFields = []; |
|
505 | 1 | switch ($operatorName) |
|
506 | { |
||
507 | 1 | case 'exists': |
|
508 | $this->addCond($fieldName, $operatorName, true); |
||
509 | break; |
||
510 | 1 | case 'notexists': |
|
511 | $this->addCond($fieldName, $operatorName, false); |
||
512 | break; |
||
513 | default: |
||
514 | 1 | $this->addCond($fieldName, $operatorName, $value); |
|
515 | } |
||
516 | |||
517 | 1 | return $this; |
|
518 | } |
||
519 | |||
520 | /** |
||
521 | * This is required for chained criteria creating, ie |
||
522 | * |
||
523 | * ``` |
||
524 | * $criteria->fieldOne->fieldTwo = 123; |
||
525 | * ``` |
||
526 | * |
||
527 | * @param string $name |
||
528 | * @return $this |
||
529 | */ |
||
530 | 2 | public function __get($name) |
|
535 | |||
536 | /** |
||
537 | * By-set-syntax handler. |
||
538 | * |
||
539 | * This allows adding *equal* conditions by |
||
540 | * using field. |
||
541 | * |
||
542 | * Example: |
||
543 | * |
||
544 | * ``` |
||
545 | * $criteria->userId = 1; |
||
546 | * ``` |
||
547 | * |
||
548 | * @param string $name |
||
549 | * @param mixed $value |
||
550 | */ |
||
551 | 11 | public function __set($name, $value) |
|
558 | |||
559 | /** |
||
560 | * Return query array |
||
561 | * @return array Query array |
||
562 | */ |
||
563 | 140 | public function getConditions() |
|
572 | |||
573 | /** |
||
574 | * Set conditions |
||
575 | * @param array|Conditions $conditions |
||
576 | * @return Criteria |
||
577 | */ |
||
578 | public function setConditions($conditions) |
||
588 | |||
589 | /** |
||
590 | * Add condition |
||
591 | * If specified field already has a condition, values will be merged |
||
592 | * duplicates will be overriden by new values! |
||
593 | * |
||
594 | * NOTE: Should NOT be part of interface |
||
595 | * |
||
596 | * @param string $fieldName |
||
597 | * @param string $op operator |
||
598 | * @param mixed $value |
||
599 | * @return $this |
||
600 | */ |
||
601 | 130 | public function addCond($fieldName, $op, $value) |
|
610 | |||
611 | /** |
||
612 | * Get condition |
||
613 | * If specified field already has a condition, values will be merged |
||
614 | * duplicates will be overridden by new values! |
||
615 | * @see getConditions |
||
616 | * @param string $fieldName |
||
617 | * @param string $op operator |
||
618 | * @param mixed $value |
||
619 | * @param array $conditions |
||
620 | * @return array |
||
621 | */ |
||
622 | 130 | private function _makeCond($fieldName, $op, $value, $conditions = []) |
|
691 | |||
692 | } |
||
693 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..