Completed
Push — master ( 51369d...9b6ea9 )
by Alessandro
01:40
created

Expression::operationExpressions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 1
nop 2
crap 2
1
<?php declare(strict_types = 1);
2
3
namespace Algatux\QueryBuilder;
4
5
/**
6
 * Class Expression.
7
 */
8
class Expression
9
{
10
    /** @var array */
11
    private $filters;
12
13
    /**
14
     * Expression constructor.
15
     */
16 14
    public function __construct()
17
    {
18 14
        $this->filters = [];
19 14
    }
20
21
    /**
22
     * @param array|Expression $expression
23
     *
24
     * @return $this
25
     */
26 View Code Duplication
    public function and($expression)
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...
27
    {
28 7
        $this->prepareFilterIndex('$and');
29
30 7
        $this->filters['$and'] = array_merge(
31 7
            $this->filters['$and'],
32 7
            $this->mapExpressions(...func_get_args())
33
        );
34
35 7
        return $this;
36
    }
37
38
    /**
39
     * @param array|Expression $expression
40
     *
41
     * @return $this
42
     */
43 View Code Duplication
    public function or($expression)
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...
44
    {
45 2
        $this->prepareFilterIndex('$or');
46
47 2
        $this->filters['$or'] = array_merge(
48 2
            $this->filters['$or'],
49 2
            $this->mapExpressions(...func_get_args())
50
        );
51
52 2
        return $this;
53
    }
54
55
    /**
56
     * @param string           $field
57
     * @param Expression[]|array $expressions
58
     */
59 2
    public function notEqual(string $field, ...$expressions)
60
    {
61 2
        $this->prepareFilterIndex($field);
62
63 2
        $this->filters[$field] = array_merge(
64 2
            $this->filters[$field],
65 2
            $this->operationExpressions('$ne', $expressions)
66
        );
67 2
    }
68
69
    /**
70
     * @return array
71
     */
72 14
    public function getExpressionFilters(): array
73
    {
74 14
        return $this->filters;
75
    }
76
77
    /**
78
     * @param string $operator
79
     */
80 9
    private function prepareFilterIndex(string $operator)
81
    {
82 9
        if (!isset($this->filters[$operator])) {
83 9
            $this->filters[$operator] = [];
84
        }
85 9
    }
86
87
    /**
88
     * @param  $expressions
89
     *
90
     * @return array
91
     */
92 7
    private function mapExpressions($expressions): array
0 ignored issues
show
Unused Code introduced by
The parameter $expressions 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...
93
    {
94 7
        return array_map(
95
            function ($expression) {
96 7
                return  $expression instanceof Expression ? $expression->getExpressionFilters() : $expression;
97 7
            },
98
            func_get_args()
99
        );
100
    }
101
102
    /**
103
     * @param string $operation
104
     * @param array  $expressions
105
     *
106
     * @return array
107
     */
108 2
    private function operationExpressions(string $operation, array $expressions): array
109
    {
110 2
        return array_map(
111 2
            function ($expression) use ($operation) {
112 2
                $filter = $expression instanceof Expression ? $expression->getExpressionFilters() : $expression;
113
114 2
                return [$operation => $filter];
115 2
            },
116
            $expressions
117
        );
118
    }
119
}
120