QueryBuilder::addToArrayNode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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
/**
17
 * @deprecated use Saxulum\ElasticSearchQueryBuilder\Node\ObjectNode
18
 */
19
final class QueryBuilder implements QueryBuilderInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Saxulum\ElasticSearchQue...r\QueryBuilderInterface has been deprecated with message: use base interface, query builder will be dropped

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
20
{
21
    /**
22
     * @var ObjectNode
23
     */
24
    private $rootNode;
25
26
    /**
27
     * @var AbstractNode
28
     */
29
    private $node;
30
31 19
    public function __construct()
32
    {
33 19
        @trigger_error(sprintf('Use "%s" instead of the "%s"', ObjectNode::class, self::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

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