Passed
Branch master (d96b32)
by Sinnarasa
02:30
created

Route::__construct()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 9
rs 8.8571
cc 5
eloc 7
nc 8
nop 1
1
<?php
2
3
namespace JetFire\Routing;
4
5
/**
6
 * Class Route
7
 * @package JetFire\Routing
8
 * @method getParameters()
9
 * @method getBlock()
10
 * @method getPath()
11
 */
12
class Route
13
{
14
15
    /**
16
     * @var
17
     */
18
    private $url;
19
    /**
20
     * @var
21
     */
22
    private $name;
23
    /**
24
     * @var
25
     */
26
    private $callback;
27
    /**
28
     * @var array
29
     */
30
    private $response = [
31
        'code'    => 404,
32
        'message' => 'Not Found',
33
        'type'    => 'text/plain'
34
    ];
35
    /**
36
     * @var string
37
     */
38
    private $method;
39
    /**
40
     * @var array
41
     */
42
    private $target = [];
43
    /**
44
     * @var array
45
     */
46
    private $detail = [];
47
48
    /**
49
     * @param array $args
50
     */
51
    public function __construct($args = [])
0 ignored issues
show
Coding Style introduced by
__construct 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...
Coding Style introduced by
__construct 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...
52
    {
53
        $this->set($args);
54
        $this->method = (
55
            isset($_POST['_METHOD'])
56
            && ($_method = (isset($_POST['_METHOD']))?strtoupper($_POST['_METHOD']):'')
57
            && in_array($_method, array('PUT', 'DELETE'))
58
        ) ? $_method : $_SERVER['REQUEST_METHOD'];
1 ignored issue
show
Bug introduced by
The variable $_method does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
59
    }
60
61
    /**
62
     * @param array $args
63
     */
64
    public function set($args = [])
65
    {
66
        if (isset($args['name'])) $this->name = $args['name'];
67
        if (isset($args['callback'])) $this->callback = $args['callback'];
68
        if (isset($args['code'])) $this->response['code'] = $args['code'];
69
        if (isset($args['target'])) $this->target = $args['target'];
70
        if (isset($args['detail'])) $this->detail = $args['detail'];
71
    }
72
73
    /**
74
     * @return null
75
     */
76
    public function getUrl()
77
    {
78
        return $this->url;
79
    }
80
81
    /**
82
     * @param $url
83
     */
84
    public function setUrl($url)
85
    {
86
        $this->url = $url;
87
    }
88
89
    /**
90
     * @return null
91
     */
92
    public function getName()
93
    {
94
        return $this->name;
95
    }
96
97
    /**
98
     * @param $name
99
     */
100
    public function setName($name)
101
    {
102
        $this->name = $name;
103
    }
104
105
    /**
106
     * @return mixed
107
     */
108
    public function getCallback()
109
    {
110
        return $this->callback;
111
    }
112
113
    /**
114
     * @param $callback
115
     */
116
    public function setCallback($callback)
117
    {
118
        $this->callback = $callback;
119
    }
120
121
    /**
122
     * @param null $key
123
     * @return array
124
     */
125
    public function getResponse($key = null)
126
    {
127
        if (!is_null($key) && isset($this->response[$key]))
128
            return $this->response[$key];
129
        return $this->response;
130
    }
131
132
    /**
133
     * @param null $key
134
     * @param null $value
135
     */
136
    public function setResponse($key = null, $value = null)
137
    {
138
        if (!is_null($key) && !is_null($value))
139
            $this->response[$key] = $value;
140
        else
141
            $this->response = array_merge($this->response, $key);
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    public function getMethod()
148
    {
149
        return $this->method;
150
    }
151
152
    /**
153
     * @return array
154
     */
155
    public function getDetail()
156
    {
157
        return $this->detail;
158
    }
159
160
    /**
161
     * @param $detail
162
     */
163
    public function setDetail($detail)
164
    {
165
        $this->detail = $detail;
166
    }
167
168
    /**
169
     * @param $key
170
     * @param $value
171
     */
172
    public function addDetail($key, $value)
173
    {
174
        $this->detail[$key] = $value;
175
    }
176
177
    /**
178
     * @param null $key
179
     * @return array|string
180
     */
181
    public function getTarget($key = null)
182
    {
183
        if (!is_null($key))
184
            return isset($this->target[$key]) ? $this->target[$key] : '';
185
        return empty($this->target) ? '' : $this->target;
186
    }
187
188
    /**
189
     * @param $target
190
     * @return mixed
191
     */
192
    public function setTarget($target = [])
193
    {
194
        $this->target = $target;
195
    }
196
197
    /**
198
     * @param null $key
199
     * @return bool
200
     */
201
    public function hasTarget($key = null)
202
    {
203
        if (!is_null($key))
204
            return isset($this->target[$key]) ? true : false;
205
        return empty($this->target) ? false : true;
206
    }
207
208
    /**
209
     * @return array
210
     */
211
    public function getData(){
212
        return (isset($this->getDetail()['data']) && is_array($this->getDetail()['data']))?$this->getDetail()['data']:[];
213
    }
214
215
    /**
216
     * @param $name
217
     * @param $arguments
218
     * @return null
219
     */
220
    public function __call($name, $arguments)
221
    {
222
        if (substr($name, 0, 3) === "get") {
223
            $key = strtolower(str_replace('get', '', $name));
224
            return isset($this->detail[$key]) ? $this->detail[$key] : '';
225
        } elseif (substr($name, 0, 3) === "set") {
226
            $key = strtolower(str_replace('set', '', $name));
227
            $this->detail[$key] = $arguments[0];
228
        }
229
        return '';
230
    }
231
}