Completed
Push — master ( b260d7...a51b64 )
by Alessandro
01:37
created

Expression::mapExpressions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 1
nop 1
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 $queryPartial;
12
13
    /**
14
     * Expression constructor.
15
     */
16
    public function __construct()
17
    {
18
        $this->queryPartial = [];
19
    }
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
        $this->prepareOperator('$and');
29
30
        $this->queryPartial['$and'] = array_merge(
31
            $this->queryPartial['$and'],
32
            $this->mapExpressions(...func_get_args())
33
        );
34
35
        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
        $this->prepareOperator('$or');
46
47
        $this->queryPartial['$or'] = array_merge(
48
            $this->queryPartial['$or'],
49
            $this->mapExpressions(...func_get_args())
50
        );
51
52
        return $this;
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function getQueryPartial(): array
59
    {
60
        return $this->queryPartial;
61
    }
62
63
    /**
64
     * @param string $operator
65
     */
66
    private function prepareOperator(string $operator)
67
    {
68
        if (!isset($this->queryPartial[$operator])) {
69
            $this->queryPartial[$operator] = [];
70
        }
71
    }
72
73
    /**
74
     * @param $expressions
75
     *
76
     * @return array
77
     */
78
    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...
79
    {
80
        return array_map(
81
            function ($expression) {
82
                return $expression instanceof Expression ? $expression->getQueryPartial() : $expression;
83
            },
84
            func_get_args()
85
        );
86
    }
87
}
88