Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Expression 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 Expression, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Expression implements ArrayableInterface |
||
26 | { |
||
27 | /** |
||
28 | * @deprecated Since 1.22 using this property is NOT ALLOWED. Use getters and setters instead. |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $_expression = array(); |
||
32 | |||
33 | /** |
||
34 | * Create new instance of expression |
||
35 | * @return Expression |
||
36 | */ |
||
37 | public function expression() |
||
41 | /** |
||
42 | * @param string $field |
||
43 | * @param string|array $value |
||
44 | * |
||
45 | * @return Expression |
||
46 | */ |
||
47 | public function where($field, $value) |
||
57 | |||
58 | /** |
||
59 | * Filter empty field |
||
60 | * |
||
61 | * @param string $field |
||
62 | * |
||
63 | * @return Expression |
||
64 | */ |
||
65 | View Code Duplication | public function whereEmpty($field) |
|
77 | |||
78 | View Code Duplication | public function whereNotEmpty($field) |
|
87 | |||
88 | public function whereGreater($field, $value) |
||
92 | |||
93 | public function whereGreaterOrEqual($field, $value) |
||
97 | |||
98 | public function whereLess($field, $value) |
||
102 | |||
103 | public function whereLessOrEqual($field, $value) |
||
107 | |||
108 | public function whereNotEqual($field, $value) |
||
112 | |||
113 | /** |
||
114 | * Selects the documents where the value of a |
||
115 | * field equals any value in the specified array. |
||
116 | * |
||
117 | * @param string $field |
||
118 | * @param array $values |
||
119 | * @return Expression |
||
120 | */ |
||
121 | public function whereIn($field, array $values) |
||
125 | |||
126 | public function whereNotIn($field, array $values) |
||
130 | |||
131 | public function whereExists($field) |
||
135 | |||
136 | public function whereNotExists($field) |
||
140 | |||
141 | public function whereHasType($field, $type) |
||
145 | |||
146 | public function whereDouble($field) |
||
150 | |||
151 | public function whereString($field) |
||
155 | |||
156 | public function whereObject($field) |
||
160 | |||
161 | public function whereBoolean($field) |
||
165 | |||
166 | public function whereArray($field) |
||
170 | |||
171 | public function whereArrayOfArrays($field) |
||
175 | |||
176 | public function whereObjectId($field) |
||
180 | |||
181 | public function whereDate($field) |
||
185 | |||
186 | public function whereNull($field) |
||
190 | |||
191 | public function whereJsCondition($condition) |
||
195 | |||
196 | public function whereLike($field, $regex, $caseInsensitive = true) |
||
215 | |||
216 | /** |
||
217 | * Find documents where the value of a field is an array |
||
218 | * that contains all the specified elements. |
||
219 | * This is equivalent of logical AND. |
||
220 | * |
||
221 | * @link http://docs.mongodb.org/manual/reference/operator/query/all/ |
||
222 | * |
||
223 | * @param string $field point-delimited field name |
||
224 | * @param array $values |
||
225 | * @return Expression |
||
226 | */ |
||
227 | public function whereAll($field, array $values) |
||
231 | |||
232 | /** |
||
233 | * Find documents where the value of a field is an array |
||
234 | * that contains none of the specified elements. |
||
235 | * This is equivalent of logical AND. |
||
236 | * |
||
237 | * @link http://docs.mongodb.org/manual/reference/operator/query/all/ |
||
238 | * |
||
239 | * @param string $field point-delimited field name |
||
240 | * @param array $values |
||
241 | * @return Expression |
||
242 | */ |
||
243 | public function whereNoneOf($field, array $values) |
||
251 | |||
252 | /** |
||
253 | * Find documents where the value of a field is an array |
||
254 | * that contains any of the specified elements. |
||
255 | * This is equivalent of logical AND. |
||
256 | * |
||
257 | * @param string $field point-delimited field name |
||
258 | * @param array $values |
||
259 | * @return Expression |
||
260 | */ |
||
261 | public function whereAny($field, array $values) |
||
265 | |||
266 | /** |
||
267 | * Matches documents in a collection that contain an array field with at |
||
268 | * least one element that matches all the specified query criteria. |
||
269 | * |
||
270 | * @param string $field point-delimited field name |
||
271 | * @param \Sokil\Mongo\Expression|callable|array $expression |
||
272 | * |
||
273 | * @return Expression |
||
274 | * |
||
275 | * @throws Exception |
||
276 | */ |
||
277 | public function whereElemMatch($field, $expression) |
||
291 | |||
292 | /** |
||
293 | * Matches documents in a collection that contain an array field with elements |
||
294 | * that do not matches all the specified query criteria. |
||
295 | * |
||
296 | * @param string $field |
||
297 | * @param Expression|callable|array $expression |
||
298 | * |
||
299 | * @return Expression |
||
300 | */ |
||
301 | public function whereElemNotMatch($field, $expression) |
||
305 | |||
306 | /** |
||
307 | * Selects documents if the array field is a specified size. |
||
308 | * |
||
309 | * @param string $field |
||
310 | * @param integer $length |
||
311 | * @return Expression |
||
312 | */ |
||
313 | public function whereArraySize($field, $length) |
||
317 | |||
318 | /** |
||
319 | * Selects the documents that satisfy at least one of the expressions |
||
320 | * |
||
321 | * @param array|\Sokil\Mongo\Expression $expressions Array of Expression instances or comma delimited expression list |
||
322 | * |
||
323 | * @return Expression |
||
324 | */ |
||
325 | View Code Duplication | public function whereOr($expressions = null /**, ...**/) |
|
335 | |||
336 | /** |
||
337 | * Select the documents that satisfy all the expressions in the array |
||
338 | * |
||
339 | * @param array|\Sokil\Mongo\Expression $expressions Array of Expression instances or comma delimited expression list |
||
340 | * @return Expression |
||
341 | */ |
||
342 | View Code Duplication | public function whereAnd($expressions = null /**, ...**/) |
|
352 | |||
353 | /** |
||
354 | * Selects the documents that fail all the query expressions in the array |
||
355 | * |
||
356 | * @param array|\Sokil\Mongo\Expression $expressions Array of Expression instances or comma delimited expression list |
||
357 | * @return Expression |
||
358 | */ |
||
359 | View Code Duplication | public function whereNor($expressions = null /**, ...**/) |
|
375 | |||
376 | public function whereNot(Expression $expression) |
||
390 | |||
391 | /** |
||
392 | * Select documents where the value of a field divided by a divisor has the specified remainder (i.e. perform a modulo operation to select documents) |
||
393 | * |
||
394 | * @param string $field |
||
395 | * @param int $divisor |
||
396 | * @param int $remainder |
||
397 | */ |
||
398 | public function whereMod($field, $divisor, $remainder) |
||
406 | |||
407 | /** |
||
408 | * Perform fulltext search |
||
409 | * |
||
410 | * @link https://docs.mongodb.org/manual/reference/operator/query/text/ |
||
411 | * @link https://docs.mongodb.org/manual/tutorial/specify-language-for-text-index/ |
||
412 | * |
||
413 | * If a collection contains documents or embedded documents that are in different languages, |
||
414 | * include a field named language in the documents or embedded documents and specify as its value the language |
||
415 | * for that document or embedded document. |
||
416 | * |
||
417 | * The specified language in the document overrides the default language for the text index. |
||
418 | * The specified language in an embedded document override the language specified in an enclosing document or |
||
419 | * the default language for the index. |
||
420 | * |
||
421 | * Case Insensitivity: |
||
422 | * @link https://docs.mongodb.org/manual/reference/operator/query/text/#text-operator-case-sensitivity |
||
423 | * |
||
424 | * Diacritic Insensitivity: |
||
425 | * @link https://docs.mongodb.org/manual/reference/operator/query/text/#text-operator-diacritic-sensitivity |
||
426 | * |
||
427 | * @param $search A string of terms that MongoDB parses and uses to query the text index. MongoDB performs a |
||
428 | * logical OR search of the terms unless specified as a phrase. |
||
429 | * @param $language Optional. The language that determines the list of stop words for the search and the |
||
430 | * rules for the stemmer and tokenizer. If not specified, the search uses the default language of the index. |
||
431 | * If you specify a language value of "none", then the text search uses simple tokenization |
||
432 | * with no list of stop words and no stemming. |
||
433 | * @param bool|false $caseSensitive Allowed from v.3.2 A boolean flag to enable or disable case |
||
434 | * sensitive search. Defaults to false; i.e. the search defers to the case insensitivity of the text index. |
||
435 | * @param bool|false $diacriticSensitive Allowed from v.3.2 A boolean flag to enable or disable diacritic |
||
436 | * sensitive search against version 3 text indexes. Defaults to false; i.e. the search defers to the diacritic |
||
437 | * insensitivity of the text index. Text searches against earlier versions of the text index are inherently |
||
438 | * diacritic sensitive and cannot be diacritic insensitive. As such, the $diacriticSensitive option has no |
||
439 | * effect with earlier versions of the text index. |
||
440 | * @return $this |
||
441 | */ |
||
442 | public function whereText( |
||
468 | |||
469 | /** |
||
470 | * Find document near points in flat surface |
||
471 | * |
||
472 | * @param string $field |
||
473 | * @param float $longitude |
||
474 | * @param float $latitude |
||
475 | * @param int|array $distance distance from point in meters. Array distance |
||
476 | * allowed only in MongoDB 2.6 |
||
477 | * @return Expression |
||
478 | */ |
||
479 | View Code Duplication | public function nearPoint($field, $longitude, $latitude, $distance) |
|
505 | |||
506 | /** |
||
507 | * Find document near points in spherical surface |
||
508 | * |
||
509 | * @param string $field |
||
510 | * @param float $longitude |
||
511 | * @param float $latitude |
||
512 | * @param int|array $distance distance from point in meters. Array distance |
||
513 | * allowed only in MongoDB 2.6 |
||
514 | * @return Expression |
||
515 | */ |
||
516 | View Code Duplication | public function nearPointSpherical($field, $longitude, $latitude, $distance) |
|
542 | |||
543 | /** |
||
544 | * Selects documents whose geospatial data intersects with a specified |
||
545 | * GeoJSON object; i.e. where the intersection of the data and the |
||
546 | * specified object is non-empty. This includes cases where the data |
||
547 | * and the specified object share an edge. Uses spherical geometry. |
||
548 | * |
||
549 | * @link http://docs.mongodb.org/manual/reference/operator/query/geoIntersects/ |
||
550 | * |
||
551 | * @param string $field |
||
552 | * @param Geometry $geometry |
||
553 | * @return Expression |
||
554 | */ |
||
555 | public function intersects($field, Geometry $geometry) |
||
565 | |||
566 | /** |
||
567 | * Selects documents with geospatial data that exists entirely within a specified shape. |
||
568 | * |
||
569 | * @link http://docs.mongodb.org/manual/reference/operator/query/geoWithin/ |
||
570 | * @param string $field |
||
571 | * @param Geometry $geometry |
||
572 | * @return Expression |
||
573 | */ |
||
574 | public function within($field, Geometry $geometry) |
||
584 | |||
585 | /** |
||
586 | * Select documents with geospatial data within circle defined |
||
587 | * by center point and radius in flat surface |
||
588 | * |
||
589 | * @param string $field |
||
590 | * @param float $longitude |
||
591 | * @param float $latitude |
||
592 | * @param float $radius |
||
593 | * @return Expression |
||
594 | */ |
||
595 | public function withinCircle($field, $longitude, $latitude, $radius) |
||
608 | |||
609 | /** |
||
610 | * Select documents with geospatial data within circle defined |
||
611 | * by center point and radius in spherical surface |
||
612 | * |
||
613 | * To calculate distance in radians |
||
614 | * @see http://docs.mongodb.org/manual/tutorial/calculate-distances-using-spherical-geometry-with-2d-geospatial-indexes/ |
||
615 | * |
||
616 | * @param string $field |
||
617 | * @param float $longitude |
||
618 | * @param float $latitude |
||
619 | * @param float $radiusInRadians in radians. |
||
620 | * @return Expression |
||
621 | */ |
||
622 | public function withinCircleSpherical($field, $longitude, $latitude, $radiusInRadians) |
||
635 | |||
636 | /** |
||
637 | * Return documents that are within the bounds of the rectangle, according |
||
638 | * to their point-based location data. |
||
639 | * |
||
640 | * Based on grid coordinates and does not query for GeoJSON shapes. |
||
641 | * |
||
642 | * Use planar geometry, so 2d index may be used but not required |
||
643 | * |
||
644 | * @param string $field |
||
645 | * @param array $bottomLeftCoordinate Bottom left coordinate of box |
||
646 | * @param array $upperRightCoordinate Upper right coordinate of box |
||
647 | * @return Expression |
||
648 | */ |
||
649 | public function withinBox($field, array $bottomLeftCoordinate, array $upperRightCoordinate) |
||
662 | |||
663 | /** |
||
664 | * Return documents that are within the polygon, according |
||
665 | * to their point-based location data. |
||
666 | * |
||
667 | * Based on grid coordinates and does not query for GeoJSON shapes. |
||
668 | * |
||
669 | * Use planar geometry, so 2d index may be used but not required |
||
670 | * |
||
671 | * @param string $field |
||
672 | * @param array $points array of coordinates |
||
673 | * @return Expression |
||
674 | */ |
||
675 | public function withinPolygon($field, array $points) |
||
685 | |||
686 | public function toArray() |
||
690 | |||
691 | public function merge(Expression $expression) |
||
696 | |||
697 | /** |
||
698 | * Transform expression in different formats to canonical array form |
||
699 | * |
||
700 | * @param callable|array|Expression $mixed |
||
701 | * |
||
702 | * @return array |
||
703 | * |
||
704 | * @throws \Sokil\Mongo\Exception |
||
705 | */ |
||
706 | public static function convertToArray($mixed) |
||
727 | } |
||
728 |