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 | 115 | public function __construct($criteria = null, AnnotatedInterface $model = null) |
|
| 208 | { |
||
| 209 | 115 | if (!empty($model)) |
|
| 210 | { |
||
| 211 | 12 | $this->setModel($model); |
|
| 212 | } |
||
| 213 | 115 | $this->setCd(new ConditionDecorator($model)); |
|
| 214 | 115 | if (is_array($criteria)) |
|
| 215 | { |
||
| 216 | 3 | $available = ['conditions', 'select', 'limit', 'offset', 'sort', 'useCursor']; |
|
| 217 | |||
| 218 | 3 | $diff = array_diff_key($criteria, array_flip($available)); |
|
| 219 | 3 | if (!empty($diff)) |
|
| 220 | { |
||
| 221 | $params = [ |
||
| 222 | '[' . implode(', ', $available) . ']', |
||
| 223 | '[' . implode(', ', array_keys($criteria)) . ']' |
||
| 224 | ]; |
||
| 225 | $msg = vsprintf('Allowed criteria keys are: %s, however was provided: %s', $params); |
||
| 226 | throw new UnexpectedValueException($msg); |
||
| 227 | } |
||
| 228 | |||
| 229 | 3 | if (isset($criteria['conditions'])) |
|
| 230 | 3 | foreach ($criteria['conditions'] as $fieldName => $conditions) |
|
| 231 | { |
||
| 232 | 3 | $fieldNameArray = explode('.', $fieldName); |
|
| 233 | 3 | if (count($fieldNameArray) === 1) |
|
| 234 | { |
||
| 235 | 3 | $fieldName = array_shift($fieldNameArray); |
|
| 236 | } |
||
| 237 | else |
||
| 238 | { |
||
| 239 | $fieldName = array_pop($fieldNameArray); |
||
| 240 | } |
||
| 241 | |||
| 242 | 3 | $this->_workingFields = $fieldNameArray; |
|
| 243 | 3 | assert(is_array($conditions), 'Each condition must be array with operator as key and value, ie: ["_id" => ["==" => "123"]]'); |
|
| 244 | 3 | foreach ($conditions as $operator => $value) |
|
| 245 | { |
||
| 246 | 3 | $operator = strtolower($operator); |
|
| 247 | 3 | if(!isset(self::$operators[$operator])) |
|
| 248 | { |
||
| 249 | $params = [ |
||
| 250 | $operator, |
||
| 251 | $fieldName |
||
| 252 | ]; |
||
| 253 | $msg = vsprintf('Unknown Criteria operator `%s` for `%s`', $params); |
||
| 254 | throw new UnexpectedValueException($msg); |
||
| 255 | } |
||
| 256 | 3 | $this->addCond($fieldName, $operator, $value); |
|
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | 3 | if (isset($criteria['select'])) |
|
| 261 | { |
||
| 262 | $this->select($criteria['select']); |
||
| 263 | } |
||
| 264 | 3 | if (isset($criteria['limit'])) |
|
| 265 | { |
||
| 266 | $this->limit($criteria['limit']); |
||
| 267 | } |
||
| 268 | 3 | if (isset($criteria['offset'])) |
|
| 269 | { |
||
| 270 | $this->offset($criteria['offset']); |
||
| 271 | } |
||
| 272 | 3 | if (isset($criteria['sort'])) |
|
| 273 | { |
||
| 274 | $this->setSort($criteria['sort']); |
||
| 275 | } |
||
| 276 | 3 | if (isset($criteria['useCursor'])) |
|
| 277 | { |
||
| 278 | 3 | $this->setUseCursor($criteria['useCursor']); |
|
| 279 | } |
||
| 280 | } |
||
| 281 | // NOTE: |
||
| 282 | //Scrunitizer: $criteria is of type object<Maslosoft\Mangan\...ria\MergeableInterface>, but the function expects a array|object<Maslosoft\M...aces\CriteriaInterface>. |
||
| 283 | // But for now it should be this way to easyli distinguish from Conditions. |
||
| 284 | // Future plan: Use CriteriaInterface here, and drop `$criteria instanceof Conditions` if clause. Conditions should implement CriteriaInterface too. |
||
| 285 | 115 | elseif ($criteria instanceof MergeableInterface) |
|
| 286 | { |
||
| 287 | assert($criteria instanceof CriteriaInterface); |
||
| 288 | $this->mergeWith($criteria); |
||
| 289 | } |
||
| 290 | 115 | elseif ($criteria instanceof Conditions) |
|
| 291 | { |
||
| 292 | $this->setConditions($criteria); |
||
| 293 | } |
||
| 294 | 115 | } |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Merge with other criteria |
||
| 298 | * - Field list operators will be merged |
||
| 299 | * - Limit and offet will be overriden |
||
| 300 | * - Select fields list will be merged |
||
| 301 | * - Sort fields list will be merged |
||
| 302 | * @param null|array|CriteriaInterface $criteria |
||
| 303 | * @return CriteriaInterface |
||
| 304 | * @since v1.0 |
||
| 305 | */ |
||
| 306 | 107 | public function mergeWith($criteria) |
|
| 340 | |||
| 341 | 115 | private function _mergeConditions($source, $conditions) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * If we have operator add it otherwise call parent implementation |
||
| 381 | * @since v1.0 |
||
| 382 | */ |
||
| 383 | 1 | public function __call($fieldName, $parameters) |
|
| 384 | { |
||
| 385 | 1 | if (isset($parameters[0])) |
|
| 386 | { |
||
| 387 | 1 | $operatorName = strtolower($parameters[0]); |
|
| 388 | } |
||
| 389 | 1 | if (array_key_exists(1, $parameters)) |
|
| 390 | { |
||
| 391 | 1 | $value = $parameters[1]; |
|
| 392 | } |
||
| 393 | 1 | if (is_numeric($operatorName)) |
|
| 394 | { |
||
| 395 | $operatorName = strtolower(trim($value)); |
||
| 396 | $value = (strtolower(trim($value)) === 'exists') ? true : false; |
||
| 397 | } |
||
| 398 | |||
| 399 | 1 | if (in_array($operatorName, array_keys(self::$operators))) |
|
| 400 | { |
||
| 401 | 1 | array_push($this->_workingFields, $fieldName); |
|
| 402 | 1 | $fieldName = implode('.', $this->_workingFields); |
|
| 403 | 1 | $this->_workingFields = []; |
|
| 404 | 1 | switch ($operatorName) |
|
| 405 | { |
||
| 406 | 1 | case 'exists': |
|
| 407 | $this->addCond($fieldName, $operatorName, true); |
||
| 408 | break; |
||
| 409 | 1 | case 'notexists': |
|
| 410 | $this->addCond($fieldName, $operatorName, false); |
||
| 411 | break; |
||
| 412 | default: |
||
| 413 | 1 | $this->addCond($fieldName, $operatorName, $value); |
|
| 414 | } |
||
| 415 | 1 | return $this; |
|
| 416 | } |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @since v1.0.2 |
||
| 421 | */ |
||
| 422 | public function __get($name) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @since v1.0.2 |
||
| 430 | */ |
||
| 431 | 9 | public function __set($name, $value) |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Return query array |
||
| 441 | * @return array query array |
||
| 442 | * @since v1.0 |
||
| 443 | */ |
||
| 444 | 115 | public function getConditions() |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Set conditions |
||
| 456 | * @param array|Conditions $conditions |
||
| 457 | * @return Criteria |
||
| 458 | */ |
||
| 459 | public function setConditions($conditions) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Add condition |
||
| 472 | * If specified field already has a condition, values will be merged |
||
| 473 | * duplicates will be overriden by new values! |
||
| 474 | * |
||
| 475 | * NOTE: Should NOT be part of interface |
||
| 476 | * |
||
| 477 | * @param string $fieldName |
||
| 478 | * @param string $op operator |
||
| 479 | * @param mixed $value |
||
| 480 | * @since v1.0 |
||
| 481 | */ |
||
| 482 | 105 | public function addCond($fieldName, $op, $value) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Get condition |
||
| 494 | * If specified field already has a condition, values will be merged |
||
| 495 | * duplicates will be overridden by new values! |
||
| 496 | * @see getConditions |
||
| 497 | * @param string $fieldName |
||
| 498 | * @param string $op operator |
||
| 499 | * @param mixed $value |
||
| 500 | * @since v1.0 |
||
| 501 | */ |
||
| 502 | 105 | private function _makeCond($fieldName, $op, $value, $conditions = []) |
|
| 571 | |||
| 572 | } |
||
| 573 |