Completed
Push — master ( 3f285b...f51ae3 )
by Alessandro
08:34
created

Expression   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 123
Duplicated Lines 17.89 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 22
loc 123
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A and() 11 11 1
A or() 11 11 1
A equal() 0 8 1
A notEqual() 0 8 1
A getExpressionFilters() 0 4 1
A prepareFilterIndex() 0 6 2
A mapExpressions() 0 9 2
A operationExpression() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

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 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 16
    public function __construct()
17
    {
18 16
        $this->filters = [];
19 16
    }
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 8
        $this->prepareFilterIndex('$and');
29
30 8
        $this->filters['$and'] = array_merge(
31 8
            $this->filters['$and'],
32 8
            $this->mapExpressions(...func_get_args())
33
        );
34
35 8
        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 3
        $this->prepareFilterIndex('$or');
46
47 3
        $this->filters['$or'] = array_merge(
48 3
            $this->filters['$or'],
49 3
            $this->mapExpressions(...func_get_args())
50
        );
51
52 3
        return $this;
53
    }
54
55
    /**
56
     * @param string     $field
57
     * @param string|Expression $expression
58
     *
59 2
     * @return $this
60
     */
61 2
    public function equal(string $field, $expression)
62
    {
63 2
        $this->prepareFilterIndex($field);
64 2
65 2
        $this->filters[$field] = $this->operationExpression('$eq', $expression);
66
67 2
        return $this;
68
    }
69
70
    /**
71
     * @param string     $field
72 16
     * @param string|Expression $expression
73
     *
74 16
     * @return $this
75
     */
76
    public function notEqual(string $field, $expression)
77
    {
78
        $this->prepareFilterIndex($field);
79
80 11
        $this->filters[$field] = $this->operationExpression('$ne', $expression);
81
82 11
        return $this;
83 11
    }
84
85 11
    /**
86
     * @return array
87
     */
88
    public function getExpressionFilters(): array
89
    {
90
        return $this->filters;
91
    }
92 9
93
    /**
94 9
     * @param string $operator
95
     */
96 9
    private function prepareFilterIndex(string $operator)
97 9
    {
98
        if (!isset($this->filters[$operator])) {
99
            $this->filters[$operator] = [];
100
        }
101
    }
102
103
    /**
104
     * @param  $expressions
105
     *
106
     * @return array
107
     */
108 2
    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...
109
    {
110 2
        return array_map(
111 2
            function ($expression) {
112 2
                return  $expression instanceof Expression ? $expression->getExpressionFilters() : $expression;
113
            },
114 2
            func_get_args()
115 2
        );
116
    }
117
118
    /**
119
     * @param string $operation
120
     * @param string|Expression  $expression
121
     *
122
     * @return array
123
     */
124
    private function operationExpression(string $operation, $expression): array
125
    {
126
        $filter = $expression instanceof Expression ? $expression->getExpressionFilters() : $expression;
127
128
        return [$operation => $filter];
129
    }
130
}
131