Issues (21)

Security Analysis    not enabled

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/Http/PaginatedResource.php (1 issue)

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
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 11/21/15
5
 * Time: 9:04 AM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace NilPortugues\Api\JsonApi\Http;
11
12
use JsonSerializable;
13
14
/**
15
 * Class PaginatedResource.
16
 */
17
class PaginatedResource implements JsonSerializable
18
{
19
    /**
20
     * @var int
21
     */
22
    protected $total;
23
    /**
24
     * @var int
25
     */
26
    protected $pages;
27
    /**
28
     * @var int
29
     */
30
    protected $currentPage;
31
    /**
32
     * @var int
33
     */
34
    protected $pageSize;
35
    /**
36
     * @var string
37
     */
38
    protected $offsetLimit;
39
40
    /**
41
     * @var string
42
     */
43
    protected $offset;
44
45
    /**
46
     * @var string
47
     */
48
    protected $cursor;
49
50
    /**
51
     * @var string
52
     */
53
    protected $data = [];
54
    /**
55
     * @var array
56
     */
57
    protected $include = [];
58
    /**
59
     * @var array
60
     */
61
    protected $links = [];
62
    /**
63
     * @var array
64
     */
65
    protected $meta = [];
66
67
    /**
68
     * @param string $elements
69
     * @param int    $currentPage
70
     * @param int    $pageSize
71
     * @param null   $total
72
     * @param array  $links
73
     */
74
    public function __construct($elements, $currentPage = null, $pageSize = null, $total = null, array $links = [])
75
    {
76
        $this->setData($elements);
0 ignored issues
show
$elements is of type string, but the function expects a array.

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...
77
        $this->setPageSize($pageSize);
78
        $this->setCurrentPage($currentPage);
79
        $this->setTotal($total);
80
        $this->setTotalPages($total, $pageSize);
81
        $this->setLinks($links);
82
    }
83
84
    /**
85
     * @param string $cursor
86
     *
87
     * @return $this
88
     */
89
    public function setPageCursor($cursor)
90
    {
91
        $this->cursor = $cursor;
92
    }
93
94
    /**
95
     * @param array $data
96
     *
97
     * @throws \InvalidArgumentException
98
     *
99
     * @return $this
100
     */
101
    protected function setData($data)
102
    {
103
        $data = (array) json_decode($data, true);
104
105
        if (false === array_key_exists('data', $data)) {
106
            throw new \InvalidArgumentException('Provided JSON has no `data` member defined');
107
        }
108
109
        $this->data = $data['data'];
110
111
        if (!empty($data['included'])) {
112
            $this->include = $data['included'];
113
        }
114
    }
115
116
    /**
117
     * @param array $links
118
     *
119
     * @return $this
120
     */
121
    public function setLinks(array $links)
122
    {
123
        foreach ($links as &$href) {
124
            $href = ['href' => $href];
125
        }
126
127
        $this->links = $links;
128
    }
129
130
    /**
131
     * @param string $offset
132
     *
133
     * @return $this
134
     */
135
    public function setPageOffset($offset)
136
    {
137
        $this->offset = $offset;
138
    }
139
140
    /**
141
     * @param string $offsetLimit
142
     *
143
     * @return $this
144
     */
145
    public function setPageOffsetLimit($offsetLimit)
146
    {
147
        $this->offsetLimit = $offsetLimit;
148
    }
149
150
    /**
151
     * @param $currentPage
152
     */
153
    protected function setCurrentPage($currentPage)
154
    {
155
        $this->currentPage = (int) $currentPage;
156
    }
157
158
    /**
159
     * @param $pageSize
160
     */
161
    protected function setPageSize($pageSize)
162
    {
163
        $this->pageSize = (int) $pageSize;
164
    }
165
    /**
166
     * @param $total
167
     */
168
    protected function setTotal($total)
169
    {
170
        $this->total = (int) $total;
171
    }
172
173
    /**
174
     * @param $total
175
     * @param $pageSize
176
     */
177
    protected function setTotalPages($total, $pageSize)
178
    {
179
        if (0 == $pageSize) {
180
            $this->pages = 0;
181
182
            return;
183
        }
184
185
        $this->pages = (int) ceil($total / $pageSize);
186
    }
187
188
    /**
189
     * @param array $meta
190
     */
191
    public function setMeta(array $meta)
192
    {
193
        $this->meta = $meta;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function jsonSerialize()
200
    {
201
        $callable = function ($v) {
202
            return $v !== null;
203
        };
204
205
        $data = array_filter([
206
                'data' => array_filter($this->data, $callable),
207
                'included' => array_filter($this->include),
208
                'links' => array_filter($this->links),
209
                'meta' => array_filter(
210
                    array_merge([
211
                            'page' => array_filter([
212
                                    'total' => $this->total,
213
                                    'last' => $this->pages,
214
                                    'number' => $this->currentPage,
215
                                    'size' => $this->pageSize,
216
                                    'limit' => $this->offsetLimit,
217
                                    'offset' => $this->offset,
218
                                    'cursor' => $this->cursor,
219
                                ], $callable),
220
                        ],
221
                        $this->meta
222
                    )
223
                ),
224
                'jsonapi' => ['version' => '1.0'],
225
            ], $callable);
226
227
        return $data;
228
    }
229
}
230