Completed
Push — master ( 4be8e1...bb1ab9 )
by Sinnarasa
05:48
created

Route   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 222
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 42
c 6
b 0
f 2
lcom 1
cbo 0
dl 0
loc 222
rs 8.295

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
B set() 0 8 6
A getUrl() 0 4 1
A setUrl() 0 4 1
A getName() 0 4 1
A setName() 0 4 1
A getCallback() 0 4 1
A setCallback() 0 4 1
A getResponse() 0 6 3
A setResponse() 0 7 3
A getMethod() 0 4 1
A getDetail() 0 4 1
A setDetail() 0 4 1
A addDetail() 0 4 1
A getTarget() 0 6 4
A setTarget() 0 4 1
A hasTarget() 0 6 4
A getData() 0 3 3
A getCollection() 0 3 1
A __call() 0 11 4

How to fix   Complexity   

Complex Class

Complex classes like Route often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Route, and based on these observations, apply Extract Interface, too.

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