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 |
||
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 = []) |
||
59 | |||
60 | /** |
||
61 | * @param array $args |
||
62 | */ |
||
63 | public function set($args = []) |
||
71 | |||
72 | /** |
||
73 | * @return null |
||
74 | */ |
||
75 | public function getUrl() |
||
79 | |||
80 | /** |
||
81 | * @param $url |
||
82 | */ |
||
83 | public function setUrl($url) |
||
87 | |||
88 | /** |
||
89 | * @return null |
||
90 | */ |
||
91 | public function getName() |
||
95 | |||
96 | /** |
||
97 | * @param $name |
||
98 | */ |
||
99 | public function setName($name) |
||
103 | |||
104 | /** |
||
105 | * @return mixed |
||
106 | */ |
||
107 | public function getCallback() |
||
111 | |||
112 | /** |
||
113 | * @param $callback |
||
114 | */ |
||
115 | public function setCallback($callback) |
||
119 | |||
120 | /** |
||
121 | * @param null $key |
||
122 | * @return array|string |
||
123 | */ |
||
124 | public function getResponse($key = null) |
||
130 | |||
131 | /** |
||
132 | * @param null $key |
||
133 | * @param null $value |
||
134 | */ |
||
135 | public function setResponse($key = null, $value = null) |
||
142 | |||
143 | /** |
||
144 | * @return array |
||
145 | */ |
||
146 | public function getMethod() |
||
150 | |||
151 | /** |
||
152 | * @return array |
||
153 | */ |
||
154 | public function getDetail() |
||
158 | |||
159 | /** |
||
160 | * @param $detail |
||
161 | */ |
||
162 | public function setDetail($detail) |
||
166 | |||
167 | /** |
||
168 | * @param $key |
||
169 | * @param $value |
||
170 | */ |
||
171 | public function addDetail($key, $value) |
||
175 | |||
176 | /** |
||
177 | * @param null $key |
||
178 | * @return array|string |
||
179 | */ |
||
180 | public function getTarget($key = null) |
||
186 | |||
187 | /** |
||
188 | * @param $target |
||
189 | * @return mixed |
||
190 | */ |
||
191 | public function setTarget($target = []) |
||
195 | |||
196 | /** |
||
197 | * @param null $key |
||
198 | * @return bool |
||
199 | */ |
||
200 | public function hasTarget($key = null) |
||
206 | |||
207 | /** |
||
208 | * @return array |
||
209 | */ |
||
210 | public function getData(){ |
||
213 | |||
214 | public function getCollection(){ |
||
217 | /** |
||
218 | * @param $name |
||
219 | * @param $arguments |
||
220 | * @return null |
||
221 | */ |
||
222 | public function __call($name, $arguments) |
||
233 | } |
||
234 |
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: