Issues (8)

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/Payload.php (6 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
namespace PerfectOblivion\Payload;
4
5
use ArrayAccess;
6
use Traversable;
7
use JsonSerializable;
8
use Illuminate\Contracts\Support\Jsonable;
9
use Illuminate\Contracts\Support\Arrayable;
10
use PerfectOblivion\Payload\Contracts\PayloadContract;
11
12
class Payload implements PayloadContract, ArrayAccess, JsonSerializable, Jsonable, Arrayable
13
{
14
    /** @var int */
15
    private $status;
16
17
    /** @var mixed */
18
    private $output;
19
20
    /** @var array */
21
    private $messages = [];
22
23
    /** @var string|null */
24
    private $outputWrapper = null;
25
26
    /** @var string */
27
    private $messagesWrapper = 'messages';
28
29
    /**
30
     * Initialize a Payload object.
31
     */
32
    public static function instance(): PayloadContract
33
    {
34
        return new static;
35
    }
36
37
    /**
38
     * Set the Payload status.
39
     *
40
     * @param  int  $status
41
     */
42
    public function setStatus(int $status): PayloadContract
43
    {
44
        $instance = clone $this;
45
        $instance->status = $status;
46
47
        return $instance;
48
    }
49
50
    /**
51
     * Get the status of the payload.
52
     */
53
    public function getStatus(): int
54
    {
55
        return $this->status;
56
    }
57
58
    /**
59
     * Set the Payload messages.
60
     *
61
     * @param  array  $output
0 ignored issues
show
There is no parameter named $output. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
62
     */
63
    public function setMessages(array $messages): PayloadContract
64
    {
65
        $instance = clone $this;
66
        $instance->messages = $messages;
67
68
        return $instance;
69
    }
70
71
    /**
72
     * Get messages array from the payload.
73
     */
74
    public function getMessages(): array
75
    {
76
        return $this->messages;
77
    }
78
79
    /**
80
     * Set the Payload output.
81
     *
82
     * @param  mixed  $output
83
     * @param  string|null  $wrapper
84
     */
85
    public function setOutput($output, ? string $wrapper = null): PayloadContract
86
    {
87
        if ($wrapper) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $wrapper of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
88
            $this->outputWrapper = $wrapper;
89
        }
90
91
        $instance = clone $this;
92
        $instance->output = $output;
93
94
        return $instance;
95
    }
96
97
    /**
98
     * Get the Payload output.
99
     */
100
    public function getOutput(): array
101
    {
102
        return $this->getArrayableItems($this->output);
103
    }
104
105
    /**
106
     * Get the raw Payload output.
107
     *
108
     * @return mixed
109
     */
110
    public function getRawOutput()
111
    {
112
        return $this->output;
113
    }
114
115
    /**
116
     * Retrieve the Payload output and wrap it.
117
     * Use the outputWrapper if it is set, otherwise use 'data'.
118
     */
119
    public function getwrappedOutput(): array
120
    {
121
        return $this->outputWrapper ? [$this->outputWrapper => $this->output] : ['data' => $this->output];
122
    }
123
124
    /**
125
     * Get the wrapper for the output.
126
     */
127
    public function getOutputWrapper(): string
128
    {
129
        return $this->outputWrapper;
130
    }
131
132
    /**
133
     * Get the wrapper for the messages.
134
     */
135
    public function getMessagesWrapper(): string
136
    {
137
        return $this->messagesWrapper;
138
    }
139
140
    /**
141
     * Return all of the components of the payload in array format.
142
     */
143
    public function all(): array
144
    {
145
        $all = ['output' => $this->getOutput()];
146
        if ($this->messages) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->messages of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
147
            $all = array_merge($all, ['messages' => $this->getMessages()]);
148
        }
149
        if ($this->status) {
150
            $all = array_merge($all, ['status' => $this->getStatus()]);
151
        }
152
153
        return $all;
154
    }
155
156
    /**
157
     * Convert the Payload output to an array.
158
     */
159
    public function getArrayableItems(): array
160
    {
161
        if (is_array($this->output)) {
162
            return $this->output;
163
        } elseif ($this->output instanceof Arrayable) {
164
            return $this->output->toArray();
165
        } elseif ($this->output instanceof Jsonable) {
166
            return json_decode($this->output->toJson(), true);
167
        } elseif ($this->output instanceof JsonSerializable) {
0 ignored issues
show
The class JsonSerializable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
168
            return $this->output->jsonSerialize();
169
        } elseif ($this->output instanceof Traversable) {
170
            return iterator_to_array($this->output);
171
        }
172
173
        return (array) $this->output;
174
    }
175
176
    /**
177
     * Dynamically retrieve attributes on the OutputItem.
178
     *
179
     * @param  string  $key
180
     *
181
     * @return mixed
182
     */
183
    public function __get($key)
184
    {
185
        return $this->output[$key];
186
    }
187
188
    /**
189
     * Convert the Payload instance to JSON.
190
     *
191
     * @param  int  $options
192
     *
193
     * @return string
194
     */
195
    public function toJson($options = 0)
196
    {
197
        return json_encode($this->jsonSerialize(), $options);
198
    }
199
200
    /**
201
     * Convert the Payload into something JSON serializable.
202
     *
203
     * @return array
204
     */
205
    public function jsonSerialize()
206
    {
207
        return $this->toArray();
208
    }
209
210
    /**
211
     * Convert the Payload instance to an array.
212
     *
213
     * @return array
214
     */
215
    public function toArray()
216
    {
217
        $output = $this->outputWrapper || $this->messages ? $this->getWrappedOutput() : $this->getOutput();
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->outputWrapper of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $this->messages of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
218
        $messages = $this->messages ? [$this->messagesWrapper => $this->messages] : $this->messages;
219
220
        return $this->messages ? array_merge($output, $messages) : $output;
221
    }
222
223
    /**
224
     * Determine if the given attribute exists.
225
     *
226
     * @param  mixed  $offset
227
     *
228
     * @return bool
229
     */
230
    public function offsetExists($offset)
231
    {
232
        return isset($this->$offset);
233
    }
234
235
    /**
236
     * Get the value for a given offset.
237
     *
238
     * @param  mixed  $offset
239
     *
240
     * @return mixed
241
     */
242
    public function offsetGet($offset)
243
    {
244
        return $this->$offset;
245
    }
246
247
    /**
248
     * Set the value for a given offset.
249
     *
250
     * @param  mixed  $offset
251
     * @param  mixed  $value
252
     */
253
    public function offsetSet($offset, $value)
254
    {
255
        $this->$offset = $value;
256
    }
257
258
    /**
259
     * Unset the value for a given offset.
260
     *
261
     * @param  mixed  $offset
262
     */
263
    public function offsetUnset($offset)
264
    {
265
        unset($this->$offset);
266
    }
267
268
    /**
269
     * Determine if an attribute exists on the Payload.
270
     *
271
     * @param  string  $key
272
     *
273
     * @return bool
274
     */
275
    public function __isset($key)
276
    {
277
        if (! $this->output) {
278
            return false;
279
        }
280
281
        return isset($this->output[$key]);
282
    }
283
284
    /**
285
     * Unset an attribute on the Payload.
286
     *
287
     * @param  string  $key
288
     */
289
    public function __unset($key)
290
    {
291
        if (! $this->output) {
292
            return;
293
        }
294
295
        unset($this->output[$key]);
296
    }
297
298
    /**
299
     * Convert the Payload to its string representation.
300
     *
301
     * @return string
302
     */
303
    public function __toString()
304
    {
305
        return $this->toJson();
306
    }
307
}
308