Completed
Push — master ( 1ec641...92e389 )
by Maciej
63:17 queued 60:26
created

Expr::abs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Doctrine\ODM\MongoDB\Aggregation;
4
5
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
6
use Doctrine\ODM\MongoDB\DocumentManager;
7
use Doctrine\ODM\MongoDB\Types\Type;
8
9
/**
10
 * Fluent interface for building aggregation pipelines.
11
 */
12
class Expr
13
{
14
    /**
15
     * @var DocumentManager
16
     */
17
    private $dm;
18
19
    /**
20
     * @var ClassMetadata
21
     */
22
    private $class;
23
24
    /**
25
     * @var array
26
     */
27
    private $expr = [];
28
29
    /**
30
     * The current field we are operating on.
31
     *
32
     * @var string
33
     */
34
    private $currentField;
35
36
    /**
37
     * @var array
38
     */
39
    private $switchBranch;
40
41
    /**
42
     * @inheritDoc
43
     */
44 367
    public function __construct(DocumentManager $dm, ClassMetadata $class)
45
    {
46 367
        $this->dm = $dm;
47 367
        $this->class = $class;
48 367
    }
49
50
    /**
51
     * Returns the absolute value of a number.
52
     *
53
     * The <number> argument can be any valid expression as long as it resolves
54
     * to a number.
55
     *
56
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/abs/
57
     * @param mixed|self $number
58
     * @return $this
59
     *
60
     * @since 1.3
61
     */
62 3
    public function abs($number)
63
    {
64 3
        return $this->operator('$abs', $number);
65
    }
66
67
    /**
68
     * Adds numbers together or adds numbers and a date. If one of the arguments
69
     * is a date, $add treats the other arguments as milliseconds to add to the
70
     * date.
71
     *
72
     * The arguments can be any valid expression as long as they resolve to
73
     * either all numbers or to numbers and a date.
74
     *
75
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/add/
76
     * @param mixed|self $expression1
77
     * @param mixed|self $expression2
78
     * @param mixed|self $expression3,... Additional expressions
0 ignored issues
show
Bug introduced by
There is no parameter named $expression3,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
79
     * @return $this
80
     */
81 15
    public function add($expression1, $expression2 /* , $expression3, ... */)
0 ignored issues
show
Unused Code introduced by
The parameter $expression1 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $expression2 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83 15
        return $this->operator('$add', func_get_args());
84
    }
85
86
    /**
87
     * Adds one or more $and clauses to the current expression.
88
     *
89
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/and/
90
     * @param array|self $expression
91
     * @return $this
92
     */
93 1 View Code Duplication
    public function addAnd($expression /*, $expression2, ... */)
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
94
    {
95 1
        if (! isset($this->expr['$and'])) {
96 1
            $this->expr['$and'] = [];
97
        }
98
99 1
        $this->expr['$and'] = array_merge($this->expr['$and'], array_map([$this, 'ensureArray'], func_get_args()));
100
101 1
        return $this;
102
    }
103
104
    /**
105
     * Adds one or more $or clause to the current expression.
106
     *
107
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/or/
108
     * @param array|self $expression
109
     * @return $this
110
     */
111 View Code Duplication
    public function addOr($expression /*, $expression2, ... */)
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
112
    {
113
        if (! isset($this->expr['$or'])) {
114
            $this->expr['$or'] = [];
115
        }
116
117
        $this->expr['$or'] = array_merge($this->expr['$or'], array_map([$this, 'ensureArray'], func_get_args()));
118
119
        return $this;
120
    }
121
122
    /**
123
     * Returns an array of all unique values that results from applying an
124
     * expression to each document in a group of documents that share the same
125
     * group by key. Order of the elements in the output array is unspecified.
126
     *
127
     * AddToSet is an accumulator operation only available in the group stage.
128
     *
129
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/addToSet/
130
     * @param mixed|self $expression
131
     * @return $this
132
     */
133 2
    public function addToSet($expression)
134
    {
135 2
        return $this->operator('$addToSet', $expression);
136
    }
137
138
    /**
139
     * Evaluates an array as a set and returns true if no element in the array
140
     * is false. Otherwise, returns false. An empty array returns true.
141
     *
142
     * The expression must resolve to an array.
143
     *
144
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/allElementsTrue/
145
     * @param mixed|self $expression
146
     * @return $this
147
     */
148 3
    public function allElementsTrue($expression)
149
    {
150 3
        return $this->operator('$allElementsTrue', $expression);
151
    }
152
153
    /**
154
     * Evaluates an array as a set and returns true if any of the elements are
155
     * true and false otherwise. An empty array returns false.
156
     *
157
     * The expression must resolve to an array.
158
     *
159
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/anyElementTrue/
160
     * @param array|self $expression
161
     * @return $this
162
     */
163 3
    public function anyElementTrue($expression)
164
    {
165 3
        return $this->operator('$anyElementTrue', $expression);
166
    }
167
168
    /**
169
     * Returns the element at the specified array index.
170
     *
171
     * The <array> expression can be any valid expression as long as it resolves
172
     * to an array.
173
     * The <idx> expression can be any valid expression as long as it resolves
174
     * to an integer.
175
     *
176
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/arrayElemAt/
177
     * @param mixed|self $array
178
     * @param mixed|self $index
179
     * @return $this
180
     *
181
     * @since 1.3
182
     */
183 3
    public function arrayElemAt($array, $index)
184
    {
185 3
        return $this->operator('$arrayElemAt', [$array, $index]);
186
    }
187
188
    /**
189
     * Returns the average value of the numeric values that result from applying
190
     * a specified expression to each document in a group of documents that
191
     * share the same group by key. Ignores nun-numeric values.
192
     *
193
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/avg/
194
     * @param mixed|self $expression
195
     * @return $this
196
     */
197 10
    public function avg($expression)
198
    {
199 10
        return $this->operator('$avg', $expression);
200
    }
201
202
    /**
203
     * Adds a case statement for a branch of the $switch operator.
204
     *
205
     * Requires {@link switch()} to be called first. The argument can be any
206
     * valid expression that resolves to a boolean. If the result is not a
207
     * boolean, it is coerced to a boolean value.
208
     *
209
     * @param mixed|self $expression
210
     *
211
     * @return $this
212
     */
213 6
    public function case($expression)
214
    {
215 6
        $this->requiresSwitchStatement(static::class . '::case');
216
217 4
        $this->switchBranch = ['case' => $expression];
218
219 4
        return $this;
220
    }
221
222
    /**
223
     * Returns the smallest integer greater than or equal to the specified number.
224
     *
225
     * The <number> expression can be any valid expression as long as it
226
     * resolves to a number.
227
     *
228
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/ceil/
229
     * @param mixed|self $number
230
     * @return $this
231
     *
232
     * @since 1.3
233
     */
234 3
    public function ceil($number)
235
    {
236 3
        return $this->operator('$ceil', $number);
237
    }
238
239
    /**
240
     * Compares two values and returns:
241
     * -1 if the first value is less than the second.
242
     * 1 if the first value is greater than the second.
243
     * 0 if the two values are equivalent.
244
     *
245
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/cmp/
246
     * @param mixed|self $expression1
247
     * @param mixed|self $expression2
248
     * @return $this
249
     */
250 3
    public function cmp($expression1, $expression2)
251
    {
252 3
        return $this->operator('$cmp', [$expression1, $expression2]);
253
    }
254
255
    /**
256
     * Concatenates strings and returns the concatenated string.
257
     *
258
     * The arguments can be any valid expression as long as they resolve to
259
     * strings. If the argument resolves to a value of null or refers to a field
260
     * that is missing, $concat returns null.
261
     *
262
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/concat/
263
     * @param mixed|self $expression1
264
     * @param mixed|self $expression2
265
     * @param mixed|self $expression3,... Additional expressions
0 ignored issues
show
Bug introduced by
There is no parameter named $expression3,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
266
     * @return $this
267
     */
268 9
    public function concat($expression1, $expression2 /* , $expression3, ... */)
0 ignored issues
show
Unused Code introduced by
The parameter $expression1 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $expression2 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
269
    {
270 9
        return $this->operator('$concat', func_get_args());
271
    }
272
273
    /**
274
     * Concatenates arrays to return the concatenated array.
275
     *
276
     * The <array> expressions can be any valid expression as long as they
277
     * resolve to an array.
278
     *
279
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/concatArrays/
280
     * @param mixed|self $array1
281
     * @param mixed|self $array2
282
     * @param mixed|self $array3, ... Additional expressions
0 ignored issues
show
Bug introduced by
There is no parameter named $array3,. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
283
     * @return $this
284
     *
285
     * @since 1.3
286
     */
287 6
    public function concatArrays($array1, $array2 /* , $array3, ... */)
0 ignored issues
show
Unused Code introduced by
The parameter $array1 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $array2 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
288
    {
289 6
        return $this->operator('$concatArrays', func_get_args());
290
    }
291
292
    /**
293
     * Evaluates a boolean expression to return one of the two specified return
294
     * expressions.
295
     *
296
     * The arguments can be any valid expression.
297
     *
298
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/cond/
299
     * @param mixed|self $if
300
     * @param mixed|self $then
301
     * @param mixed|self $else
302
     * @return $this
303
     */
304 10
    public function cond($if, $then, $else)
305
    {
306 10
        return $this->operator('$cond', ['if' => $if, 'then' => $then, 'else' => $else]);
307
    }
308
309
    /**
310
     * Converts an expression object into an array, recursing into nested items
311
     *
312
     * For expression objects, it calls getExpression on the expression object.
313
     * For arrays, it recursively calls itself for each array item. Other values
314
     * are returned directly.
315
     *
316
     * @param mixed|self $expression
317
     * @return string|array
318
     * @internal
319
     */
320 1
    public static function convertExpression($expression)
321
    {
322 1
        if (is_array($expression)) {
323
            return array_map(['static', 'convertExpression'], $expression);
324 1
        } elseif ($expression instanceof self) {
325 1
            return $expression->getExpression();
326
        }
327
328
        return $expression;
329
    }
330
331
    /**
332
     * Converts a date object to a string according to a user-specified format.
333
     *
334
     * The format string can be any string literal, containing 0 or more format
335
     * specifiers.
336
     * The date argument can be any expression as long as it resolves to a date.
337
     *
338
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/dateToString/
339
     * @param string $format
340
     * @param mixed|self $expression
341
     * @return $this
342
     */
343 3
    public function dateToString($format, $expression)
344
    {
345 3
        return $this->operator('$dateToString', ['format' => $format, 'date' => $expression]);
346
    }
347
348
    /**
349
     * Returns the day of the month for a date as a number between 1 and 31.
350
     *
351
     * The argument can be any expression as long as it resolves to a date.
352
     *
353
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/dayOfMonth/
354
     * @param mixed|self $expression
355
     * @return $this
356
     */
357 7
    public function dayOfMonth($expression)
358
    {
359 7
        return $this->operator('$dayOfMonth', $expression);
360
    }
361
362
    /**
363
     * Returns the day of the week for a date as a number between 1 (Sunday) and
364
     * 7 (Saturday).
365
     *
366
     * The argument can be any expression as long as it resolves to a date.
367
     *
368
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/dayOfWeek/
369
     * @param mixed|self $expression
370
     * @return $this
371
     */
372 7
    public function dayOfWeek($expression)
373
    {
374 7
        return $this->operator('$dayOfWeek', $expression);
375
    }
376
377
    /**
378
     * Returns the day of the year for a date as a number between 1 and 366.
379
     *
380
     * The argument can be any expression as long as it resolves to a date.
381
     *
382
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/dayOfYear/
383
     * @param mixed|self $expression
384
     * @return $this
385
     */
386 3
    public function dayOfYear($expression)
387
    {
388 3
        return $this->operator('$dayOfYear', $expression);
389
    }
390
391
    /**
392
     * Adds a default statement for the current $switch operator.
393
     *
394
     * Requires {@link switch()} to be called first. The argument can be any
395
     * valid expression.
396
     *
397
     * Note: if no default is specified and no branch evaluates to true, the
398
     * $switch operator throws an error.
399
     *
400
     * @param mixed|self $expression
401
     *
402
     * @return $this
403
     */
404 4
    public function default($expression)
405
    {
406 4
        $this->requiresSwitchStatement(static::class . '::default');
407
408 2
        if ($this->currentField) {
409
            $this->expr[$this->currentField]['$switch']['default'] = $this->ensureArray($expression);
410
        } else {
411 2
            $this->expr['$switch']['default'] = $this->ensureArray($expression);
412
        }
413
414 2
        return $this;
415
    }
416
417
    /**
418
     * Divides one number by another and returns the result. The first argument
419
     * is divided by the second argument.
420
     *
421
     * The arguments can be any valid expression as long as the resolve to numbers.
422
     *
423
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/divide/
424
     * @param mixed|self $expression1
425
     * @param mixed|self $expression2
426
     * @return $this
427
     */
428 3
    public function divide($expression1, $expression2)
429
    {
430 3
        return $this->operator('$divide', [$expression1, $expression2]);
431
    }
432
433
    /**
434
     * Compares two values and returns whether the are equivalent.
435
     *
436
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/eq/
437
     * @param mixed|self $expression1
438
     * @param mixed|self $expression2
439
     * @return $this
440
     */
441 9
    public function eq($expression1, $expression2)
442
    {
443 9
        return $this->operator('$eq', [$expression1, $expression2]);
444
    }
445
446
    /**
447
     * Raises Euler’s number to the specified exponent and returns the result.
448
     *
449
     * The <exponent> expression can be any valid expression as long as it
450
     * resolves to a number.
451
     *
452
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/exp/
453
     * @param mixed|self $exponent
454
     * @return $this
455
     *
456
     * @since 1.3
457
     */
458 3
    public function exp($exponent)
459
    {
460 3
        return $this->operator('$exp', $exponent);
461
    }
462
463
    /**
464
     * Returns a new expression object
465
     *
466
     * @return static
467
     *
468
     * @since 1.3
469
     */
470 4
    public function expr()
471
    {
472 4
        return new static($this->dm, $this->class);
473
    }
474
475
    /**
476
     * Allows any expression to be used as a field value.
477
     *
478
     * @see http://docs.mongodb.org/manual/meta/aggregation-quick-reference/#aggregation-expressions
479
     * @param mixed|self $value
480
     * @return $this
481
     */
482 12
    public function expression($value)
483
    {
484 12
        $this->requiresCurrentField(__METHOD__);
485 11
        $this->expr[$this->currentField] = $this->ensureArray($value);
486
487 11
        return $this;
488
    }
489
490
    /**
491
     * Set the current field for building the expression.
492
     *
493
     * @param string $fieldName
494
     * @return $this
495
     */
496 139
    public function field($fieldName)
497
    {
498 139
        $fieldName = $this->getDocumentPersister()->prepareFieldName($fieldName);
499 139
        $this->currentField = (string) $fieldName;
500
501 139
        return $this;
502
    }
503
504
    /**
505
     * Selects a subset of the array to return based on the specified condition.
506
     *
507
     * Returns an array with only those elements that match the condition. The
508
     * returned elements are in the original order.
509
     *
510
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/filter/
511
     * @param mixed|self $input
512
     * @param mixed|self $as
513
     * @param mixed|self $cond
514
     * @return $this
515
     *
516
     * @since 1.3
517
     */
518 3
    public function filter($input, $as, $cond)
519
    {
520 3
        return $this->operator('$filter', ['input' => $input, 'as' => $as, 'cond' => $cond]);
521
    }
522
523
    /**
524
     * Returns the value that results from applying an expression to the first
525
     * document in a group of documents that share the same group by key. Only
526
     * meaningful when documents are in a defined order.
527
     *
528
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/first/
529
     * @param mixed|self $expression
530
     * @return $this
531
     */
532 2
    public function first($expression)
533
    {
534 2
        return $this->operator('$first', $expression);
535
    }
536
537
    /**
538
     * Returns the largest integer less than or equal to the specified number.
539
     *
540
     * The <number> expression can be any valid expression as long as it
541
     * resolves to a number.
542
     *
543
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/floor/
544
     * @param mixed|self $number
545
     * @return $this
546
     *
547
     * @since 1.3
548
     */
549 3
    public function floor($number)
550
    {
551 3
        return $this->operator('$floor', $number);
552
    }
553
554
    /**
555
     * @return array
556
     */
557 339
    public function getExpression()
558
    {
559 339
        return $this->expr;
560
    }
561
562
    /**
563
     * Compares two values and returns:
564
     * true when the first value is greater than the second value.
565
     * false when the first value is less than or equivalent to the second
566
     * value.
567
     *
568
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/gt/
569
     * @param mixed|self $expression1
570
     * @param mixed|self $expression2
571
     * @return $this
572
     */
573 3
    public function gt($expression1, $expression2)
574
    {
575 3
        return $this->operator('$gt', [$expression1, $expression2]);
576
    }
577
578
    /**
579
     * Compares two values and returns:
580
     * true when the first value is greater than or equivalent to the second
581
     * value.
582
     * false when the first value is less than the second value.
583
     *
584
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/gte/
585
     * @param mixed|self $expression1
586
     * @param mixed|self $expression2
587
     * @return $this
588
     */
589 6
    public function gte($expression1, $expression2)
590
    {
591 6
        return $this->operator('$gte', [$expression1, $expression2]);
592
    }
593
594
    /**
595
     * Returns the hour portion of a date as a number between 0 and 23.
596
     *
597
     * The argument can be any expression as long as it resolves to a date.
598
     *
599
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/hour/
600
     * @param mixed|self $expression
601
     * @return $this
602
     */
603 3
    public function hour($expression)
604
    {
605 3
        return $this->operator('$hour', $expression);
606
    }
607
608
    /**
609
     * Evaluates an expression and returns the value of the expression if the
610
     * expression evaluates to a non-null value. If the expression evaluates to
611
     * a null value, including instances of undefined values or missing fields,
612
     * returns the value of the replacement expression.
613
     *
614
     * The arguments can be any valid expression.
615
     *
616
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/ifNull/
617
     * @param mixed|self $expression
618
     * @param mixed|self $replacementExpression
619
     * @return $this
620
     */
621 3
    public function ifNull($expression, $replacementExpression)
622
    {
623 3
        return $this->operator('$ifNull', [$expression, $replacementExpression]);
624
    }
625
626
    /**
627
     * Returns a boolean indicating whether a specified value is in an array.
628
     *
629
     * Unlike the $in query operator, the aggregation $in operator does not
630
     * support matching by regular expressions.
631
     *
632
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/in/
633
     * @since 1.5
634
     * @param mixed|self $expression
635
     * @param mixed|self $arrayExpression
636
     * @return $this
637
     */
638 3
    public function in($expression, $arrayExpression)
639
    {
640 3
        return $this->operator('$in', [$expression, $arrayExpression]);
641
    }
642
643
    /**
644
     * Searches an array for an occurence of a specified value and returns the
645
     * array index (zero-based) of the first occurence. If the value is not
646
     * found, returns -1.
647
     *
648
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/indexOfArray/
649
     * @since 1.5
650
     * @param mixed|self $arrayExpression Can be any valid expression as long as it resolves to an array.
651
     * @param mixed|self $searchExpression Can be any valid expression.
652
     * @param mixed|self $start Optional. An integer, or a number that can be represented as integers (such as 2.0), that specifies the starting index position for the search. Can be any valid expression that resolves to a non-negative integral number.
653
     * @param mixed|self $end An integer, or a number that can be represented as integers (such as 2.0), that specifies the ending index position for the search. Can be any valid expression that resolves to a non-negative integral number.
654
     * @return $this
655
     */
656 12 View Code Duplication
    public function indexOfArray($arrayExpression, $searchExpression, $start = null, $end = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
657
    {
658 12
        $args = [$arrayExpression, $searchExpression];
659 12
        if ($start !== null) {
660 6
            $args[] = $start;
661
662 6
            if ($end !== null) {
663 3
                $args[] = $end;
664
            }
665
        }
666
667 12
        return $this->operator('$indexOfArray', $args);
668
    }
669
670
    /**
671
     * Searches a string for an occurence of a substring and returns the UTF-8
672
     * byte index (zero-based) of the first occurence. If the substring is not
673
     * found, returns -1.
674
     *
675
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/indexOfBytes/
676
     * @since 1.5
677
     * @param mixed|self $stringExpression Can be any valid expression as long as it resolves to a string.
678
     * @param mixed|self $substringExpression Can be any valid expression as long as it resolves to a string.
679
     * @param int|null $start An integral number that specifies the starting index position for the search. Can be any valid expression that resolves to a non-negative integral number.
680
     * @param int|null $end An integral number that specifies the ending index position for the search. Can be any valid expression that resolves to a non-negative integral number.
681
     *
682
     * @return $this
683
     */
684 12 View Code Duplication
    public function indexOfBytes($stringExpression, $substringExpression, $start = null, $end = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
685
    {
686 12
        $args = [$stringExpression, $substringExpression];
687 12
        if ($start !== null) {
688 6
            $args[] = $start;
689
690 6
            if ($end !== null) {
691 3
                $args[] = $end;
692
            }
693
        }
694
695 12
        return $this->operator('$indexOfBytes', $args);
696
    }
697
698
    /**
699
     * Searches a string for an occurence of a substring and returns the UTF-8
700
     * code point index (zero-based) of the first occurence. If the substring is
701
     * not found, returns -1.
702
     *
703
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/indexOfCP/
704
     * @since 1.5
705
     * @param mixed|self $stringExpression Can be any valid expression as long as it resolves to a string.
706
     * @param mixed|self $substringExpression Can be any valid expression as long as it resolves to a string.
707
     * @param int|null $start An integral number that specifies the starting index position for the search. Can be any valid expression that resolves to a non-negative integral number.
708
     * @param int|null $end An integral number that specifies the ending index position for the search. Can be any valid expression that resolves to a non-negative integral number.
709
     *
710
     * @return $this
711
     */
712 12 View Code Duplication
    public function indexOfCP($stringExpression, $substringExpression, $start = null, $end = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
713
    {
714 12
        $args = [$stringExpression, $substringExpression];
715 12
        if ($start !== null) {
716 6
            $args[] = $start;
717
718 6
            if ($end !== null) {
719 3
                $args[] = $end;
720
            }
721
        }
722
723 12
        return $this->operator('$indexOfCP', $args);
724
    }
725
726
    /**
727
     * Determines if the operand is an array. Returns a boolean.
728
     *
729
     * The <expression> can be any valid expression.
730
     *
731
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/isArray/
732
     * @param mixed|self $expression
733
     * @return $this
734
     *
735
     * @since 1.3
736
     */
737 3
    public function isArray($expression)
738
    {
739 3
        return $this->operator('$isArray', $expression);
740
    }
741
742
    /**
743
     * Returns the weekday number in ISO 8601 format, ranging from 1 (for Monday)
744
     * to 7 (for Sunday).
745
     *
746
     * The argument can be any expression as long as it resolves to a date.
747
     *
748
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/isoDayOfWeek/
749
     * @since 1.5
750
     * @param mixed|self $expression
751
     * @return $this
752
     */
753 3
    public function isoDayOfWeek($expression)
754
    {
755 3
        return $this->operator('$isoDayOfWeek', $expression);
756
    }
757
758
    /**
759
     * Returns the week number in ISO 8601 format, ranging from 1 to 53.
760
     *
761
     * Week numbers start at 1 with the week (Monday through Sunday) that
762
     * contains the year’s first Thursday.
763
     *
764
     * The argument can be any expression as long as it resolves to a date.
765
     *
766
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/isoWeek/
767
     * @since 1.5
768
     * @param mixed|self $expression
769
     * @return $this
770
     */
771 3
    public function isoWeek($expression)
772
    {
773 3
        return $this->operator('$isoWeek', $expression);
774
    }
775
776
    /**
777
     * Returns the year number in ISO 8601 format.
778
     *
779
     * The year starts with the Monday of week 1 (ISO 8601) and ends with the
780
     * Sunday of the last week (ISO 8601).
781
     *
782
     * The argument can be any expression as long as it resolves to a date.
783
     *
784
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/isoWeek/
785
     * @since 1.5
786
     * @param mixed|self $expression
787
     * @return $this
788
     */
789 3
    public function isoWeekYear($expression)
790
    {
791 3
        return $this->operator('$isoWeekYear', $expression);
792
    }
793
794
    /**
795
     * Returns the value that results from applying an expression to the last
796
     * document in a group of documents that share the same group by a field.
797
     * Only meaningful when documents are in a defined order.
798
     *
799
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/last/
800
     * @param mixed|self $expression
801
     * @return $this
802
     */
803 2
    public function last($expression)
804
    {
805 2
        return $this->operator('$last', $expression);
806
    }
807
808
    /**
809
     * Binds variables for use in the specified expression, and returns the
810
     * result of the expression.
811
     *
812
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/let/
813
     * @param mixed|self $vars Assignment block for the variables accessible in the in expression. To assign a variable, specify a string for the variable name and assign a valid expression for the value.
814
     * @param mixed|self $in   The expression to evaluate.
815
     * @return $this
816
     */
817 3
    public function let($vars, $in)
818
    {
819 3
        return $this->operator('$let', ['vars' => $vars, 'in' => $in]);
820
    }
821
822
    /**
823
     * Returns a value without parsing. Use for values that the aggregation
824
     * pipeline may interpret as an expression.
825
     *
826
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/literal/
827
     * @param mixed|self $value
828
     * @return $this
829
     */
830 3
    public function literal($value)
831
    {
832 3
        return $this->operator('$literal', $value);
833
    }
834
835
    /**
836
     * Calculates the natural logarithm ln (i.e loge) of a number and returns
837
     * the result as a double.
838
     *
839
     * The <number> expression can be any valid expression as long as it
840
     * resolves to a non-negative number.
841
     *
842
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/log/
843
     * @param mixed|self $number
844
     * @return $this
845
     *
846
     * @since 1.3
847
     */
848 3
    public function ln($number)
849
    {
850 3
        return $this->operator('$ln', $number);
851
    }
852
853
    /**
854
     * Calculates the log of a number in the specified base and returns the
855
     * result as a double.
856
     *
857
     * The <number> expression can be any valid expression as long as it
858
     * resolves to a non-negative number.
859
     * The <base> expression can be any valid expression as long as it resolves
860
     * to a positive number greater than 1.
861
     *
862
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/log/
863
     * @param mixed|self $number
864
     * @param mixed|self $base
865
     * @return $this
866
     *
867
     * @since 1.3
868
     */
869 3
    public function log($number, $base)
870
    {
871 3
        return $this->operator('$log', [$number, $base]);
872
    }
873
874
    /**
875
     * Calculates the log base 10 of a number and returns the result as a double.
876
     *
877
     * The <number> expression can be any valid expression as long as it
878
     * resolves to a non-negative number.
879
     *
880
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/log10/
881
     * @param mixed|self $number
882
     * @return $this
883
     *
884
     * @since 1.3
885
     */
886 3
    public function log10($number)
887
    {
888 3
        return $this->operator('$log10', $number);
889
    }
890
891
    /**
892
     * Compares two values and returns:
893
     * true when the first value is less than the second value.
894
     * false when the first value is greater than or equivalent to the second
895
     * value.
896
     *
897
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/lt/
898
     * @param mixed|self $expression1
899
     * @param mixed|self $expression2
900
     * @return $this
901
     */
902 4
    public function lt($expression1, $expression2)
903
    {
904 4
        return $this->operator('$lt', [$expression1, $expression2]);
905
    }
906
907
    /**
908
     * Compares two values and returns:
909
     * true when the first value is less than or equivalent to the second value.
910
     * false when the first value is greater than the second value.
911
     *
912
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/lte/
913
     * @param mixed|self $expression1
914
     * @param mixed|self $expression2
915
     * @return $this
916
     */
917 6
    public function lte($expression1, $expression2)
918
    {
919 6
        return $this->operator('$lte', [$expression1, $expression2]);
920
    }
921
922
    /**
923
     * Applies an expression to each item in an array and returns an array with
924
     * the applied results.
925
     *
926
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/map/
927
     * @param mixed|self $input An expression that resolves to an array.
928
     * @param string $as        The variable name for the items in the input array. The in expression accesses each item in the input array by this variable.
929
     * @param mixed|self $in    The expression to apply to each item in the input array. The expression accesses the item by its variable name.
930
     * @return $this
931
     */
932 3
    public function map($input, $as, $in)
933
    {
934 3
        return $this->operator('$map', ['input' => $input, 'as' => $as, 'in' => $in]);
935
    }
936
937
    /**
938
     * Returns the highest value that results from applying an expression to
939
     * each document in a group of documents that share the same group by key.
940
     *
941
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/max/
942
     * @param mixed|self $expression
943
     * @return $this
944
     */
945 3
    public function max($expression)
946
    {
947 3
        return $this->operator('$max', $expression);
948
    }
949
950
    /**
951
     * Returns the metadata associated with a document in a pipeline operations.
952
     *
953
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/meta/
954
     * @param $metaDataKeyword
955
     * @return $this
956
     */
957 3
    public function meta($metaDataKeyword)
958
    {
959 3
        return $this->operator('$meta', $metaDataKeyword);
960
    }
961
962
    /**
963
     * Returns the millisecond portion of a date as an integer between 0 and 999.
964
     *
965
     * The argument can be any expression as long as it resolves to a date.
966
     *
967
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/millisecond/
968
     * @param mixed|self $expression
969
     * @return $this
970
     */
971 3
    public function millisecond($expression)
972
    {
973 3
        return $this->operator('$millisecond', $expression);
974
    }
975
976
    /**
977
     * Returns the lowest value that results from applying an expression to each
978
     * document in a group of documents that share the same group by key.
979
     *
980
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/min/
981
     * @param mixed|self $expression
982
     * @return $this
983
     */
984 3
    public function min($expression)
985
    {
986 3
        return $this->operator('$min', $expression);
987
    }
988
989
    /**
990
     * Returns the minute portion of a date as a number between 0 and 59.
991
     *
992
     * The argument can be any expression as long as it resolves to a date.
993
     *
994
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/minute/
995
     * @param mixed|self $expression
996
     * @return $this
997
     */
998 3
    public function minute($expression)
999
    {
1000 3
        return $this->operator('$minute', $expression);
1001
    }
1002
1003
    /**
1004
     * Divides one number by another and returns the remainder. The first
1005
     * argument is divided by the second argument.
1006
     *
1007
     * The arguments can be any valid expression as long as they resolve to numbers.
1008
     *
1009
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/mod/
1010
     * @param mixed|self $expression1
1011
     * @param mixed|self $expression2
1012
     * @return $this
1013
     */
1014 3
    public function mod($expression1, $expression2)
1015
    {
1016 3
        return $this->operator('$mod', [$expression1, $expression2]);
1017
    }
1018
1019
    /**
1020
     * Returns the month of a date as a number between 1 and 12.
1021
     *
1022
     * The argument can be any expression as long as it resolves to a date.
1023
     *
1024
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/month/
1025
     * @param mixed|self $expression
1026
     * @return $this
1027
     */
1028 3
    public function month($expression)
1029
    {
1030 3
        return $this->operator('$month', $expression);
1031
    }
1032
1033
    /**
1034
     * Multiplies numbers together and returns the result.
1035
     *
1036
     * The arguments can be any valid expression as long as they resolve to numbers.
1037
     *
1038
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/multiply/
1039
     * @param mixed|self $expression1
1040
     * @param mixed|self $expression2
1041
     * @param mixed|self $expression3,... Additional expressions
0 ignored issues
show
Bug introduced by
There is no parameter named $expression3,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1042
     * @return $this
1043
     */
1044 13
    public function multiply($expression1, $expression2 /* , $expression3, ... */)
0 ignored issues
show
Unused Code introduced by
The parameter $expression1 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $expression2 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1045
    {
1046 13
        return $this->operator('$multiply', func_get_args());
1047
    }
1048
1049
    /**
1050
     * Compares two values and returns:
1051
     * true when the values are not equivalent.
1052
     * false when the values are equivalent.
1053
     *
1054
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/ne/
1055
     * @param mixed|self $expression1
1056
     * @param mixed|self $expression2
1057
     * @return $this
1058
     */
1059 4
    public function ne($expression1, $expression2)
1060
    {
1061 4
        return $this->operator('$ne', [$expression1, $expression2]);
1062
    }
1063
1064
    /**
1065
     * Evaluates a boolean and returns the opposite boolean value.
1066
     *
1067
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/not/
1068
     * @param mixed|self $expression
1069
     * @return $this
1070
     */
1071 3
    public function not($expression)
1072
    {
1073 3
        return $this->operator('$not', $expression);
1074
    }
1075
1076
    /**
1077
     * Raises a number to the specified exponent and returns the result.
1078
     *
1079
     * The <number> expression can be any valid expression as long as it
1080
     * resolves to a non-negative number.
1081
     * The <exponent> expression can be any valid expression as long as it
1082
     * resolves to a number.
1083
     *
1084
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/pow/
1085
     * @param mixed|self $number
1086
     * @param mixed|self $exponent
1087
     * @return $this
1088
     *
1089
     * @since 1.3
1090
     */
1091 3
    public function pow($number, $exponent)
1092
    {
1093 3
        return $this->operator('$pow', [$number, $exponent]);
1094
    }
1095
1096
    /**
1097
     * Returns an array of all values that result from applying an expression to
1098
     * each document in a group of documents that share the same group by key.
1099
     *
1100
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/push/
1101
     * @param mixed|self $expression
1102
     * @return $this
1103
     */
1104 2
    public function push($expression)
1105
    {
1106 2
        return $this->operator('$push', $expression);
1107
    }
1108
1109
    /**
1110
     * Returns an array whose elements are a generated sequence of numbers.
1111
     *
1112
     * $range generates the sequence from the specified starting number by successively incrementing the starting number by the specified step value up to but not including the end point.
1113
     *
1114
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/range/
1115
     * @since 1.5
1116
     * @param mixed|self $start An integer that specifies the start of the sequence. Can be any valid expression that resolves to an integer.
1117
     * @param mixed|self $end An integer that specifies the exclusive upper limit of the sequence. Can be any valid expression that resolves to an integer.
1118
     * @param mixed|self $step Optional. An integer that specifies the increment value. Can be any valid expression that resolves to a non-zero integer. Defaults to 1.
1119
     * @return $this
1120
     */
1121 6
    public function range($start, $end, $step = 1)
1122
    {
1123 6
        return $this->operator('$range', [$start, $end, $step]);
1124
    }
1125
1126
    /**
1127
     * Applies an expression to each element in an array and combines them into
1128
     * a single value.
1129
     *
1130
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/reduce/
1131
     * @since 1.5
1132
     * @param mixed|self $input Can be any valid expression that resolves to an array.
1133
     * @param mixed|self $initialValue The initial cumulative value set before in is applied to the first element of the input array.
1134
     * @param mixed|self $in A valid expression that $reduce applies to each element in the input array in left-to-right order. Wrap the input value with $reverseArray to yield the equivalent of applying the combining expression from right-to-left.
1135
     * @return $this
1136
     */
1137 3
    public function reduce($input, $initialValue, $in)
1138
    {
1139 3
        return $this->operator('$reduce', ['input' => $input, 'initialValue' => $initialValue, 'in' => $in]);
1140
    }
1141
1142
    /**
1143
     * Accepts an array expression as an argument and returns an array with the
1144
     * elements in reverse order.
1145
     *
1146
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/reverseArray/
1147
     * @since 1.5
1148
     * @param mixed|self $expression
1149
     * @return $this
1150
     */
1151 3
    public function reverseArray($expression)
1152
    {
1153 3
        return $this->operator('$reverseArray', $expression);
1154
    }
1155
1156
    /**
1157
     * Returns the second portion of a date as a number between 0 and 59, but
1158
     * can be 60 to account for leap seconds.
1159
     *
1160
     * The argument can be any expression as long as it resolves to a date.
1161
     *
1162
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/second/
1163
     * @param mixed|self $expression
1164
     * @return $this
1165
     */
1166 3
    public function second($expression)
1167
    {
1168 3
        return $this->operator('$second', $expression);
1169
    }
1170
1171
    /**
1172
     * Takes two sets and returns an array containing the elements that only
1173
     * exist in the first set.
1174
     *
1175
     * The arguments can be any valid expression as long as they each resolve to an array.
1176
     *
1177
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/setDifference/
1178
     * @param mixed|self $expression1
1179
     * @param mixed|self $expression2
1180
     * @return $this
1181
     */
1182 3
    public function setDifference($expression1, $expression2)
1183
    {
1184 3
        return $this->operator('$setDifference', [$expression1, $expression2]);
1185
    }
1186
1187
    /**
1188
     * Compares two or more arrays and returns true if they have the same
1189
     * distinct elements and false otherwise.
1190
     *
1191
     * The arguments can be any valid expression as long as they each resolve to an array.
1192
     *
1193
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/setEquals/
1194
     * @param mixed|self $expression1
1195
     * @param mixed|self $expression2
1196
     * @param mixed|self $expression3,...   Additional sets
0 ignored issues
show
Bug introduced by
There is no parameter named $expression3,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1197
     * @return $this
1198
     */
1199 6
    public function setEquals($expression1, $expression2 /* , $expression3, ... */)
0 ignored issues
show
Unused Code introduced by
The parameter $expression1 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $expression2 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1200
    {
1201 6
        return $this->operator('$setEquals', func_get_args());
1202
    }
1203
1204
    /**
1205
     * Takes two or more arrays and returns an array that contains the elements
1206
     * that appear in every input array.
1207
     *
1208
     * The arguments can be any valid expression as long as they each resolve to an array.
1209
     *
1210
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/setIntersection/
1211
     * @param mixed|self $expression1
1212
     * @param mixed|self $expression2
1213
     * @param mixed|self $expression3,...   Additional sets
0 ignored issues
show
Bug introduced by
There is no parameter named $expression3,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1214
     * @return $this
1215
     */
1216 6
    public function setIntersection($expression1, $expression2 /* , $expression3, ... */)
0 ignored issues
show
Unused Code introduced by
The parameter $expression1 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $expression2 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1217
    {
1218 6
        return $this->operator('$setIntersection', func_get_args());
1219
    }
1220
1221
    /**
1222
     * Takes two arrays and returns true when the first array is a subset of the
1223
     * second, including when the first array equals the second array, and false otherwise.
1224
     *
1225
     * The arguments can be any valid expression as long as they each resolve to an array.
1226
     *
1227
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/setIsSubset/
1228
     * @param mixed|self $expression1
1229
     * @param mixed|self $expression2
1230
     * @return $this
1231
     */
1232 3
    public function setIsSubset($expression1, $expression2)
1233
    {
1234 3
        return $this->operator('$setIsSubset', [$expression1, $expression2]);
1235
    }
1236
1237
    /**
1238
     * Takes two or more arrays and returns an array containing the elements
1239
     * that appear in any input array.
1240
     *
1241
     * The arguments can be any valid expression as long as they each resolve to an array.
1242
     *
1243
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/setUnion/
1244
     * @param mixed|self $expression1
1245
     * @param mixed|self $expression2
1246
     * @param mixed|self $expression3,...   Additional sets
0 ignored issues
show
Bug introduced by
There is no parameter named $expression3,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1247
     * @return $this
1248
     */
1249 6
    public function setUnion($expression1, $expression2 /* , $expression3, ... */)
0 ignored issues
show
Unused Code introduced by
The parameter $expression1 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $expression2 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1250
    {
1251 6
        return $this->operator('$setUnion', func_get_args());
1252
    }
1253
1254
    /**
1255
     * Counts and returns the total the number of items in an array.
1256
     *
1257
     * The argument can be any expression as long as it resolves to an array.
1258
     *
1259
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/size/
1260
     * @param mixed|self $expression
1261
     * @return $this
1262
     */
1263 3
    public function size($expression)
1264
    {
1265 3
        return $this->operator('$size', $expression);
1266
    }
1267
1268
    /**
1269
     * Returns a subset of an array.
1270
     *
1271
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/slice/
1272
     * @param mixed|self $array
1273
     * @param mixed|self $n
1274
     * @param mixed|self|null $position
1275
     * @return $this
1276
     *
1277
     * @since 1.3
1278
     */
1279 6
    public function slice($array, $n, $position = null)
1280
    {
1281 6
        if ($position === null) {
1282 3
            return $this->operator('$slice', [$array, $n]);
1283
        }
1284
1285 3
        return $this->operator('$slice', [$array, $position, $n]);
1286
    }
1287
1288
    /**
1289
     * Divides a string into an array of substrings based on a delimiter.
1290
     *
1291
     * $split removes the delimiter and returns the resulting substrings as
1292
     * elements of an array. If the delimiter is not found in the string, $split
1293
     * returns the original string as the only element of an array.
1294
     *
1295
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/split/
1296
     * @since 1.5
1297
     * @param mixed|self $string The string to be split. Can be any valid expression as long as it resolves to a string.
1298
     * @param mixed|self $delimiter The delimiter to use when splitting the string expression. Can be any valid expression as long as it resolves to a string.
1299
     *
1300
     * @return $this
1301
     */
1302 3
    public function split($string, $delimiter)
1303
    {
1304 3
        return $this->operator('$split', [$string, $delimiter]);
1305
    }
1306
1307
    /**
1308
     * Calculates the square root of a positive number and returns the result as
1309
     * a double.
1310
     *
1311
     * The argument can be any valid expression as long as it resolves to a
1312
     * non-negative number.
1313
     *
1314
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/sqrt/
1315
     * @param mixed|self $expression
1316
     * @return $this
1317
     */
1318 3
    public function sqrt($expression)
1319
    {
1320 3
        return $this->operator('$sqrt', $expression);
1321
    }
1322
1323
    /**
1324
     * Calculates the population standard deviation of the input values.
1325
     *
1326
     * The arguments can be any expression as long as it resolves to an array.
1327
     *
1328
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/stdDevPop/
1329
     * @param mixed|self $expression1
1330
     * @param mixed|self $expression2,... Additional samples
0 ignored issues
show
Bug introduced by
There is no parameter named $expression2,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1331
     * @return $this
1332
     *
1333
     * @since 1.3
1334
     */
1335 5
    public function stdDevPop($expression1 /* , $expression2, ... */)
1336
    {
1337 5
        $expression = (func_num_args() === 1) ? $expression1 : func_get_args();
1338
1339 5
        return $this->operator('$stdDevPop', $expression);
1340
    }
1341
1342
    /**
1343
     * Calculates the sample standard deviation of the input values.
1344
     *
1345
     * The arguments can be any expression as long as it resolves to an array.
1346
     *
1347
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/stdDevSamp/
1348
     * @param mixed|self $expression1
1349
     * @param mixed|self $expression2,... Additional samples
0 ignored issues
show
Bug introduced by
There is no parameter named $expression2,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1350
     * @return $this
1351
     *
1352
     * @since 1.3
1353
     */
1354 5
    public function stdDevSamp($expression1 /* , $expression2, ... */)
1355
    {
1356 5
        $expression = (func_num_args() === 1) ? $expression1 : func_get_args();
1357
1358 5
        return $this->operator('$stdDevSamp', $expression);
1359
    }
1360
1361
    /**
1362
     * Performs case-insensitive comparison of two strings. Returns
1363
     * 1 if first string is “greater than” the second string.
1364
     * 0 if the two strings are equal.
1365
     * -1 if the first string is “less than” the second string.
1366
     *
1367
     * The arguments can be any valid expression as long as they resolve to strings.
1368
     *
1369
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/strcasecmp/
1370
     * @param mixed|self $expression1
1371
     * @param mixed|self $expression2
1372
     * @return $this
1373
     */
1374 3
    public function strcasecmp($expression1, $expression2)
1375
    {
1376 3
        return $this->operator('$strcasecmp', [$expression1, $expression2]);
1377
    }
1378
1379
    /**
1380
     * Returns the number of UTF-8 encoded bytes in the specified string.
1381
     *
1382
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/strLenBytes/
1383
     * @since 1.5
1384
     * @param mixed|self $string
1385
     *
1386
     * @return $this
1387
     */
1388 3
    public function strLenBytes($string)
1389
    {
1390 3
        return $this->operator('$strLenBytes', $string);
1391
    }
1392
1393
    /**
1394
     * Returns the number of UTF-8 code points in the specified string.
1395
     *
1396
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/strLenCP/
1397
     * @since 1.5
1398
     * @param mixed|self $string
1399
     *
1400
     * @return $this
1401
     */
1402 3
    public function strLenCP($string)
1403
    {
1404 3
        return $this->operator('$strLenCP', $string);
1405
    }
1406
1407
    /**
1408
     * Returns a substring of a string, starting at a specified index position
1409
     * and including the specified number of characters. The index is zero-based.
1410
     *
1411
     * The arguments can be any valid expression as long as long as the first argument resolves to a string, and the second and third arguments resolve to integers.
1412
     *
1413
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/substr/
1414
     * @param mixed|self $string
1415
     * @param mixed|self $start
1416
     * @param mixed|self $length
1417
     * @return $this
1418
     */
1419 3
    public function substr($string, $start, $length)
1420
    {
1421 3
        return $this->operator('$substr', [$string, $start, $length]);
1422
    }
1423
1424
    /**
1425
     * Returns the substring of a string.
1426
     *
1427
     * The substring starts with the character at the specified UTF-8 byte index
1428
     * (zero-based) in the string and continues for the number of bytes
1429
     * specified.
1430
     *
1431
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/substrBytes/
1432
     * @since 1.5
1433
     * @param mixed|self $string The string from which the substring will be extracted. Can be any valid expression as long as it resolves to a string.
1434
     * @param mixed|self $start Indicates the starting point of the substring. Can be any valid expression as long as it resolves to a non-negative integer or number that can be represented as an integer.
1435
     * @param mixed|self $count Can be any valid expression as long as it resolves to a non-negative integer or number that can be represented as an integer.
1436
     *
1437
     * @return $this
1438
     */
1439 3
    public function substrBytes($string, $start, $count)
1440
    {
1441 3
        return $this->operator('$substrBytes', [$string, $start, $count]);
1442
    }
1443
1444
    /**
1445
     * Returns the substring of a string.
1446
     *
1447
     * The substring starts with the character at the specified UTF-8 code point
1448
     * (CP) index (zero-based) in the string for the number of code points
1449
     * specified.
1450
     *
1451
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/substrBytes/
1452
     * @since 1.5
1453
     * @param mixed|self $string The string from which the substring will be extracted. Can be any valid expression as long as it resolves to a string.
1454
     * @param mixed|self $start Indicates the starting point of the substring. Can be any valid expression as long as it resolves to a non-negative integer or number that can be represented as an integer.
1455
     * @param mixed|self $count Can be any valid expression as long as it resolves to a non-negative integer or number that can be represented as an integer.
1456
     *
1457
     * @return $this
1458
     */
1459 3
    public function substrCP($string, $start, $count)
1460
    {
1461 3
        return $this->operator('$substrCP', [$string, $start, $count]);
1462
    }
1463
1464
    /**
1465
     * Subtracts two numbers to return the difference. The second argument is
1466
     * subtracted from the first argument.
1467
     *
1468
     * The arguments can be any valid expression as long as they resolve to numbers and/or dates.
1469
     *
1470
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/subtract/
1471
     * @param mixed|self $expression1
1472
     * @param mixed|self $expression2
1473
     * @return $this
1474
     */
1475 3
    public function subtract($expression1, $expression2)
1476
    {
1477 3
        return $this->operator('$subtract', [$expression1, $expression2]);
1478
    }
1479
1480
    /**
1481
     * Calculates and returns the sum of all the numeric values that result from
1482
     * applying a specified expression to each document in a group of documents
1483
     * that share the same group by key. Ignores nun-numeric values.
1484
     *
1485
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/sum/
1486
     * @param mixed|self $expression
1487
     * @return $this
1488
     */
1489 9
    public function sum($expression)
1490
    {
1491 9
        return $this->operator('$sum', $expression);
1492
    }
1493
1494
    /**
1495
     * Converts a string to lowercase, returning the result.
1496
     *
1497
     * The argument can be any expression as long as it resolves to a string.
1498
     *
1499
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/toLower/
1500
     * @param mixed|self $expression
1501
     * @return $this
1502
     */
1503 3
    public function toLower($expression)
1504
    {
1505 3
        return $this->operator('$toLower', $expression);
1506
    }
1507
1508
    /**
1509
     * Converts a string to uppercase, returning the result.
1510
     *
1511
     * The argument can be any expression as long as it resolves to a string.
1512
     *
1513
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/toUpper/
1514
     * @param mixed|self $expression
1515
     * @return $this
1516
     */
1517 3
    public function toUpper($expression)
1518
    {
1519 3
        return $this->operator('$toUpper', $expression);
1520
    }
1521
1522
    /**
1523
     * Truncates a number to its integer.
1524
     *
1525
     * The <number> expression can be any valid expression as long as it
1526
     * resolves to a number.
1527
     *
1528
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/trunc/
1529
     * @param mixed|self $number
1530
     * @return $this
1531
     *
1532
     * @since 1.3
1533
     */
1534 3
    public function trunc($number)
1535
    {
1536 3
        return $this->operator('$trunc', $number);
1537
    }
1538
1539
    /**
1540
     * Returns a string that specifies the BSON type of the argument.
1541
     *
1542
     * The argument can be any valid expression.
1543
     *
1544
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/type/
1545
     * @since 1.5
1546
     * @param mixed|self $expression
1547
     *
1548
     * @return $this
1549
     */
1550 3
    public function type($expression)
1551
    {
1552 3
        return $this->operator('$type', $expression);
1553
    }
1554
1555
    /**
1556
     * Returns the week of the year for a date as a number between 0 and 53.
1557
     *
1558
     * The argument can be any expression as long as it resolves to a date.
1559
     *
1560
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/week/
1561
     * @param mixed|self $expression
1562
     * @return $this
1563
     */
1564 3
    public function week($expression)
1565
    {
1566 3
        return $this->operator('$week', $expression);
1567
    }
1568
1569
    /**
1570
     * Returns the year portion of a date.
1571
     *
1572
     * The argument can be any expression as long as it resolves to a date.
1573
     *
1574
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/year/
1575
     * @param mixed|self $expression
1576
     * @return $this
1577
     */
1578 4
    public function year($expression)
1579
    {
1580 4
        return $this->operator('$year', $expression);
1581
    }
1582
1583
    /**
1584
     * Transposes an array of input arrays so that the first element of the
1585
     * output array would be an array containing, the first element of the first
1586
     * input array, the first element of the second input array, etc.
1587
     *
1588
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/zip/
1589
     * @since 1.5
1590
     * @param mixed|self $inputs An array of expressions that resolve to arrays. The elements of these input arrays combine to form the arrays of the output array.
1591
     * @param bool|null $useLongestLength A boolean which specifies whether the length of the longest array determines the number of arrays in the output array.
1592
     * @param mixed|self|null $defaults An array of default element values to use if the input arrays have different lengths. You must specify useLongestLength: true along with this field, or else $zip will return an error.
1593
     * @return $this
1594
     */
1595 9
    public function zip($inputs, $useLongestLength = null, $defaults = null)
1596
    {
1597 9
        $args = ['inputs' => $inputs];
1598 9
        if ($useLongestLength !== null) {
1599 6
            $args['useLongestLength'] = $useLongestLength;
1600
        }
1601 9
        if ($defaults !== null) {
1602 3
            $args['defaults'] = $defaults;
1603
        }
1604
1605 9
        return $this->operator('$zip', $args);
1606
    }
1607
1608
1609
    /**
1610
     * @param mixed|self $expression
1611
     * @return mixed
1612
     */
1613 343
    private function ensureArray($expression)
1614
    {
1615 343
        if (is_string($expression) && substr($expression, 0, 1) === '$') {
1616 341
            return '$' . $this->getDocumentPersister()->prepareFieldName(substr($expression, 1));
1617 217
        } elseif (is_array($expression)) {
1618 209
            return array_map([$this, 'ensureArray'], $expression);
1619 87
        } elseif ($expression instanceof self) {
1620 20
            return $expression->getExpression();
1621
        }
1622
1623
        // Convert PHP types to MongoDB types for everything else
1624 85
        return Type::convertPHPToDatabaseValue($expression);
1625
    }
1626
1627
    /**
1628
     * @return \Doctrine\ODM\MongoDB\Persisters\DocumentPersister
1629
     */
1630 343
    private function getDocumentPersister()
1631
    {
1632 343
        return $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name);
0 ignored issues
show
Bug introduced by
Accessing name on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
1633
    }
1634
1635
    /**
1636
     * Defines an operator and value on the expression.
1637
     *
1638
     * If there is a current field, the operator will be set on it; otherwise,
1639
     * the operator is set at the top level of the query.
1640
     *
1641
     * @param string $operator
1642
     * @param array|self[]|self $expression
1643
     * @return $this
1644
     */
1645 341
    private function operator($operator, $expression)
1646
    {
1647 341
        if ($this->currentField) {
1648 137
            $this->expr[$this->currentField][$operator] = $this->ensureArray($expression);
1649
        } else {
1650 212
            $this->expr[$operator] = $this->ensureArray($expression);
1651
        }
1652
1653 341
        return $this;
1654
    }
1655
1656
    /**
1657
     * Ensure that a current field has been set.
1658
     *
1659
     * @param string $method
1660
     *
1661
     * @throws \LogicException if a current field has not been set
1662
     */
1663 12
    private function requiresCurrentField($method = null)
1664
    {
1665 12
        if ( ! $this->currentField) {
1666 1
            throw new \LogicException(($method ?: 'This method') . ' requires you set a current field using field().');
1667
        }
1668 11
    }
1669
1670
    /**
1671
     * @param string $method
1672
     *
1673
     * @throws \BadMethodCallException if there is no current switch operator
1674
     */
1675 8
    private function requiresSwitchStatement($method = null)
1676
    {
1677 8
        $message = ($method ?: 'This method') . ' requires a valid switch statement (call switch() first).';
1678
1679 8
        if ($this->currentField) {
1680
            if (! isset($this->expr[$this->currentField]['$switch'])) {
1681
                throw new \BadMethodCallException($message);
1682
            }
1683 8
        } elseif (! isset($this->expr['$switch'])) {
1684 4
            throw new \BadMethodCallException($message);
1685
        }
1686 4
    }
1687
1688
    /**
1689
     * Evaluates a series of case expressions. When it finds an expression which
1690
     * evaluates to true, $switch executes a specified expression and breaks out
1691
     * of the control flow.
1692
     *
1693
     * To add statements, use the {@link case()}, {@link then()} and
1694
     * {@link default()} methods.
1695
     *
1696
     * @return $this
1697
     */
1698 4
    public function switch()
1699
    {
1700 4
        $this->operator('$switch', []);
1701
1702 4
        return $this;
1703
    }
1704
1705
    /**
1706
     * Adds a case statement for the current branch of the $switch operator.
1707
     *
1708
     * Requires {@link case()} to be called first. The argument can be any valid
1709
     * expression.
1710
     *
1711
     * @param mixed|self $expression
1712
     *
1713
     * @return $this
1714
     */
1715 6
    public function then($expression)
1716
    {
1717 6
        if (! is_array($this->switchBranch)) {
1718 4
            throw new \BadMethodCallException(static::class . '::then requires a valid case statement (call case() first).');
1719
        }
1720
1721 4
        $this->switchBranch['then'] = $expression;
1722
1723 4
        if ($this->currentField) {
1724
            $this->expr[$this->currentField]['$switch']['branches'][] = $this->ensureArray($this->switchBranch);
1725
        } else {
1726 4
            $this->expr['$switch']['branches'][] = $this->ensureArray($this->switchBranch);
1727
        }
1728
1729 4
        $this->switchBranch = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $switchBranch.

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..

Loading history...
1730
1731 4
        return $this;
1732
    }
1733
1734
}
1735