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.

Request::sessionInit()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 5
nop 0
dl 0
loc 17
ccs 0
cts 12
cp 0
crap 42
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Kotori.php
4
 *
5
 * A Tiny Model-View-Controller PHP Framework
6
 *
7
 * This content is released under the Apache 2 License
8
 *
9
 * Copyright (c) 2015-2017 Kotori Technology. All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 *     http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23
24
/**
25
 * Request Class
26
 *
27
 * @package     Kotori
28
 * @subpackage  Http
29
 * @author      Kokororin
30
 * @link        https://kotori.love
31
 */
32
namespace Kotori\Http;
33
34
use Kotori\Core\Container;
35
use Kotori\Debug\Hook;
36
use Kotori\Exception\NotFoundException;
37
38
class Request
39
{
40
    /**
41
     * Ip address
42
     *
43
     * @var mixed
44
     */
45
    protected $ip = null;
46
47
    /**
48
     * Http headers
49
     *
50
     * @var array
51
     */
52
    protected $headers = [];
53
54
    /**
55
     * Session has been started or not
56
     *
57
     * @var boolean
58
     */
59
    protected $hasSessionStarted = false;
60
61
    /**
62
     * Class constructor
63
     *
64
     * Initialize Request.
65
     */
66
    public function __construct()
67
    {
68
        Hook::listen(__CLASS__);
69
    }
70
71
    /**
72
     * Internal method used to retrieve values from given arrays.
73
     *
74
     * @param  array $source
75
     * @param  mixed $key
76
     * @param  mixed $default
77
     * @return mixed
78
     */
79
    protected function getRequestParams(&$source, $key = null, $default = null)
80
    {
81
        // If $key is NULL, it means that the whole $source is requested
82
        if (!isset($key) || $key == null) {
83
            $key = array_keys($source);
84
        }
85
86
        if (is_array($key)) {
87
            $output = [];
88
            foreach ($key as $k) {
89
                $output[$k] = $this->getRequestParams($source, $k);
90
            }
91
92
            return $output;
93
        }
94
95
        if (isset($source[$key])) {
96
            $value = $source[$key];
97
        } else {
98
            return $default;
99
        }
100
101
        return $value;
102
    }
103
104
    /**
105
     * Fetch an item from the GET array
106
     *
107
     * @param  mixed $key
108
     * @return mixed
109
     */
110
    public function get($key = null, $default = null)
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
111
    {
112
        return $this->getRequestParams($_GET, $key, $default);
113
    }
114
115
    /**
116
     * Fetch an item from the POST array
117
     *
118
     * @param  mixed $key
119
     * @param  mixed $default
120
     * @return mixed
121
     */
122
    public function post($key = null, $default = null)
0 ignored issues
show
Coding Style introduced by
post uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
123
    {
124
        $rawPostData = file_get_contents('php://input');
125
        $source = json_decode($rawPostData, true);
126
        if (json_last_error() != JSON_ERROR_NONE) {
127
            $source = $_POST;
128
        }
129
130
        return $this->getRequestParams($source, $key, $default);
131
    }
132
133
    /**
134
     * Fetch an item from the SERVER array
135
     *
136
     * @param  mixed $key
137
     * @param  mixed $default
138
     * @return mixed
139
     */
140
    public function server($key = null, $default = null)
0 ignored issues
show
Coding Style introduced by
server uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
141
    {
142
        return $this->getRequestParams($_SERVER, $key, $default);
143
    }
144
145
    /**
146
     * Set or Get cookie
147
     *
148
     * @param  mixed  $key
149
     * @param  string $value
150
     * @param  mixed  $options
151
     * @return mixed
152
     */
153 2
    public function cookie($key = '', $value = '', $options = null)
0 ignored issues
show
Coding Style introduced by
cookie uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
154
    {
155
        $defaultOptions = [
156 2
            'prefix' => '',
157
            'expire' => 86400,
158
            'path' => '/',
159
            'secure' => false,
160
            'httponly' => false,
161
        ];
162
163 2
        if (!is_null($options)) {
164
            if (is_numeric($options)) {
165
                $options = ['expire' => $options];
166
            } elseif (is_string($options)) {
167
                parse_str($options, $options);
168
            }
169
170
            $options = array_merge($defaultOptions, array_change_key_case($options));
171
        }
172
173 2
        if (!empty($options['httponly'])) {
174
            ini_set('session.cookie_httponly', 1);
175
        }
176
177 2
        if (is_null($key)) {
178
            if (empty($_COOKIE)) {
179
                return null;
180
            }
181
182
            $prefix = empty($value) ? $options['prefix'] : $value;
183
            if (!empty($prefix)) {
184
                foreach ($_COOKIE as $key => $val) {
185
                    if (0 === stripos($key, $prefix)) {
186
                        setcookie($key, '', time() - 3600, $options['path'], $options['domain'], $options['secure'], $options['httponly']);
187
                        unset($_COOKIE[$key]);
188
                    }
189
                }
190
            }
191
192
            return null;
193 2
        } elseif ('' === $key) {
194
            // Get All Cookie
195
            return $_COOKIE;
196
        }
197
198 2
        $key = $options['prefix'] . str_replace('.', '_', $key);
199 2
        if ('' === $value) {
200 2
            if (isset($_COOKIE[$key])) {
201 1
                $value = $_COOKIE[$key];
202 1
                if (0 === strpos($value, 'kotori:')) {
203
                    $value = substr($value, 6);
204
                    return array_map('urldecode', json_decode(MAGIC_QUOTES_GPC ? stripslashes($value) : $value, true));
205
                } else {
206 1
                    return $value;
207
                }
208
            } else {
209 1
                return null;
210
            }
211
        } else {
212 2
            if (is_null($value)) {
213 1
                setcookie($key, '', time() - 3600, $options['path'], $options['domain'], $options['secure'], $options['httponly']);
214 1
                unset($_COOKIE[$key]); // Delete Cookie
215
            } else {
216
                // Set Cookie
217 2
                if (is_array($value)) {
218
                    $value = 'kotori:' . json_encode(array_map('urlencode', $value));
219
                }
220
221 2
                $expire = !empty($options['expire']) ? time() + intval($options['expire']) : 0;
222 2
                setcookie($key, $value, $expire, $options['path'], $options['domain'], $options['secure'], $options['httponly']);
223 2
                $_COOKIE[$key] = $value;
224
            }
225
        }
226
227 2
        return null;
228
    }
229
230
    /**
231
     * Session Initialize
232
     *
233
     * @return void
234
     */
235
    public function sessionInit()
236
    {
237
        ini_set('session.auto_start', 0);
238
        $config = Container::get('config')->get('session');
239
        if ($config['adapter'] != '') {
240
            $class = '\\Kotori\\Http\\Session\\' . ucfirst($config['adapter']);
241
            if (!class_exists($class) || !session_set_save_handler(new $class($config))) {
242
                throw new NotFoundException('error session handler:' . $class);
243
            }
244
        }
245
246
        session_name('KOTORI_SESSID');
247
        if (isset($config['auto_start']) && $config['auto_start']) {
248
            session_start();
249
            $this->hasSessionStarted = true;
250
        }
251
    }
252
253
    /**
254
     * Start a Session
255
     *
256
     * @return void
257
     */
258 2
    public function sessionStart()
259
    {
260 2
        if (!$this->hasSessionStarted) {
261 1
            if (PHP_SESSION_ACTIVE != session_status()) {
262 1
                session_start();
263
            }
264
265 1
            $this->hasSessionStarted = true;
266
        }
267 2
    }
268
269
    /**
270
     * Set or Get session
271
     *
272
     * @param  mixed  $key
273
     * @param  string $value
274
     * @return mixed
275
     */
276 2
    public function session($key = '', $value = '')
0 ignored issues
show
Coding Style introduced by
session uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
277
    {
278 2
        $this->sessionStart();
279
280 2
        if (is_null($key)) {
281
            if (empty($_SESSION)) {
282
                return null;
283
            }
284
285
            unset($_SESSION);
286 2
        } elseif ('' === $key) {
287
            // Get All Session
288
            return $_SESSION;
289
        }
290
291 2
        if ('' === $value) {
292 2
            return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
293
        } else {
294 2
            if (is_null($value)) {
295 1
                unset($_SESSION[$key]);
296
            } else {
297 2
                $_SESSION[$key] = $value;
298
            }
299
        }
300
301 2
        return null;
302
    }
303
304
    /**
305
     * Is HTTPS?
306
     *
307
     * Determines if the application is accessed via an encrypted
308
     * (HTTPS) connection.
309
     *
310
     * @return  boolean
311
     */
312 2
    public function isSecure()
0 ignored issues
show
Coding Style introduced by
isSecure uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
313
    {
314 2
        if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
315
            return true;
316 2
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO']) {
317
            return true;
318 2
        } elseif (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
319
            return true;
320
        }
321
322 2
        return false;
323
    }
324
325
    /**
326
     * Base URL
327
     *
328
     * Returns base url
329
     *
330
     * @return string
331
     */
332 1
    public function getBaseUrl()
0 ignored issues
show
Coding Style introduced by
getBaseUrl uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
333
    {
334 1
        if (isset($_SERVER['HTTP_HOST']) && preg_match('/^((\[[0-9a-f:]+\])|(\d{1,3}(\.\d{1,3}){3})|[a-z0-9\-\.]+)(:\d+)?$/i', $_SERVER['HTTP_HOST'])) {
335 1
            $base_url = ($this->isSecure() ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST']
336 1
            . substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])));
337
        } else {
338
            $base_url = 'http://localhost/';
339
        }
340
341 1
        return rtrim($base_url, '/') . '/';
342
    }
343
344
    /**
345
     * Returns Client Ip Address
346
     *
347
     * @param  int     $type
348
     * @return string
349
     */
350 1
    public function getClientIp($type = 0)
0 ignored issues
show
Coding Style introduced by
getClientIp uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
351
    {
352 1
        $type = $type ? 1 : 0;
353
354 1
        if ($this->ip !== null) {
355
            return $this->ip[$type];
356
        }
357
358 1
        if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
359
            $this->ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
360 1
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
361
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
362
            $pos = array_search('unknown', $arr);
363
            if (false !== $pos) {
364
                unset($arr[$pos]);
365
            }
366
367
            $this->ip = trim($arr[0]);
368 1
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
369
            $this->ip = $_SERVER['HTTP_CLIENT_IP'];
370 1
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
371 1
            $this->ip = $_SERVER['REMOTE_ADDR'];
372
        }
373
374
        // Check ip
375 1
        $long = sprintf("%u", ip2long($this->ip));
376 1
        $this->ip = $long ? [$this->ip, $long] : ['0.0.0.0', 0];
377 1
        return $this->ip[$type];
378
    }
379
380
    /**
381
     * Returns Http Host
382
     *
383
     * @return string
384
     */
385 1
    public function getHostName()
0 ignored issues
show
Coding Style introduced by
getHostName uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
386
    {
387 1
        $possibleHostSources = ['HTTP_X_FORWARDED_HOST', 'HTTP_HOST', 'SERVER_NAME', 'SERVER_ADDR'];
388
        $sourceTransformations = [
389 1
            "HTTP_X_FORWARDED_HOST" => function ($value) {
390
                $elements = explode(',', $value);
391
                return trim(end($elements));
392 1
            },
393
        ];
394 1
        $host = '';
395 1
        foreach ($possibleHostSources as $source) {
396 1
            if (!empty($host)) {
397 1
                break;
398
            }
399
400 1
            if (empty($_SERVER[$source])) {
401 1
                continue;
402
            }
403
404 1
            $host = $_SERVER[$source];
405 1
            if (array_key_exists($source, $sourceTransformations)) {
406 1
                $host = $sourceTransformations[$source]($host);
407
            }
408
        }
409
410
        // Remove port number from host
411 1
        $host = preg_replace('/:\d+$/', '', $host);
412
413 1
        return trim($host);
414
    }
415
416
    /**
417
     * Return specified header
418
     *
419
     * @param  string $name
420
     * @return string
421
     */
422 1
    public function getHeader($name)
0 ignored issues
show
Coding Style introduced by
getHeader uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
423
    {
424 1
        if (empty($this->headers)) {
425 1
            if (function_exists('apache_request_headers')) {
426
                $this->headers = apache_request_headers();
427
            } else {
428 1
                $server = $_SERVER;
429 1
                foreach ($server as $key => $value) {
430 1
                    if (0 === strpos($key, 'HTTP_')) {
431 1
                        $key = str_replace('_', '-', strtolower(substr($key, 5)));
432 1
                        $this->headers[$key] = $value;
433
                    }
434
                }
435
436 1
                if (isset($server['CONTENT_TYPE'])) {
437
                    $this->headers['content-type'] = $server['CONTENT_TYPE'];
438
                }
439
440 1
                if (isset($server['CONTENT_LENGTH'])) {
441
                    $this->headers['content-length'] = $server['CONTENT_LENGTH'];
442
                }
443
            }
444
445 1
            $this->headers = array_change_key_case($this->headers);
446
        }
447
448 1
        $name = str_replace('_', '-', strtolower($name));
449 1
        return isset($this->headers[$name]) ? $this->headers[$name] : null;
450
    }
451
452
    /**
453
     * Detect whether request method is GET
454
     *
455
     * @return boolean
456
     */
457
    public function isGet()
0 ignored issues
show
Coding Style introduced by
isGet uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
458
    {
459
        return 'get' == strtolower($_SERVER['REQUEST_METHOD']);
460
    }
461
462
    /**
463
     * Detect whether request method is POST
464
     *
465
     * @return boolean
466
     */
467
    public function isPost()
0 ignored issues
show
Coding Style introduced by
isPost uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
468
    {
469
        return 'post' == strtolower($_SERVER['REQUEST_METHOD']);
470
    }
471
472
    /**
473
     * Detect whether request method is PUT
474
     *
475
     * @return boolean
476
     */
477
    public function isPut()
0 ignored issues
show
Coding Style introduced by
isPut uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
478
    {
479
        return 'put' == strtolower($_SERVER['REQUEST_METHOD']);
480
    }
481
482
    /**
483
     * Detect whether request method is PATCH
484
     *
485
     * @return boolean
486
     */
487
    public function isPatch()
0 ignored issues
show
Coding Style introduced by
isPatch uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
488
    {
489
        return 'patch' == strtolower($_SERVER['REQUEST_METHOD']);
490
    }
491
492
    /**
493
     * Detect whether request method is DELETE
494
     *
495
     * @return boolean
496
     */
497
    public function isDelete()
0 ignored issues
show
Coding Style introduced by
isDelete uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
498
    {
499
        return 'delete' == strtolower($_SERVER['REQUEST_METHOD']);
500
    }
501
502
    /**
503
     * Detect whether request method is OPTIONS
504
     *
505
     * @return boolean
506
     */
507
    public function isOptions()
0 ignored issues
show
Coding Style introduced by
isOptions uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
508
    {
509
        return 'options' == strtolower($_SERVER['REQUEST_METHOD']);
510
    }
511
512
    /**
513
     * Detect whether request method is AJAX
514
     *
515
     * @return boolean
516
     */
517
    public function isAjax()
0 ignored issues
show
Coding Style introduced by
isAjax uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
518
    {
519
        return ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) ? true : false;
520
    }
521
522
    /**
523
     * Is CLI?
524
     *
525
     * Test to see if a request was made from the command line.
526
     *
527
     * @return  boolean
528
     */
529 4
    public function isCli()
530
    {
531 4
        return PHP_SAPI === 'cli';
532
    }
533
534
    /**
535
     * Detect whether user agent is Mobile
536
     *
537
     * @return boolean
538
     */
539 1
    public function isMobile()
0 ignored issues
show
Coding Style introduced by
isMobile uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
540
    {
541 1
        $userAgent = isset($_SERVER['USER_AGENT']) ? $_SERVER['USER_AGENT'] : null;
542 1
        if ($userAgent == null) {
543 1
            return false;
544
        }
545
546
        return preg_match('/android.+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $userAgent) || preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i', substr($userAgent, 0, 4));
547
    }
548
}
549