Passed
Push — master ( 90b83d...6af3d7 )
by Oleg
03:48
created

Request::getOption()   C

Complexity

Conditions 16
Paths 39

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 5.0151
cc 16
eloc 18
nc 39
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php /** MicroRequest */
2
3
namespace Micro\Web;
4
5
/**
6
 * Request class file.
7
 *
8
 * @author Oleg Lunegov <[email protected]>
9
 * @link https://github.com/lugnsk/micro
10
 * @copyright Copyright &copy; 2013 Oleg Lunegov
11
 * @license /LICENSE
12
 * @package Micro
13
 * @subpackage Web
14
 * @version 1.0
15
 * @since 1.0
16
 */
17
class Request implements IRequest
18
{
19
    /** @var bool $cli Is running as CLI */
20
    protected $cli;
21
    /** @var array $data Data from request */
22
    protected $data;
23
24
25
    /**
26
     * Constructor Request
27
     *
28
     * @access public
29
     *
30
     * @result void
31
     */
32
    public function __construct()
33
    {
34
        $this->cli = PHP_SAPI === 'cli';
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function isCli()
41
    {
42
        return $this->cli;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    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...
49
    {
50
        return !empty($_SERVER['HTTP_X_REQUEST_WITH']) && $_SERVER['HTTP_X_REQUEST_WITH'] === 'XMLHttpRequest';
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function getMethod()
0 ignored issues
show
Coding Style introduced by
getMethod 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...
57
    {
58
        return $_SERVER['REQUEST_METHOD'];
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function getUserIP()
0 ignored issues
show
Coding Style introduced by
getUserIP 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...
65
    {
66
        return !empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function getBrowser($agent = null)
0 ignored issues
show
Coding Style introduced by
getBrowser 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...
73
    {
74
        return get_browser($agent ?: $_SERVER['HTTP_USER_AGENT'], true);
75
    }
76
77
    /**
78
     * Get arguments from command line
79
     *
80
     * @access public
81
     *
82
     * @param string $char -a .. -z option char
83
     * @param string $name --optionName_string
84
     * @param bool|null $required Required value?
85
     *
86
     * @return mixed
87
     */
88
    public function getOption($char = '', $name = '', $required = null)
89
    {
90
        if (!$char && !$name) {
91
            return false;
92
        }
93
94
        if ($char && (1 < strlen($char) || 1 !== preg_match('/^\w$/', $char))) {
95
            return false;
96
        }
97
98
        if ($name && (1 !== preg_match('/^\w+$/', $name))) {
99
            return false;
100
        }
101
102
        switch ($required) {
103
            case true:
104
                $char = $char ? $char . ':' : $char;
105
                $name = $name ? $name . ':' : $name;
106
                break;
107
            case false:
108
                $char = $char ? $char . '::' : $char;
109
                $name = $name ? $name . '::' : $name;
110
                break;
111
        }
112
113
        $argv = ($opts = getopt($char, [$name])) ? array_shift($opts) : [];
114
115
        return is_array($argv) ? array_shift($argv) : $argv;
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121
    public function getFiles($className = '\Micro\Web\Uploader')
0 ignored issues
show
Coding Style introduced by
getFiles uses the super-global variable $_FILES 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...
122
    {
123
        if (!is_array($_FILES)) {
124
            return false;
125
        }
126
127
        return new $className($_FILES);
128
    }
129
130
    /**
131
     * @inheritdoc
132
     */
133
    public function getStorage($name)
0 ignored issues
show
Coding Style introduced by
getStorage uses the super-global variable $GLOBALS 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...
134
    {
135
        return $GLOBALS[$name];
136
    }
137
138
    /**
139
     * @inheritdoc
140
     */
141
    public function setStorage($name, array $data = [])
0 ignored issues
show
Coding Style introduced by
setStorage uses the super-global variable $GLOBALS 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...
142
    {
143
        $GLOBALS[$name] = $data;
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149
    public function query($name)
150
    {
151
        return $this->getVar($name, '_GET');
152
    }
153
154
    /**
155
     * @inheritdoc
156
     */
157
    public function getVar($name, $storage)
0 ignored issues
show
Coding Style introduced by
getVar uses the super-global variable $GLOBALS 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...
158
    {
159
        return array_key_exists($name, $GLOBALS[$storage]) ? $GLOBALS[$storage][$name] : null;
160
    }
161
162
    /**
163
     * @inheritdoc
164
     */
165
    public function post($name)
166
    {
167
        return $this->getVar($name, '_POST');
168
    }
169
170
    /**
171
     * @inheritdoc
172
     */
173
    public function cookie($name)
174
    {
175
        return $this->getVar($name, '_COOKIE');
176
    }
177
178
    /**
179
     * @inheritdoc
180
     */
181
    public function session($name)
182
    {
183
        return $this->getVar($name, '_SESSION');
184
    }
185
186
    /**
187
     * Get value by key from server storage
188
     *
189
     * @access public
190
     *
191
     * @param string $name Key name
192
     *
193
     * @return bool
194
     */
195
    public function server($name)
196
    {
197
        return $this->getVar($name, '_SERVER');
198
    }
199
200
    /**
201
     * @inheritdoc
202
     */
203
    public function setQuery($name, $value)
204
    {
205
        $this->setVar($name, $value, '_GET');
206
    }
207
208
    /**
209
     * @inheritdoc
210
     */
211
    public function setVar($name, $value, $storage)
0 ignored issues
show
Coding Style introduced by
setVar uses the super-global variable $GLOBALS 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...
212
    {
213
        $GLOBALS[$storage][$name] = $value;
214
    }
215
216
    /**
217
     * @inheritdoc
218
     */
219
    public function setPost($name, $value)
220
    {
221
        $this->setVar($name, $value, '_POST');
222
    }
223
224
    /**
225
     * @inheritdoc
226
     */
227
    public function setCookie($name, $value)
228
    {
229
        $this->setVar($name, $value, '_COOKIE');
230
    }
231
232
    /**
233
     * @inheritdoc
234
     */
235
    public function setSession($name, $value)
236
    {
237
        $this->setVar($name, $value, '_SESSION');
238
    }
239
240
    /**
241
     * @inheritdoc
242
     */
243
    public function unsetQuery($name)
244
    {
245
        $this->unsetVar($name, '_GET');
246
    }
247
248
    /**
249
     * @inheritdoc
250
     */
251
    public function unsetVar($name, $storage)
0 ignored issues
show
Coding Style introduced by
unsetVar uses the super-global variable $GLOBALS 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...
252
    {
253
        unset($GLOBALS[$storage][$name]);
254
    }
255
256
    /**
257
     * @inheritdoc
258
     */
259
    public function unsetPost($name)
260
    {
261
        $this->unsetVar($name, '_POST');
262
    }
263
264
    /**
265
     * @inheritdoc
266
     */
267
    public function unsetSession($name)
268
    {
269
        $this->unsetVar($name, '_SESSION');
270
    }
271
272
    /**
273
     * @inheritdoc
274
     */
275
    public function getRequestPayload()
276
    {
277
        return file_get_contents('php://input');
278
    }
279
}
280