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 | 715 | 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 | 715 | public function process() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Process request params |
||
| 144 | * |
||
| 145 | * - type conversion |
||
| 146 | * - set default value |
||
| 147 | * |
||
| 148 | * @param array $requestParams |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | 13 | public function params($requestParams) |
|
| 152 | { |
||
| 153 | // apply type and default value for request params |
||
| 154 | 13 | $params = []; |
|
| 155 | 13 | foreach ($this->params as $param => $type) { |
|
| 156 | 3 | if (isset($requestParams[$param])) { |
|
| 157 | switch ($type) { |
||
| 158 | 3 | case 'bool': |
|
| 159 | 3 | case 'boolean': |
|
| 160 | $params[] = (bool)$requestParams[$param]; |
||
| 161 | break; |
||
| 162 | 3 | case 'int': |
|
| 163 | 3 | case 'integer': |
|
| 164 | 3 | $params[] = (int)$requestParams[$param]; |
|
| 165 | 3 | break; |
|
| 166 | 3 | case 'float': |
|
| 167 | $params[] = (float)$requestParams[$param]; |
||
| 168 | break; |
||
| 169 | 3 | case 'string': |
|
| 170 | $params[] = (string)$requestParams[$param]; |
||
| 171 | break; |
||
| 172 | 3 | case 'array': |
|
| 173 | $params[] = (array)$requestParams[$param]; |
||
| 174 | break; |
||
| 175 | default: |
||
| 176 | 3 | $params[] = $requestParams[$param]; |
|
| 177 | 3 | break; |
|
| 178 | } |
||
| 179 | } elseif (isset($this->values[$param])) { |
||
| 180 | $params[] = $this->values[$param]; |
||
| 181 | } else { |
||
| 182 | 3 | $params[] = null; |
|
| 183 | } |
||
| 184 | } |
||
| 185 | 13 | return $params; |
|
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get path to file |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function getFile() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get Cache TTL |
||
| 200 | * |
||
| 201 | * @return integer |
||
| 202 | */ |
||
| 203 | 14 | public function getCache() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Set Cache TTL |
||
| 210 | * |
||
| 211 | * @param string $ttl |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | 2 | public function setCache($ttl) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Prepare Cache |
||
| 221 | * |
||
| 222 | * @param string $cache |
||
| 223 | * @return integer |
||
| 224 | */ |
||
| 225 | 2 | protected function prepareCache($cache) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Get accepted type |
||
| 249 | * |
||
| 250 | * @return array|null |
||
| 251 | */ |
||
| 252 | public function getAccept() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Set accepted types |
||
| 259 | * |
||
| 260 | * @param string $accept |
||
| 261 | * @return void |
||
| 262 | */ |
||
| 263 | public function setAccept($accept) |
||
| 264 | { |
||
| 265 | // allow accept map |
||
| 266 | $acceptMap = [ |
||
| 267 | 'ANY' => Request::TYPE_ANY, |
||
| 268 | 'HTML' => Request::TYPE_HTML, |
||
| 269 | 'JSON' => Request::TYPE_JSON |
||
| 270 | ]; |
||
| 271 | |||
| 272 | $accept = strtoupper($accept); |
||
| 273 | |||
| 274 | if (isset($acceptMap[$accept])) { |
||
| 275 | $this->accept[] = $acceptMap[$accept]; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get Acl privileges |
||
| 281 | * |
||
| 282 | * @return array|null |
||
| 283 | */ |
||
| 284 | 1 | public function getAcl() |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Set Acl privileges |
||
| 291 | * |
||
| 292 | * @param string $acl |
||
| 293 | * @return void |
||
| 294 | */ |
||
| 295 | 2 | public function setAcl($acl) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Get HTTP Method |
||
| 302 | * |
||
| 303 | * @return array|null |
||
| 304 | */ |
||
| 305 | 14 | public function getMethod() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Set HTTP Method |
||
| 312 | * |
||
| 313 | * @param string $method |
||
| 314 | * @return void |
||
| 315 | */ |
||
| 316 | 2 | public function setMethod($method) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Get all params |
||
| 323 | * |
||
| 324 | * @return array |
||
| 325 | */ |
||
| 326 | 713 | public function getParams() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Set param types |
||
| 333 | * |
||
| 334 | * @param string $param |
||
| 335 | * @return void |
||
| 336 | */ |
||
| 337 | 713 | public function setParam($param) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Get Privilege fo ACL |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | 14 | public function getPrivilege() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Set Privilege fo ACL allow only one privilege |
||
| 362 | * |
||
| 363 | * @param string $privilege |
||
| 364 | * @return void |
||
| 365 | */ |
||
| 366 | 2 | public function setPrivilege($privilege) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Get Route |
||
| 373 | * |
||
| 374 | * @return array|null |
||
| 375 | */ |
||
| 376 | 713 | public function getRoute() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Set Route |
||
| 383 | * |
||
| 384 | * @param string $route |
||
| 385 | * @return void |
||
| 386 | */ |
||
| 387 | 713 | public function setRoute($route) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Init Route |
||
| 394 | * |
||
| 395 | * @return void |
||
| 396 | */ |
||
| 397 | 715 | protected function initRoute() |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Prepare Route pattern |
||
| 406 | * |
||
| 407 | * @param string $route |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | 713 | protected function prepareRoutePattern($route) |
|
| 436 | } |
||
| 437 |