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 |
||
16 | View Code Duplication | abstract class AbstractOutput extends Stage |
|
|
|||
17 | { |
||
18 | /** |
||
19 | * @var Stage\AbstractBucket |
||
20 | */ |
||
21 | protected $bucket; |
||
22 | |||
23 | /** |
||
24 | * @var Expr |
||
25 | */ |
||
26 | private $expr; |
||
27 | |||
28 | /** |
||
29 | * @param Builder $builder |
||
30 | * @param Stage\AbstractBucket $bucket |
||
31 | */ |
||
32 | 6 | public function __construct(Builder $builder, Stage\AbstractBucket $bucket) |
|
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | 6 | public function getExpression() |
|
47 | |||
48 | /** |
||
49 | * Returns an array of all unique values that results from applying an |
||
50 | * expression to each document in a group of documents that share the same |
||
51 | * group by key. Order of the elements in the output array is unspecified. |
||
52 | * |
||
53 | * AddToSet is an accumulator operation only available in the group stage. |
||
54 | * |
||
55 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/addToSet/ |
||
56 | * @see Expr::addToSet |
||
57 | * |
||
58 | * @param mixed|Expr $expression |
||
59 | * |
||
60 | * @return $this |
||
61 | */ |
||
62 | public function addToSet($expression) |
||
68 | |||
69 | /** |
||
70 | * Returns the average value of the numeric values that result from applying |
||
71 | * a specified expression to each document in a group of documents that |
||
72 | * share the same group by key. Ignores nun-numeric values. |
||
73 | * |
||
74 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/avg/ |
||
75 | * @see Expr::avg |
||
76 | * |
||
77 | * @param mixed|Expr $expression |
||
78 | * |
||
79 | * @return $this |
||
80 | */ |
||
81 | 6 | public function avg($expression) |
|
87 | |||
88 | /** |
||
89 | * Used to use an expression as field value. Can be any expression. |
||
90 | * |
||
91 | * @see http://docs.mongodb.org/manual/meta/aggregation-quick-reference/#aggregation-expressions |
||
92 | * @see Expr::expression |
||
93 | * |
||
94 | * @param mixed|Expr $value |
||
95 | * |
||
96 | * @return $this |
||
97 | */ |
||
98 | public function expression($value) |
||
104 | |||
105 | /** |
||
106 | * Set the current field for building the expression. |
||
107 | * |
||
108 | * @see Expr::field |
||
109 | * |
||
110 | * @param string $fieldName |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | 6 | public function field($fieldName) |
|
120 | |||
121 | /** |
||
122 | * Returns the value that results from applying an expression to the first |
||
123 | * document in a group of documents that share the same group by key. Only |
||
124 | * meaningful when documents are in a defined order. |
||
125 | * |
||
126 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/first/ |
||
127 | * @see Expr::first |
||
128 | * |
||
129 | * @param mixed|Expr $expression |
||
130 | * |
||
131 | * @return $this |
||
132 | */ |
||
133 | public function first($expression) |
||
139 | |||
140 | /** |
||
141 | * Returns the value that results from applying an expression to the last |
||
142 | * document in a group of documents that share the same group by a field. |
||
143 | * Only meaningful when documents are in a defined order. |
||
144 | * |
||
145 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/last/ |
||
146 | * @see Expr::last |
||
147 | * |
||
148 | * @param mixed|Expr $expression |
||
149 | * |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function last($expression) |
||
158 | |||
159 | /** |
||
160 | * Returns the highest value that results from applying an expression to |
||
161 | * each document in a group of documents that share the same group by key. |
||
162 | * |
||
163 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/max/ |
||
164 | * @see Expr::max |
||
165 | * |
||
166 | * @param mixed|Expr $expression |
||
167 | * |
||
168 | * @return $this |
||
169 | */ |
||
170 | public function max($expression) |
||
176 | |||
177 | /** |
||
178 | * Returns the lowest value that results from applying an expression to each |
||
179 | * document in a group of documents that share the same group by key. |
||
180 | * |
||
181 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/min/ |
||
182 | * @see Expr::min |
||
183 | * |
||
184 | * @param mixed|Expr $expression |
||
185 | * |
||
186 | * @return $this |
||
187 | */ |
||
188 | public function min($expression) |
||
194 | |||
195 | /** |
||
196 | * Returns an array of all values that result from applying an expression to |
||
197 | * each document in a group of documents that share the same group by key. |
||
198 | * |
||
199 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/push/ |
||
200 | * @see Expr::push |
||
201 | * |
||
202 | * @param mixed|Expr $expression |
||
203 | * |
||
204 | * @return $this |
||
205 | */ |
||
206 | public function push($expression) |
||
212 | |||
213 | /** |
||
214 | * Calculates the population standard deviation of the input values. |
||
215 | * |
||
216 | * The argument can be any expression as long as it resolves to an array. |
||
217 | * |
||
218 | * @see https://docs.mongodb.org/manual/reference/operator/aggregation/stdDevPop/ |
||
219 | * @see Expr::stdDevPop |
||
220 | * |
||
221 | * @param mixed|Expr $expression |
||
222 | * |
||
223 | * @return $this |
||
224 | */ |
||
225 | public function stdDevPop($expression) |
||
231 | |||
232 | /** |
||
233 | * Calculates the sample standard deviation of the input values. |
||
234 | * |
||
235 | * The argument can be any expression as long as it resolves to an array. |
||
236 | * |
||
237 | * @see https://docs.mongodb.org/manual/reference/operator/aggregation/stdDevSamp/ |
||
238 | * @see Expr::stdDevSamp |
||
239 | * |
||
240 | * @param mixed|Expr $expression |
||
241 | * |
||
242 | * @return $this |
||
243 | */ |
||
244 | public function stdDevSamp($expression) |
||
250 | |||
251 | /** |
||
252 | * Calculates and returns the sum of all the numeric values that result from |
||
253 | * applying a specified expression to each document in a group of documents |
||
254 | * that share the same group by key. Ignores nun-numeric values. |
||
255 | * |
||
256 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/sum/ |
||
257 | * @see Expr::sum |
||
258 | * |
||
259 | * @param mixed|Expr $expression |
||
260 | * |
||
261 | * @return $this |
||
262 | */ |
||
263 | public function sum($expression) |
||
269 | } |
||
270 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.