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:
| 1 | <?php |
||
| 17 | class Pipeline implements Arrayable, \JsonSerializable |
||
| 18 | { |
||
| 19 | |||
| 20 | private $stages = array(); |
||
| 21 | |||
| 22 | private $options = array(); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var \Sokil\Mongo\Collection |
||
| 26 | */ |
||
| 27 | private $collection; |
||
| 28 | |||
| 29 | public function __construct(Collection $collection) |
||
| 33 | |||
| 34 | private function addStage($operator, $stage) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Filter documents by expression |
||
| 50 | * |
||
| 51 | * @param array|\Sokil\Mongo\Expression $expression |
||
| 52 | * @return \Sokil\Mongo\Pipeline |
||
| 53 | * @throws \Sokil\Mongo\Exception |
||
| 54 | */ |
||
| 55 | View Code Duplication | public function match($expression) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Passes along the documents with only the specified fields to the next |
||
| 72 | * stage in the pipeline. The specified fields can be existing fields |
||
| 73 | * from the input documents or newly computed fields. |
||
| 74 | * |
||
| 75 | * @param array $pipeline |
||
| 76 | * @return \Sokil\Mongo\Pipeline |
||
| 77 | */ |
||
| 78 | public function project(array $pipeline) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Groups documents by some specified expression and outputs to the next |
||
| 86 | * stage a document for each distinct grouping. The output documents |
||
| 87 | * contain an _id field which contains the distinct group by key. The |
||
| 88 | * output documents can also contain computed fields that hold the values |
||
| 89 | * of some accumulator expression grouped by the $group‘s _id field. $group |
||
| 90 | * does not order its output documents. |
||
| 91 | * |
||
| 92 | * @link http://docs.mongodb.org/manual/reference/operator/aggregation/group/ |
||
| 93 | * |
||
| 94 | * @param array|callable $stage |
||
| 95 | * @return \Sokil\Mongo\Pipeline |
||
| 96 | * @throws \Sokil\Mongo\Exception |
||
| 97 | */ |
||
| 98 | public function group($stage) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Deconstructs an array field from the input documents to output a document for each element. |
||
| 124 | * Each output document is the input document with the value of the array field replaced by the element. |
||
| 125 | * @link http://docs.mongodb.org/manual/reference/operator/aggregation/unwind/ |
||
| 126 | * |
||
| 127 | * @param string $path path to field |
||
| 128 | * @return \Sokil\Mongo\Pipeline |
||
| 129 | */ |
||
| 130 | public function unwind($path) |
||
| 135 | |||
| 136 | public function sort(array $sortFields) |
||
| 141 | |||
| 142 | public function limit($limit) |
||
| 147 | |||
| 148 | public function skip($skip) |
||
| 153 | |||
| 154 | public function aggregate(array $options = array()) { |
||
| 157 | |||
| 158 | public function aggregateCursor(array $options = array()) |
||
| 162 | |||
| 163 | public function toArray() |
||
| 167 | |||
| 168 | public function jsonSerialize() |
||
| 172 | |||
| 173 | public function __toString() |
||
| 177 | |||
| 178 | public function explain($allow = true) |
||
| 183 | |||
| 184 | public function allowDiskUse($allow = true) |
||
| 189 | |||
| 190 | public function setBatchSize($batchSize) |
||
| 195 | |||
| 196 | public function getOptions() |
||
| 200 | } |
||
| 201 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.