GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#133)
by Jens A.
04:20
created

AbstractRequest::setMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the php-phantomjs.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace JonnyW\PhantomJs\Http;
11
12
use JonnyW\PhantomJs\Exception\InvalidUrlException;
13
use JonnyW\PhantomJs\Exception\InvalidMethodException;
14
use JonnyW\PhantomJs\Procedure\InputInterface;
15
16
/**
17
 * PHP PhantomJs
18
 *
19
 * @author Jon Wenmoth <[email protected]>
20
 */
21
abstract class AbstractRequest
22
    implements RequestInterface, InputInterface
23
{
24
    /**
25
     * Headers
26
     *
27
     * @var array
28
     * @access protected
29
     */
30
    protected $headers;
31
32
    /**
33
     * Request data
34
     *
35
     * @var array
36
     * @access protected
37
     */
38
    protected $data;
39
40
    /**
41
     * Request URL
42
     *
43
     * @var string
44
     * @access protected
45
     */
46
    protected $url;
47
48
    /**
49
     * Request method
50
     *
51
     * @var string
52
     * @access protected
53
     */
54
    protected $method;
55
56
    /**
57
     * Timeout period
58
     *
59
     * @var int
60
     * @access protected
61
     */
62
    protected $timeout;
63
64
    /**
65
     * Page load delay time.
66
     *
67
     * @var int
68
     * @access protected
69
     */
70
    protected $delay;
71
72
    /**
73
     * Viewport width.
74
     *
75
     * @var int
76
     * @access protected
77
     */
78
    protected $viewportWidth;
79
80
    /**
81
     * Viewport height.
82
     *
83
     * @var int
84
     * @access protected
85
     */
86
    protected $viewportHeight;
87
88
    /**
89
     * Body styles.
90
     *
91
     * @var array
92
     * @access protected
93
     */
94
    protected $bodyStyles;
95
96
    /**
97
     * Internal constructor
98
     *
99
     * @access public
100
     * @param string $url     (default: null)
101
     * @param string $method  (default: RequestInterface::METHOD_GET)
102
     * @param int    $timeout (default: 5000)
103
     */
104 118
    public function __construct($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
105
    {
106 118
        $this->headers         = array();
107 118
        $this->data            = array();
108 118
        $this->bodyStyles      = array();
109 118
        $this->delay           = 0;
110 118
        $this->viewportWidth   = 0;
111 118
        $this->viewportHeight  = 0;
112
113 118
        $this->setMethod($method);
114 118
        $this->setTimeout($timeout);
115
116 118
        if ($url) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $url 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...
117 8
            $this->setUrl($url);
118 8
        }
119 118
    }
120
121
    /**
122
     * Set request method
123
     *
124
     * @access public
125
     * @param  string                                             $method
126
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
127
     * @throws \JonnyW\PhantomJs\Exception\InvalidMethodException
128
     */
129 118
    public function setMethod($method)
130
    {
131 118
        $method     = strtoupper($method);
132 118
        $reflection = new \ReflectionClass('\JonnyW\PhantomJs\Http\RequestInterface');
133
134 118
        if (!$reflection->hasConstant('METHOD_' . $method)) {
135 3
            throw new InvalidMethodException(sprintf('Invalid method provided: %s', $method));
136
        }
137
138 118
        $this->method = $method;
139
140 118
        return $this;
141
    }
142
143
    /**
144
     * Get request method
145
     *
146
     * @access public
147
     * @return string
148
     */
149 60
    public function getMethod()
150
    {
151 60
        return $this->method;
152
    }
153
154
    /**
155
     * Set timeout period
156
     *
157
     * @access public
158
     * @param  int                                    $timeout
159
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
160
     */
161 118
    public function setTimeout($timeout)
162
    {
163 118
        $this->timeout = $timeout;
164
165 118
        return $this;
166
    }
167
168
    /**
169
     * Get timeout period
170
     *
171
     * @access public
172
     * @return int
173
     */
174 35
    public function getTimeout()
175
    {
176 35
        return $this->timeout;
177
    }
178
179
    /**
180
     * Set page load delay time (seconds).
181
     *
182
     * @access public
183
     * @param  int                                    $delay
184
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
185
     */
186 6
    public function setDelay($delay)
187
    {
188 6
        $this->delay = (int) $delay;
189
190 6
        return $this;
191
    }
192
193
    /**
194
     * Get page load delay time (seconds).
195
     *
196
     * @access public
197
     * @return int
198
     */
199 27
    public function getDelay()
200
    {
201 27
        return (int) $this->delay;
202
    }
203
204
    /**
205
     * Set viewport size.
206
     *
207
     * @access public
208
     * @param  int  $width
209
     * @param  int  $height
210
     * @return void
211
     */
212 8
    public function setViewportSize($width, $height)
213
    {
214 8
        $this->viewportWidth  = (int) $width;
215 8
        $this->viewportHeight = (int) $height;
216
217 8
        return $this;
218
    }
219
220
    /**
221
     * Get viewport width.
222
     *
223
     * @access public
224
     * @return int
225
     */
226 32
    public function getViewportWidth()
227
    {
228 32
        return (int) $this->viewportWidth;
229
    }
230
231
    /**
232
     * Get viewport height.
233
     *
234
     * @access public
235
     * @return int
236
     */
237 32
    public function getViewportHeight()
238
    {
239 32
        return (int) $this->viewportHeight;
240
    }
241
242
    /**
243
     * Set request URL
244
     *
245
     * @access public
246
     * @param  string                                          $url
247
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
248
     * @throws \JonnyW\PhantomJs\Exception\InvalidUrlException
249
     */
250 56
    public function setUrl($url)
251
    {
252 56
        if (!filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
253 3
            throw new InvalidUrlException(sprintf('Invalid URL provided: %s', $url));
254
        }
255
256 53
        $this->url = $url;
257
258 53
        return $this;
259
    }
260
261
    /**
262
     * Get request URL
263
     *  - Assembles query string for GET
264
     *  and HEAD requests
265
     *
266
     * @access public
267
     * @return string
268
     */
269 46
    public function getUrl()
270
    {
271 46
        if (!in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
272 4
            return $this->url;
273
        }
274
275 42
        $url = $this->url;
276
277 42
        if (count($this->data)) {
278
279 10
            $url .= false === strpos($url, '?') ? '?' : '&';
280 10
            $url .= http_build_query($this->data);
281 10
        }
282
283 42
        return $url;
284
    }
285
286
    /**
287
     * Get content body
288
     *  - Returns query string if not GET or HEAD
289
     *
290
     * @access public
291
     * @return string
292
     */
293 38
    public function getBody()
294
    {
295 38
        if (in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
296 34
            return '';
297
        }
298
299 4
        return http_build_query($this->getRequestData());
300
    }
301
302
    /**
303
     * Set request data
304
     *
305
     * @access public
306
     * @param  array                                  $data
307
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
308
     */
309 29
    public function setRequestData(array $data)
310
    {
311 29
        $this->data = $data;
312
313 29
        return $this;
314
    }
315
316
    /**
317
     * Get request data
318
     *
319
     * @access public
320
     * @param  boolean $flat
321
     * @return array
322
     */
323 10
    public function getRequestData($flat = true)
324
    {
325 10
        if ($flat) {
326 7
            return $this->flattenData($this->data);
327
        }
328
329 3
        return $this->data;
330
    }
331
332
    /**
333
     * Set headers
334
     *
335
     * @access public
336
     * @param  array                                  $headers
337
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
338
     */
339 9
    public function setHeaders(array $headers)
340
    {
341 9
        $this->headers = $headers;
342 9
    }
343
344
    /**
345
     * Add single header
346
     *
347
     * @access public
348
     * @param  string                                 $header
349
     * @param  string                                 $value
350
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
351
     */
352 1
    public function addHeader($header, $value)
353
    {
354 1
        $this->headers[$header] = $value;
355
356 1
        return $this;
357
    }
358
359
    /**
360
     * Merge headers with existing
361
     *
362
     * @access public
363
     * @param  array                                  $headers
364
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
365
     */
366 3
    public function addHeaders(array $headers)
367
    {
368 3
        $this->headers = array_merge($this->headers, $headers);
369
370 3
        return $this;
371
    }
372
373
    /**
374
     * Get request headers
375
     *
376
     * @access public
377
     * @param  string       $format
378
     * @return array|string
379
     */
380 39
    public function getHeaders($format = 'default')
381
    {
382 39
        if ($format === 'json') {
383 4
            return json_encode($this->headers);
384
        }
385
386 35
        return $this->headers;
387
    }
388
389
    /**
390
     * Set body styles
391
     *
392
     * @access public
393
     * @param  array                                  $styles
394
     * @return \JonnyW\PhantomJs\Http\AbstractRequest
395
     */
396 1
    public function setBodyStyles(array $styles)
397
    {
398 1
        $this->bodyStyles = $styles;
399
400 1
        return $this;
401
    }
402
403
    /**
404
     * Get body styles
405
     *
406
     * @access public
407
     * @param  string       $format (default: 'default')
408
     * @return array|string
409
     */
410 29
    public function getBodyStyles($format = 'default')
411
    {
412 29
        if ($format === 'json') {
413
            return json_encode($this->bodyStyles);
414
        }
415
416 29
        return $this->bodyStyles;
417
    }
418
419
    /**
420
     * Flatten data into single
421
     * dimensional array
422
     *
423
     * @access protected
424
     * @param  array  $data
425
     * @param  string $prefix
426
     * @param  string $format
427
     * @return array
428
     */
429 7
    protected function flattenData(array $data, $prefix = '', $format = '%s')
430
    {
431 7
        $flat = array();
432
433 7
        foreach ($data as $name => $value) {
434
435 7
            $ref = $prefix . sprintf($format, $name);
436
437 7
            if (is_array($value)) {
438
439 3
                $flat += $this->flattenData($value, $ref, '[%s]');
440 3
                continue;
441
            }
442
443 7
            $flat[$ref] = $value;
444 7
        }
445
446 7
        return $flat;
447
    }
448
}
449