Completed
Push — master ( 09b86b...ba0798 )
by Andreas
20:05 queued 12s
created

CriteriaMerger::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 3
cts 5
cp 0.6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.2559
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Query;
6
7
use const E_USER_DEPRECATED;
8
use function array_filter;
9
use function array_values;
10
use function count;
11
use function sprintf;
12
use function trigger_error;
13
14
/**
15
 * Utility class for merging query criteria.
16
 *
17
 * This is mainly used to incorporate filter and ReferenceMany mapping criteria
18
 * into a query. Each criteria array will be joined with "$and" to avoid cases
19
 * where criteria might be inadvertently overridden with array_merge().
20
 *
21
 * @final
22
 */
23
class CriteriaMerger
24
{
25 1148 View Code Duplication
    public function __construct()
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...
26
    {
27 1148
        if (self::class === static::class) {
28 1148
            return;
29
        }
30
31
        @trigger_error(sprintf('The class "%s" extends "%s" which will be final in MongoDB ODM 2.0.', static::class, self::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
32
    }
33
34
    /**
35
     * Combines any number of criteria arrays as clauses of an "$and" query.
36
     *
37
     * @param array ...$criterias Any number of query criteria arrays
38
     */
39 74
    public function merge(...$criterias) : array
40
    {
41
        $nonEmptyCriterias = array_values(array_filter($criterias, static function (array $criteria) {
42 73
            return ! empty($criteria);
43 74
        }));
44
45 74
        switch (count($nonEmptyCriterias)) {
46 74
            case 0:
47 11
                return [];
48
49 73
            case 1:
50 68
                return $nonEmptyCriterias[0];
51
52
            default:
53 22
                return ['$and' => $nonEmptyCriterias];
54
        }
55
    }
56
}
57