Issues (33)

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/ResponseArrayTrait.php (8 issues)

Labels

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
namespace Bouhnosaure\Dogecoin;
4
5
trait ResponseArrayTrait
6
{
7
    /**
8
     * Gets data by using key with dotted notation.
9
     *
10
     * @param string|null $key
11
     *
12
     * @return mixed
13
     */
14 36
    public function get($key = null)
15
    {
16 36
        $key = $this->constructKey($key);
17
18 36
        if (is_null($key)) {
19 21
            return $this->result();
0 ignored issues
show
It seems like result() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
20
        }
21
22 16
        return $this->parseKey($key, function ($part, $result) {
23 24
            if (isset($result[$part])) {
24 24
                return $result[$part];
25
            }
26 24
        });
27
    }
28
29
    /**
30
     * Checks if key exists.
31
     *
32
     * @param string|null $key
33
     *
34
     * @return bool
35
     */
36 6
    public function exists($key = null)
37
    {
38 6
        $key = $this->constructKey($key);
39
40 4
        return $this->parseKey($key, function ($part, $result) {
41 6
            return array_key_exists($part, $result);
42 6
        });
43
    }
44
45
    /**
46
     * Checks if key exists and not null.
47
     *
48
     * @param string|null $key
49
     *
50
     * @return bool
51
     */
52 3
    public function has($key = null)
53
    {
54 3
        $key = $this->constructKey($key);
55
56 3
        return $this->parseKey($key, function ($part, $result) {
57 3
            return isset($result[$part]);
58 3
        });
59
    }
60
61
    /**
62
     * Gets first element.
63
     *
64
     * @return mixed
65
     */
66 3
    public function first()
67
    {
68 3
        $value = $this->get();
69
70 3
        if (is_array($value)) {
71 3
            return reset($value);
72
        }
73
74 3
        return $value;
75
    }
76
77
    /**
78
     * Gets last element.
79
     *
80
     * @return mixed
81
     */
82 3
    public function last()
83
    {
84 3
        $value = $this->get();
85
86 3
        if (is_array($value)) {
87 3
            return end($value);
88
        }
89
90 3
        return $value;
91
    }
92
93
    /**
94
     * Checks if response contains value.
95
     *
96
     * @param mixed $value
97
     *
98
     * @return bool
99
     */
100 3
    public function contains($value)
101
    {
102 3
        return in_array($value, $this->result());
0 ignored issues
show
It seems like result() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
103
    }
104
105
    /**
106
     * Set current key.
107
     *
108
     * @param string|null $key
109
     *
110
     * @return static
111
     */
112 24
    public function key($key = null)
113
    {
114 24
        $new = clone $this;
115 24
        $new->current = $key;
0 ignored issues
show
The property current does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
116
117 24
        return $new;
118
    }
119
120
    /**
121
     * Gets response keys.
122
     *
123
     * @return array
124
     */
125 3
    public function keys()
126
    {
127 3
        return array_keys($this->result());
0 ignored issues
show
It seems like result() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
128
    }
129
130
    /**
131
     * Gets response values.
132
     *
133
     * @return array
134
     */
135 3
    public function values()
136
    {
137 3
        return array_values($this->result());
0 ignored issues
show
It seems like result() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
138
    }
139
140
    /**
141
     * Gets random value.
142
     *
143
     * @param int         $number
144
     * @param string|null $key
145
     *
146
     * @return mixed
147
     */
148 3
    public function random($number = 1, $key = null)
149
    {
150 3
        $value = $this->get($key);
151
152 3
        if (is_array($value)) {
153 3
            $keys = array_keys($value);
154 3
            $keysLength = count($keys);
155
156 3
            shuffle($keys);
157
158 3
            if ($number > $keysLength) {
159 3
                $number = $keysLength;
160 1
            }
161
162 3
            for ($result = [], $count = 0; $count < $number; $count++) {
163 3
                $result[$keys[$count]] = $value[$keys[$count]];
164 1
            }
165
166 3
            return count($result) > 1 ? $result : current($result);
167
        }
168
169 3
        return $value;
170
    }
171
172
    /**
173
     * Counts response items.
174
     *
175
     * @param string|null $key
176
     *
177
     * @return int
178
     */
179 3
    public function count($key = null)
180
    {
181 3
        if (is_null($this->constructKey($key))) {
182 3
            return count($this->result());
0 ignored issues
show
It seems like result() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
183
        }
184
185 3
        if (!$this->exists($key)) {
186 3
            return 0;
187
        }
188
189 3
        $value = $this->get($key);
190
191 3
        if (is_array($value)) {
192 3
            return count($value);
193
        }
194
195 3
        return 1;
196
    }
197
198
    /**
199
     * Flattens multi-dimensional array.
200
     *
201
     * @param string|null $key
202
     *
203
     * @return array
204
     */
205 6
    public function flatten($key = null)
206
    {
207 6
        $array = new \RecursiveIteratorIterator(
208 6
            new \RecursiveArrayIterator((array) $this->get($key))
209 2
        );
210
211 6
        $tmp = [];
212 6
        foreach ($array as $value) {
213 6
            $tmp[] = $value;
214 2
        }
215
216 6
        return $tmp;
217
    }
218
219
    /**
220
     * Gets sum of values.
221
     *
222
     * @param string|null $key
223
     *
224
     * @return float
225
     */
226 3
    public function sum($key = null)
227
    {
228 3
        return array_sum($this->flatten($key));
229
    }
230
231
    /**
232
     * Get response item by key.
233
     *
234
     * @param string|null $key
235
     *
236
     * @return mixed
237
     */
238 15
    public function __invoke($key = null)
239
    {
240 15
        return $this->key($key);
241
    }
242
243
    /**
244
     * Constructs full key.
245
     *
246
     * @param string|null $key
247
     *
248
     * @return string|null
249
     */
250 42
    protected function constructKey($key = null)
251
    {
252 42
        if (!is_null($key) && !is_null($this->current)) {
253 9
            return $this->current.'.'.$key;
254
        }
255
256 42
        if (is_null($key) && !is_null($this->current)) {
257 21
            return $this->current;
258
        }
259
260 39
        return $key;
261
    }
262
263
    /**
264
     * Parses dotted notation.
265
     *
266
     * @param string     $key
267
     * @param callable   $callback
268
     * @param array|null $result
269
     *
270
     * @return mixed
271
     */
272 30
    protected function parseKey($key, callable $callback, $result = null)
273
    {
274 30
        $parts = explode('.', trim($key, '.'));
275 30
        $result = $result ?: $this->result();
0 ignored issues
show
It seems like result() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
276
277 30
        foreach ($parts as $index => $part) {
278 30
            if ($part == '*') {
279 6
                $sub = [];
280
281 6
                foreach (array_keys($result) as $subKey) {
282 6
                    $path = $subKey;
283
284 6
                    if (isset($parts[$index + 1])) {
285 6
                        $pathParts = array_slice($parts, $index + 1);
286 6
                        $path .= '.'.implode('.', $pathParts);
287 2
                    }
288
289 6
                    $sub[$subKey] = $this->parseKey($path, $callback, $result);
290 2
                }
291
292 6
                return $sub;
293
            }
294
295 30
            if (!$return = $callback($part, $result)) {
296 9
                return $return;
297
            }
298
299 30
            $result = $result[$part];
300 10
        }
301
302 30
        return $return;
0 ignored issues
show
The variable $return does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
303
    }
304
}
305