Completed
Pull Request — master (#20)
by Daniel
03:37 queued 01:43
created

Query   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 5.41 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 6
loc 111
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 1
B create() 6 24 2
A comparison() 0 4 1
A composite() 0 4 1
A join() 0 4 1
A getClassFqn() 0 4 1
A hasExpression() 0 4 1
A getExpression() 0 4 1
A getOrderings() 0 4 1
A getMaxResults() 0 4 1
A getFirstResult() 0 4 1
A getJoins() 0 4 1
A getSelects() 0 4 1

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
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\ObjectAgent\Query;
6
7
use Psi\Component\ObjectAgent\Query\Join;
8
9
final class Query
10
{
11
    private $classFqn;
12
    private $expression;
13
    private $orderings;
14
    private $firstResult;
15
    private $maxResults;
16
    private $joins;
17
    private $selects;
18
19
    private function __construct(
20
        string $classFqn,
21
        array $selects = [],
22
        array $joins = [],
23
        Expression $expression = null,
24
        array $orderings = [],
25
        int $firstResult = null,
26
        int $maxResults = null
27
    ) {
28
        $this->classFqn = $classFqn;
29
        $this->expression = $expression;
30
        $this->orderings = $orderings;
31
        $this->firstResult = $firstResult;
32
        $this->maxResults = $maxResults;
33
        $this->joins = $joins;
34
        $this->selects = $selects;
35
36
        array_walk($joins, function (Join $join) {
0 ignored issues
show
Unused Code introduced by
The parameter $join 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...
37
        });
38
    }
39
40
    public static function create(
41
        string $classFqn,
42
        array $query = []
43
    ) {
44
        $defaults = [
45
            'selects' => [],
46
            'criteria' => null,
47
            'orderings' => [],
48
            'joins' => [],
49
            'firstResult' => null,
50
            'maxResults' => null,
51
        ];
52
53 View Code Duplication
        if ($diff = array_diff(array_keys($query), array_keys($defaults))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
54
            throw new \InvalidArgumentException(sprintf(
55
                'Invalid query keys "%s", valid keys: "%s"',
56
                implode('", "', $diff), implode('", "', array_keys($defaults))
57
            ));
58
        }
59
60
        $query = array_merge($defaults, $query);
61
62
        return new self($classFqn, $query['selects'], $query['joins'], $query['criteria'], $query['orderings'], $query['firstResult'], $query['maxResults']);
63
    }
64
65
    public static function comparison(string $comparator, $value1, $value2): Comparison
66
    {
67
        return new Comparison($comparator, $value1, $value2);
68
    }
69
70
    public static function composite(string $type, ...$expressions): Composite
71
    {
72
        return new Composite($type, $expressions);
73
    }
74
75
    public static function join(string $type, string $alias)
76
    {
77
        return new Join($type, $alias);
78
    }
79
80
    public function getClassFqn(): string
81
    {
82
        return $this->classFqn;
83
    }
84
85
    public function hasExpression()
86
    {
87
        return null !== $this->expression;
88
    }
89
90
    public function getExpression(): Expression
91
    {
92
        return $this->expression;
93
    }
94
95
    public function getOrderings(): array
96
    {
97
        return $this->orderings;
98
    }
99
100
    public function getMaxResults()
101
    {
102
        return $this->maxResults;
103
    }
104
105
    public function getFirstResult()
106
    {
107
        return $this->firstResult;
108
    }
109
110
    public function getJoins(): array
111
    {
112
        return $this->joins;
113
    }
114
115
    public function getSelects(): array
116
    {
117
        return $this->selects;
118
    }
119
}
120