Complex classes like Controller 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 Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class Controller implements \JsonSerializable |
||
| 44 | { |
||
| 45 | use Helper; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $module; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $controller; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $template; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $file; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var Reflection |
||
| 69 | */ |
||
| 70 | protected $reflection; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var Data |
||
| 74 | */ |
||
| 75 | protected $data; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * One of HTML, JSON or empty string |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $render = 'HTML'; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Constructor of Statement |
||
| 85 | * |
||
| 86 | * @param string $module |
||
| 87 | * @param string $controller |
||
| 88 | */ |
||
| 89 | 632 | public function __construct($module, $controller) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Check `Privilege` |
||
| 101 | * |
||
| 102 | * @throws ForbiddenException |
||
| 103 | */ |
||
| 104 | 5 | public function checkPrivilege() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Check `Method` |
||
| 115 | * |
||
| 116 | * @throws NotAllowedException |
||
| 117 | */ |
||
| 118 | 5 | public function checkMethod() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Check `Accept` |
||
| 129 | * |
||
| 130 | * @throws NotAcceptableException |
||
| 131 | */ |
||
| 132 | 5 | public function checkAccept() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * __invoke |
||
| 177 | * |
||
| 178 | * @param array $params |
||
| 179 | * @return Data |
||
| 180 | * @throws ControllerException |
||
| 181 | */ |
||
| 182 | 4 | public function run($params = []) // : array |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Setup controller file |
||
| 245 | * |
||
| 246 | * @return void |
||
| 247 | * @throws ControllerException |
||
| 248 | */ |
||
| 249 | 632 | protected function setFile() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Get controller file path |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | 632 | protected function getFile() // : string |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Retrieve reflection for anonymous function |
||
| 275 | * @return Reflection |
||
| 276 | * @throws \Bluz\Common\Exception\ComponentException |
||
| 277 | */ |
||
| 278 | 632 | protected function setReflection() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Get Reflection |
||
| 293 | * @return Reflection |
||
| 294 | */ |
||
| 295 | 632 | public function getReflection() // : Reflection |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Assign key/value pair to Data object |
||
| 305 | * @param string $key |
||
| 306 | * @param mixed $value |
||
| 307 | * @return Controller |
||
| 308 | */ |
||
| 309 | public function assign($key, $value) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get controller Data container |
||
| 317 | * |
||
| 318 | * @return Data |
||
| 319 | */ |
||
| 320 | 4 | public function getData() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Render controller |
||
| 330 | * |
||
| 331 | * @param string $type |
||
| 332 | * @return mixed |
||
| 333 | */ |
||
| 334 | 2 | public function render($type = 'HTML') |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Specify data which should be serialized to JSON |
||
| 373 | * |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | public function jsonSerialize() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Magic cast to string |
||
| 383 | * |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | public function __toString() |
||
| 390 | } |
||
| 391 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.