Issues (18)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/QueryBuilder.php (8 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
The call to addToObjectNode() misses a required argument $node.

This check looks for function calls that miss required arguments.

Loading history...
$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
$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
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
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
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