Complex classes like Reflection 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 Reflection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Reflection |
||
| 25 | { |
||
| 26 | use Options; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string full path to file |
||
| 30 | */ |
||
| 31 | protected $file; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var integer cache TTL |
||
| 35 | */ |
||
| 36 | protected $cache = 0; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array list of Accept |
||
| 40 | */ |
||
| 41 | protected $accept = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array list of Acl |
||
| 45 | */ |
||
| 46 | protected $acl = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array list of HTTP methods |
||
| 50 | */ |
||
| 51 | protected $method = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array described params |
||
| 55 | */ |
||
| 56 | protected $params = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string privilege |
||
| 60 | */ |
||
| 61 | protected $privilege; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array routers |
||
| 65 | */ |
||
| 66 | protected $route = array(); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array default values of params |
||
| 70 | */ |
||
| 71 | protected $values = array(); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Constructor of Reflection |
||
| 75 | * |
||
| 76 | * @param string $file |
||
| 77 | */ |
||
| 78 | 632 | public function __construct($file) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Set state required for working with var_export (used inside PHP File cache) |
||
| 85 | * |
||
| 86 | * @param $array |
||
| 87 | * @return Reflection |
||
| 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 | */ |
||
| 104 | 632 | public function process() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Process request params |
||
| 148 | * |
||
| 149 | * - type conversion |
||
| 150 | * - set default value |
||
| 151 | * |
||
| 152 | * @param array $requestParams |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | 4 | public function params($requestParams) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Get path to file |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function getFile() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get Cache TTL |
||
| 204 | * |
||
| 205 | * @return integer |
||
| 206 | */ |
||
| 207 | 5 | public function getCache() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Set Cache TTL |
||
| 214 | * |
||
| 215 | * @param string $ttl |
||
| 216 | * @return void |
||
| 217 | */ |
||
| 218 | 2 | public function setCache($ttl) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Prepare Cache |
||
| 225 | * |
||
| 226 | * @param string $cache |
||
| 227 | * @return integer |
||
| 228 | */ |
||
| 229 | 2 | protected function prepareCache($cache) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Get accepted type |
||
| 248 | * |
||
| 249 | * @return array|null |
||
| 250 | */ |
||
| 251 | public function getAccept() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Set accepted types |
||
| 258 | * |
||
| 259 | * @param string $accept |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | public function setAccept($accept) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get Acl privileges |
||
| 280 | * |
||
| 281 | * @return array|null |
||
| 282 | */ |
||
| 283 | 1 | public function getAcl() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Set Acl privileges |
||
| 290 | * |
||
| 291 | * @param string $acl |
||
| 292 | * @return void |
||
| 293 | */ |
||
| 294 | 2 | public function setAcl($acl) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Get HTTP Method |
||
| 301 | * |
||
| 302 | * @return array|null |
||
| 303 | */ |
||
| 304 | 5 | public function getMethod() |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Set HTTP Method |
||
| 311 | * |
||
| 312 | * @param string $method |
||
| 313 | * @return void |
||
| 314 | */ |
||
| 315 | 2 | public function setMethod($method) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Get all params |
||
| 322 | * |
||
| 323 | * @return array |
||
| 324 | */ |
||
| 325 | 630 | public function getParams() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Set param types |
||
| 332 | * |
||
| 333 | * @param string $param |
||
| 334 | * @return void |
||
| 335 | */ |
||
| 336 | 630 | public function setParam($param) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Get Privilege fo ACL |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | 5 | public function getPrivilege() |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Set Privilege fo ACL allow only one privilege |
||
| 361 | * |
||
| 362 | * @param string $privilege |
||
| 363 | * @return void |
||
| 364 | */ |
||
| 365 | 2 | public function setPrivilege($privilege) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Get Route |
||
| 372 | * |
||
| 373 | * @return array|null |
||
| 374 | */ |
||
| 375 | 630 | public function getRoute() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Set Route |
||
| 382 | * |
||
| 383 | * @param string $route |
||
| 384 | * @return void |
||
| 385 | */ |
||
| 386 | 630 | public function setRoute($route) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Init Route |
||
| 393 | * |
||
| 394 | * @return void |
||
| 395 | */ |
||
| 396 | 632 | protected function initRoute() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Prepare Route pattern |
||
| 405 | * |
||
| 406 | * @param string $route |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | 630 | protected function prepareRoutePattern($route) |
|
| 435 | } |
||
| 436 |