Complex classes like ApiRoute 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 ApiRoute, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class ApiRoute extends ApiRouteSpec implements IRouter |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var callable[] |
||
25 | */ |
||
26 | public $onMatch; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $presenter; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | private $actions = [ |
||
37 | 'POST' => FALSE, |
||
38 | 'GET' => FALSE, |
||
39 | 'PUT' => FALSE, |
||
40 | 'DELETE' => FALSE |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | private $default_actions = [ |
||
47 | 'POST' => 'create', |
||
48 | 'GET' => 'read', |
||
49 | 'PUT' => 'update', |
||
50 | 'DELETE' => 'delete' |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | private $formats = [ |
||
57 | 'json' => 'application/json', |
||
58 | 'xml' => 'application/xml' |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | private $placeholder_order = []; |
||
65 | |||
66 | |||
67 | /** |
||
68 | * @param mixed $data |
||
69 | * @param string $presenter |
||
70 | * @param array $data |
||
71 | */ |
||
72 | public function __construct($path, $presenter = NULL, array $data = []) |
||
110 | |||
111 | |||
112 | /** |
||
113 | * @param string $presenter |
||
114 | * @return void |
||
115 | */ |
||
116 | public function setPresenter($presenter) |
||
120 | |||
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getPresenter() |
||
129 | |||
130 | |||
131 | /** |
||
132 | * @param string $action |
||
133 | * @param string $method |
||
134 | * @return void |
||
135 | */ |
||
136 | public function setAction($action, $method = NULL) { |
||
147 | |||
148 | |||
149 | /** |
||
150 | * @param string $string |
||
151 | * @return string |
||
152 | */ |
||
153 | private function prepareForMatch($string) |
||
157 | |||
158 | |||
159 | /** |
||
160 | * Get all parameters from url mask |
||
161 | * @return array |
||
162 | */ |
||
163 | public function getPlacehodlerParameters() |
||
177 | |||
178 | |||
179 | /** |
||
180 | * Get required parameters from url mask |
||
181 | * @return array |
||
182 | */ |
||
183 | public function getRequiredParams() |
||
200 | |||
201 | |||
202 | /** |
||
203 | * @param Nette\Http\IRequest $httpRequest |
||
204 | * @return void |
||
205 | */ |
||
206 | public function resolveFormat(Nette\Http\IRequest $httpRequest) |
||
224 | |||
225 | |||
226 | /** |
||
227 | * @return string |
||
228 | */ |
||
229 | public function getFormatFull() |
||
233 | |||
234 | /** |
||
235 | * @return array |
||
236 | */ |
||
237 | public function getMethods() |
||
241 | |||
242 | |||
243 | /** |
||
244 | * @param Nette\Http\IRequest $request |
||
245 | * @return string |
||
246 | */ |
||
247 | public function resolveMethod(Nette\Http\IRequest $request) { |
||
260 | |||
261 | |||
262 | /******************************************************************************** |
||
263 | * Interface IRouter * |
||
264 | ********************************************************************************/ |
||
265 | |||
266 | |||
267 | /** |
||
268 | * Maps HTTP request to a Request object. |
||
269 | * @return Request|NULL |
||
270 | */ |
||
271 | public function match(Nette\Http\IRequest $httpRequest) |
||
371 | |||
372 | |||
373 | /** |
||
374 | * Constructs absolute URL from Request object. |
||
375 | * @return string|NULL |
||
376 | */ |
||
377 | public function constructUrl(Request $request, Nette\Http\Url $url) |
||
423 | |||
424 | } |
||
425 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: