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
Push — master ( cf8d9a...01d377 )
by Jonny
03:52
created

AbstractRequest::getMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 27
    public function __construct($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
105
    {
106 27
        $this->headers         = array();
107 27
        $this->data            = array();
108 27
        $this->bodyStyles      = array();
109 27
        $this->delay           = 0;
110 27
        $this->viewportWidth   = 0;
111 27
        $this->viewportHeight  = 0;
112
113 27
        $this->setMethod($method);
114 27
        $this->setTimeout($timeout);
115
116 27
        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
            $this->setUrl($url);
118
        }
119 27
    }
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 27
    public function setMethod($method)
130
    {
131 27
        $method     = strtoupper($method);
132 27
        $reflection = new \ReflectionClass('\JonnyW\PhantomJs\Http\RequestInterface');
133
134 27
        if (!$reflection->hasConstant('METHOD_' . $method)) {
135
            throw new InvalidMethodException(sprintf('Invalid method provided: %s', $method));
136
        }
137
138 27
        $this->method = $method;
139
140 27
        return $this;
141
    }
142
143
    /**
144
     * Get request method
145
     *
146
     * @access public
147
     * @return string
148
     */
149 24
    public function getMethod()
150
    {
151 24
        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 27
    public function setTimeout($timeout)
162
    {
163 27
        $this->timeout = $timeout;
164
165 27
        return $this;
166
    }
167
168
    /**
169
     * Get timeout period
170
     *
171
     * @access public
172
     * @return int
173
     */
174 24
    public function getTimeout()
175
    {
176 24
        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 24
    public function getDelay()
200
    {
201 24
        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 2
    public function setViewportSize($width, $height)
213
    {
214 2
        $this->viewportWidth  = (int) $width;
215 2
        $this->viewportHeight = (int) $height;
216
217 2
        return $this;
218
    }
219
220
    /**
221
     * Get viewport width.
222
     *
223
     * @access public
224
     * @return int
225
     */
226 24
    public function getViewportWidth()
227
    {
228 24
        return (int) $this->viewportWidth;
229
    }
230
231
    /**
232
     * Get viewport height.
233
     *
234
     * @access public
235
     * @return int
236
     */
237 24
    public function getViewportHeight()
238
    {
239 24
        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 24
    public function setUrl($url)
251
    {
252 24
        if (!filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
253
            throw new InvalidUrlException(sprintf('Invalid URL provided: %s', $url));
254
        }
255
256 24
        $this->url = $url;
257
258 24
        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 24
    public function getUrl()
270
    {
271 24
        if (!in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
272 1
            return $this->url;
273
        }
274
275 23
        $url = $this->url;
276
277 23
        if (count($this->data)) {
278
279 1
            $url .= false === strpos($url, '?') ? '?' : '&';
280 1
            $url .= http_build_query($this->data);
281 1
        }
282
283 23
        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 24
    public function getBody()
294
    {
295 24
        if (in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
296 23
            return '';
297
        }
298
299 1
        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 2
    public function setRequestData(array $data)
310
    {
311 2
        $this->data = $data;
312
313 2
        return $this;
314
    }
315
316
    /**
317
     * Get request data
318
     *
319
     * @access public
320
     * @param  boolean $flat
321
     * @return array
322
     */
323 1
    public function getRequestData($flat = true)
324
    {
325 1
        if ($flat) {
326 1
            return $this->flattenData($this->data);
327
        }
328
329
        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
    public function setHeaders(array $headers)
340
    {
341
        $this->headers = $headers;
342
    }
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
    public function addHeader($header, $value)
353
    {
354
        $this->headers[$header] = $value;
355
356
        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
    public function addHeaders(array $headers)
367
    {
368
        $this->headers = array_merge($this->headers, $headers);
369
370
        return $this;
371
    }
372
373
    /**
374
     * Get request headers
375
     *
376
     * @access public
377
     * @param  string       $format
378
     * @return array|string
379
     */
380 24
    public function getHeaders($format = 'default')
381
    {
382 24
        if ($format === 'json') {
383
            return json_encode($this->headers);
384
        }
385
386 24
        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
    public function setBodyStyles(array $styles)
397
    {
398
        $this->bodyStyles = $styles;
399
400
        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 24
    public function getBodyStyles($format = 'default')
411
    {
412 24
        if ($format === 'json') {
413
            return json_encode($this->bodyStyles);
414
        }
415
416 24
        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 1
    protected function flattenData(array $data, $prefix = '', $format = '%s')
430
    {
431 1
        $flat = array();
432
433 1
        foreach ($data as $name => $value) {
434
435 1
            $ref = $prefix . sprintf($format, $name);
436
437 1
            if (is_array($value)) {
438
439
                $flat += $this->flattenData($value, $ref, '[%s]');
440
                continue;
441
            }
442
443 1
            $flat[$ref] = $value;
444 1
        }
445
446 1
        return $flat;
447
    }
448
}
449