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 | 690 | public function __construct($file) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Set state required for working with var_export (used inside PHP File cache) |
||
| 84 | * |
||
| 85 | * @param $array |
||
| 86 | * @return Meta |
||
| 87 | */ |
||
| 88 | public static function __set_state($array) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Process to get reflection from file |
||
| 99 | * |
||
| 100 | * @return void |
||
| 101 | * @throws ComponentException |
||
| 102 | */ |
||
| 103 | 690 | public function process() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Process request params |
||
| 147 | * |
||
| 148 | * - type conversion |
||
| 149 | * - set default value |
||
| 150 | * |
||
| 151 | * @param array $requestParams |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | 8 | public function params($requestParams) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Get path to file |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function getFile() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get Cache TTL |
||
| 203 | * |
||
| 204 | * @return integer |
||
| 205 | */ |
||
| 206 | 9 | public function getCache() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Set Cache TTL |
||
| 213 | * |
||
| 214 | * @param string $ttl |
||
| 215 | * @return void |
||
| 216 | */ |
||
| 217 | 2 | public function setCache($ttl) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Prepare Cache |
||
| 224 | * |
||
| 225 | * @param string $cache |
||
| 226 | * @return integer |
||
| 227 | */ |
||
| 228 | 2 | protected function prepareCache($cache) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Get accepted type |
||
| 252 | * |
||
| 253 | * @return array|null |
||
| 254 | */ |
||
| 255 | public function getAccept() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Set accepted types |
||
| 262 | * |
||
| 263 | * @param string $accept |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | public function setAccept($accept) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get Acl privileges |
||
| 284 | * |
||
| 285 | * @return array|null |
||
| 286 | */ |
||
| 287 | 1 | public function getAcl() |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Set Acl privileges |
||
| 294 | * |
||
| 295 | * @param string $acl |
||
| 296 | * @return void |
||
| 297 | */ |
||
| 298 | 2 | public function setAcl($acl) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Get HTTP Method |
||
| 305 | * |
||
| 306 | * @return array|null |
||
| 307 | */ |
||
| 308 | 9 | public function getMethod() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Set HTTP Method |
||
| 315 | * |
||
| 316 | * @param string $method |
||
| 317 | * @return void |
||
| 318 | */ |
||
| 319 | 2 | public function setMethod($method) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Get all params |
||
| 326 | * |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | 688 | public function getParams() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Set param types |
||
| 336 | * |
||
| 337 | * @param string $param |
||
| 338 | * @return void |
||
| 339 | */ |
||
| 340 | 688 | public function setParam($param) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Get Privilege fo ACL |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | 9 | public function getPrivilege() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Set Privilege fo ACL allow only one privilege |
||
| 365 | * |
||
| 366 | * @param string $privilege |
||
| 367 | * @return void |
||
| 368 | */ |
||
| 369 | 2 | public function setPrivilege($privilege) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Get Route |
||
| 376 | * |
||
| 377 | * @return array|null |
||
| 378 | */ |
||
| 379 | 688 | public function getRoute() |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Set Route |
||
| 386 | * |
||
| 387 | * @param string $route |
||
| 388 | * @return void |
||
| 389 | */ |
||
| 390 | 688 | public function setRoute($route) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Init Route |
||
| 397 | * |
||
| 398 | * @return void |
||
| 399 | */ |
||
| 400 | 690 | protected function initRoute() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Prepare Route pattern |
||
| 409 | * |
||
| 410 | * @param string $route |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | 688 | protected function prepareRoutePattern($route) |
|
| 439 | } |
||
| 440 |