Complex classes like Meta 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 Meta, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Meta |
||
24 | { |
||
25 | use Options; |
||
26 | |||
27 | /** |
||
28 | * @var string full path to file |
||
29 | */ |
||
30 | protected $file; |
||
31 | |||
32 | /** |
||
33 | * @var integer cache TTL |
||
34 | */ |
||
35 | protected $cache = 0; |
||
36 | |||
37 | /** |
||
38 | * @var array list of Accept |
||
39 | */ |
||
40 | protected $accept = []; |
||
41 | |||
42 | /** |
||
43 | * @var array list of Acl |
||
44 | */ |
||
45 | protected $acl = []; |
||
46 | |||
47 | /** |
||
48 | * @var array list of HTTP methods |
||
49 | */ |
||
50 | protected $method = []; |
||
51 | |||
52 | /** |
||
53 | * @var array described params |
||
54 | */ |
||
55 | protected $params = []; |
||
56 | |||
57 | /** |
||
58 | * @var string privilege |
||
59 | */ |
||
60 | protected $privilege; |
||
61 | |||
62 | /** |
||
63 | * @var array routers |
||
64 | */ |
||
65 | protected $route = []; |
||
66 | |||
67 | /** |
||
68 | * @var array default values of params |
||
69 | */ |
||
70 | protected $values = []; |
||
71 | |||
72 | /** |
||
73 | * Constructor of Reflection |
||
74 | * |
||
75 | * @param string $file |
||
76 | */ |
||
77 | 602 | public function __construct($file) |
|
81 | |||
82 | /** |
||
83 | * Set state required for working with var_export (used inside PHP File cache) |
||
84 | * |
||
85 | * @param array $array |
||
86 | * |
||
87 | * @return Meta |
||
88 | */ |
||
89 | public static function __set_state($array) |
||
97 | |||
98 | /** |
||
99 | * Process to get reflection from file |
||
100 | * |
||
101 | * @return void |
||
102 | * @throws ComponentException |
||
103 | * @throws \ReflectionException |
||
104 | */ |
||
105 | 602 | public function process(): void |
|
143 | |||
144 | /** |
||
145 | * Process request params |
||
146 | * |
||
147 | * - type conversion |
||
148 | * - set default value |
||
149 | * |
||
150 | * @param array $requestParams |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | 40 | public function params($requestParams): array |
|
155 | { |
||
156 | // apply type and default value for request params |
||
157 | 40 | $params = []; |
|
158 | 40 | foreach ($this->params as $param => $type) { |
|
159 | 6 | if (isset($requestParams[$param])) { |
|
160 | 6 | switch ($type) { |
|
161 | 6 | case 'bool': |
|
162 | 6 | case 'boolean': |
|
163 | $params[] = (bool)$requestParams[$param]; |
||
164 | break; |
||
165 | 6 | case 'int': |
|
166 | 6 | case 'integer': |
|
167 | 3 | $params[] = (int)$requestParams[$param]; |
|
168 | 3 | break; |
|
169 | 6 | case 'float': |
|
170 | $params[] = (float)$requestParams[$param]; |
||
171 | break; |
||
172 | 6 | case 'string': |
|
173 | $params[] = (string)$requestParams[$param]; |
||
174 | break; |
||
175 | 6 | case 'array': |
|
176 | $params[] = (array)$requestParams[$param]; |
||
177 | break; |
||
178 | default: |
||
179 | 6 | $params[] = $requestParams[$param]; |
|
180 | 6 | break; |
|
181 | } |
||
182 | 2 | } elseif (isset($this->values[$param])) { |
|
183 | $params[] = $this->values[$param]; |
||
184 | } else { |
||
185 | 6 | $params[] = null; |
|
186 | } |
||
187 | } |
||
188 | 40 | return $params; |
|
189 | } |
||
190 | |||
191 | /** |
||
192 | * Get path to file |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | public function getFile(): string |
||
200 | |||
201 | /** |
||
202 | * Get Cache TTL |
||
203 | * |
||
204 | * @return integer |
||
205 | */ |
||
206 | 41 | public function getCache(): int |
|
210 | |||
211 | /** |
||
212 | * Set Cache TTL |
||
213 | * |
||
214 | * @param string $ttl |
||
215 | * |
||
216 | * @return void |
||
217 | */ |
||
218 | 2 | public function setCache($ttl): void |
|
222 | |||
223 | /** |
||
224 | * Prepare Cache |
||
225 | * |
||
226 | * @param string $cache |
||
227 | * |
||
228 | * @return integer |
||
229 | */ |
||
230 | 2 | protected function prepareCache($cache): int |
|
231 | { |
||
232 | 2 | $num = (int)$cache; |
|
233 | 2 | $time = 'min'; |
|
234 | |||
235 | 2 | if ($pos = strpos($cache, ' ')) { |
|
236 | 2 | $time = substr($cache, $pos); |
|
237 | } |
||
238 | |||
239 | 2 | switch ($time) { |
|
240 | 2 | case 'day': |
|
241 | 2 | case 'days': |
|
242 | return $num * 86400; |
||
243 | 2 | case 'hour': |
|
244 | 2 | case 'hours': |
|
245 | return $num * 3600; |
||
246 | 2 | case 'min': |
|
247 | default: |
||
248 | 2 | return $num * 60; |
|
249 | } |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Get accepted type |
||
254 | * |
||
255 | * @return array|null |
||
256 | */ |
||
257 | 40 | public function getAccept(): ?array |
|
261 | |||
262 | /** |
||
263 | * Set accepted types |
||
264 | * |
||
265 | * @param string $accept |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | public function setAccept($accept): void |
||
284 | |||
285 | /** |
||
286 | * Get Acl privileges |
||
287 | * |
||
288 | * @return array|null |
||
289 | */ |
||
290 | 1 | public function getAcl(): ?array |
|
294 | |||
295 | /** |
||
296 | * Set Acl privileges |
||
297 | * |
||
298 | * @param string $acl |
||
299 | * |
||
300 | * @return void |
||
301 | */ |
||
302 | 2 | public function setAcl($acl): void |
|
306 | |||
307 | /** |
||
308 | * Get HTTP Method |
||
309 | * |
||
310 | * @return array|null |
||
311 | */ |
||
312 | 41 | public function getMethod(): ?array |
|
316 | |||
317 | /** |
||
318 | * Set HTTP Method |
||
319 | * |
||
320 | * @param string $method |
||
321 | * |
||
322 | * @return void |
||
323 | */ |
||
324 | 2 | public function setMethod($method): void |
|
328 | |||
329 | /** |
||
330 | * Get all params |
||
331 | * |
||
332 | * @return array |
||
333 | */ |
||
334 | 602 | public function getParams(): array |
|
338 | |||
339 | /** |
||
340 | * Set param types |
||
341 | * |
||
342 | * @param string $param |
||
343 | * |
||
344 | * @return void |
||
345 | */ |
||
346 | 602 | public function setParam($param): void |
|
358 | |||
359 | /** |
||
360 | * Get Privilege fo ACL |
||
361 | * |
||
362 | * @return string|null |
||
363 | */ |
||
364 | 41 | public function getPrivilege(): ?string |
|
368 | |||
369 | /** |
||
370 | * Set Privilege fo ACL allow only one privilege |
||
371 | * |
||
372 | * @param string $privilege |
||
373 | * |
||
374 | * @return void |
||
375 | */ |
||
376 | 2 | public function setPrivilege($privilege): void |
|
380 | |||
381 | /** |
||
382 | * Get Route |
||
383 | * |
||
384 | * @return array|null |
||
385 | */ |
||
386 | 602 | public function getRoute(): ?array |
|
390 | |||
391 | /** |
||
392 | * Set Route |
||
393 | * |
||
394 | * @param string $route |
||
395 | * |
||
396 | * @return void |
||
397 | */ |
||
398 | 602 | public function setRoute($route): void |
|
402 | |||
403 | /** |
||
404 | * Init Route |
||
405 | * |
||
406 | * @return void |
||
407 | */ |
||
408 | 602 | protected function initRoute(): void |
|
414 | |||
415 | /** |
||
416 | * Prepare Route pattern |
||
417 | * |
||
418 | * @param string $route |
||
419 | * |
||
420 | * @return string |
||
421 | */ |
||
422 | 602 | protected function prepareRoutePattern($route): string |
|
448 | } |
||
449 |