Completed
Push — master ( ae3e1f...594fe9 )
by Dominik
03:33
created

QueryBuilder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 26.51 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 92.31%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 10
c 6
b 1
f 1
lcom 2
cbo 4
dl 22
loc 83
ccs 24
cts 26
cp 0.9231
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addToArrayNode() 11 11 2
A addToObjectNode() 11 11 2
A reassignParent() 0 6 3
A end() 0 6 1
A query() 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
namespace Saxulum\ElasticSearchQueryBuilder;
4
5
use Saxulum\ElasticSearchQueryBuilder\Node\AbstractNode;
6
use Saxulum\ElasticSearchQueryBuilder\Node\ArrayNode;
7
use Saxulum\ElasticSearchQueryBuilder\Node\ObjectNode;
8
9
class QueryBuilder
10
{
11
    /**
12
     * @var ObjectNode
13
     */
14
    protected $rootNode;
15
16
    /**
17
     * @var AbstractNode
18
     */
19
    protected $node;
20
21 14
    public function __construct()
22
    {
23 14
        $this->rootNode = new ObjectNode();
24 14
        $this->node = $this->rootNode;
25 14
    }
26
27
    /**
28
     * @param AbstractNode $node
29
     * @param bool $allowDefault
30
     * @return $this
31
     * @throws \Exception
32
     */
33 2 View Code Duplication
    public function addToArrayNode(AbstractNode $node, $allowDefault = false)
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...
34
    {
35 2
        if (!$this->node instanceof ArrayNode) {
36
            throw new \Exception(sprintf('You cannot call %s on node type %s', __METHOD__, get_class($this->node)));
37
        }
38
        
39 2
        $this->node->add($node, $allowDefault);
40 2
        $this->reassignParent($node);
41
        
42 2
        return $this;
43
    }
44
45
    /**
46
     * @param string $key
47
     * @param AbstractNode $node
48
     * @param bool $allowDefault
49
     * @return $this
50
     * @throws \Exception
51
     */
52 14 View Code Duplication
    public function addToObjectNode($key, AbstractNode $node, $allowDefault = false)
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...
53
    {
54 14
        if (!$this->node instanceof ObjectNode) {
55
            throw new \Exception(sprintf('You cannot call %s on node type %s', __METHOD__, get_class($this->node)));
56
        }
57
58 14
        $this->node->add($key, $node, $allowDefault);
59 14
        $this->reassignParent($node);
60
        
61 14
        return $this;
62
    }
63
64
    /**
65
     * @param AbstractNode $node
66
     */
67 14
    protected function reassignParent(AbstractNode $node)
68
    {
69 14
        if ($node instanceof ArrayNode || $node instanceof ObjectNode) {
70 14
            $this->node = $node;
71 14
        }
72 14
    }
73
74
    /**
75
     * @return $this
76
     */
77 1
    public function end()
78
    {
79 1
        $this->node = $this->node->getParent();
80
81 1
        return $this;
82
    }
83
84
    /**
85
     * @return Query
86
     */
87 14
    public function query()
88
    {
89 14
        return new Query($this->rootNode);
90
    }
91
}
92