Completed
Push — master ( 9dfc82...fbb6e1 )
by Dominik
02:13
created

QueryBuilder::nullNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Saxulum\ElasticSearchQueryBuilder;
6
7
use Saxulum\ElasticSearchQueryBuilder\Node\AbstractNode;
8
use Saxulum\ElasticSearchQueryBuilder\Node\ArrayNode;
9
use Saxulum\ElasticSearchQueryBuilder\Node\BoolNode;
10
use Saxulum\ElasticSearchQueryBuilder\Node\FloatNode;
11
use Saxulum\ElasticSearchQueryBuilder\Node\IntNode;
12
use Saxulum\ElasticSearchQueryBuilder\Node\NullNode;
13
use Saxulum\ElasticSearchQueryBuilder\Node\ObjectNode;
14
use Saxulum\ElasticSearchQueryBuilder\Node\StringNode;
15
16
final class QueryBuilder implements QueryBuilderInterface
17
{
18
    /**
19
     * @var ObjectNode
20
     */
21
    private $rootNode;
22
23
    /**
24
     * @var AbstractNode
25
     */
26
    private $node;
27
28 19
    public function __construct()
29
    {
30 19
        $this->rootNode = new ObjectNode();
31 19
        $this->node = $this->rootNode;
32 19
    }
33
34
    /**
35
     * @param array ...$arguments
36
     * @return QueryBuilderInterface
37
     * @throws \Exception
38
     */
39 16
    public function add(...$arguments): QueryBuilderInterface
40
    {
41 16
        if ($this->node instanceof ObjectNode) {
42 16
            return $this->addToObjectNode(...$arguments);
0 ignored issues
show
Bug introduced by
The call to addToObjectNode() misses a required argument $node.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$arguments is of type array<integer,array>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
        }
44
45 4
        return $this->addToArrayNode(...$arguments);
0 ignored issues
show
Documentation introduced by
$arguments is of type array<integer,array>, but the function expects a object<Saxulum\ElasticSe...lder\Node\AbstractNode>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
    }
47
48
    /**
49
     * @param AbstractNode $node
50
     *
51
     * @return QueryBuilderInterface
52
     *
53
     * @throws \Exception
54
     */
55 5 View Code Duplication
    public function addToArrayNode(AbstractNode $node): QueryBuilderInterface
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...
56
    {
57 5
        if (!$this->node instanceof ArrayNode) {
58 1
            throw new \Exception(sprintf('You cannot call %s on node type: %s', __FUNCTION__, get_class($this->node)));
59
        }
60
61 4
        $this->node->add($node);
62 4
        $this->reassignParent($node);
63
64 4
        return $this;
65
    }
66
67
    /**
68
     * @param string       $key
69
     * @param AbstractNode $node
70
     *
71
     * @return QueryBuilderInterface
72
     *
73
     * @throws \Exception
74
     */
75 17 View Code Duplication
    public function addToObjectNode(string $key, AbstractNode $node): QueryBuilderInterface
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...
76
    {
77 17
        if (!$this->node instanceof ObjectNode) {
78 1
            throw new \Exception(sprintf('You cannot call %s on node type: %s', __FUNCTION__, get_class($this->node)));
79
        }
80
81 17
        $this->node->add($key, $node);
82 17
        $this->reassignParent($node);
83
84 17
        return $this;
85
    }
86
87
    /**
88
     * @param AbstractNode $node
89
     */
90 17
    private function reassignParent(AbstractNode $node)
91
    {
92 17
        if ($node instanceof ArrayNode || $node instanceof ObjectNode) {
93 17
            $this->node = $node;
94
        }
95 17
    }
96
97
    /**
98
     * @return QueryBuilderInterface
99
     *
100
     * @throws \Exception
101
     */
102 2
    public function end(): QueryBuilderInterface
103
    {
104 2
        if (null === $this->node = $this->node->getParent()) {
105 1
            throw new \Exception(sprintf('You cannot call %s on main node', __FUNCTION__));
106
        }
107
108 1
        return $this;
109
    }
110
111
    /**
112
     * @param bool $allowDefault
113
     * @return ArrayNode
114
     */
115 5
    public function arrayNode(bool $allowDefault = false): ArrayNode
116
    {
117 5
        return new ArrayNode($allowDefault);
118
    }
119
120
    /**
121
     * @param bool|null $value
122
     * @param bool $allowDefault
123
     * @return BoolNode
124
     */
125 1
    public function boolNode($value = null, bool $allowDefault = false): BoolNode
126
    {
127 1
        return new BoolNode($value, $allowDefault);
128
    }
129
130
    /**
131
     * @param float|null $value
132
     * @param bool $allowDefault
133
     * @return FloatNode
134
     */
135 1
    public function floatNode($value = null, bool $allowDefault = false): FloatNode
136
    {
137 1
        return new FloatNode($value, $allowDefault);
138
    }
139
140
    /**
141
     * @param int|null $value
142
     * @param bool $allowDefault
143
     * @return IntNode
144
     */
145 4
    public function intNode($value = null, bool $allowDefault = false): IntNode
146
    {
147 4
        return new IntNode($value, $allowDefault);
148
    }
149
150
    /**
151
     * @return NullNode
152
     */
153 1
    public function nullNode(): NullNode
154
    {
155 1
        return new NullNode;
156
    }
157
158
    /**
159
     * @param bool $allowDefault
160
     * @return ObjectNode
161
     */
162 16
    public function objectNode(bool $allowDefault = false): ObjectNode
163
    {
164 16
        return new ObjectNode($allowDefault);
165
    }
166
167
    /**
168
     * @param string|null $value
169
     * @param bool $allowDefault
170
     * @return StringNode
171
     */
172 13
    public function stringNode($value = null, bool $allowDefault = false): StringNode
173
    {
174 13
        return new StringNode($value, $allowDefault);
175
    }
176
177
    /**
178
     * @return \stdClass|null
179
     */
180 16
    public function serialize()
181
    {
182 16
        return $this->rootNode->serialize();
183
    }
184
185
    /**
186
     * @param boolean $beautify
187
     * @return string
188
     */
189 16
    public function json(bool $beautify = false): string
190
    {
191 16
        if (null === $serialized = $this->serialize()) {
192 2
            return '';
193
        }
194
195 14
        if ($beautify) {
196 1
            return json_encode($serialized, JSON_PRETTY_PRINT);
197
        }
198
199 13
        return json_encode($serialized);
200
    }
201
}
202