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 |
||
| 17 | class Controller |
||
| 18 | { |
||
| 19 | /** @var ServerRequestInterface */ |
||
| 20 | protected $request; |
||
| 21 | |||
| 22 | /** @var ViewEngine $plates */ |
||
| 23 | protected $viewEngine; |
||
| 24 | |||
| 25 | /** @var string $controller */ |
||
| 26 | protected $controller; |
||
| 27 | |||
| 28 | /** @var string $action */ |
||
| 29 | protected $action; |
||
| 30 | |||
| 31 | /** @var stdClass $view */ |
||
| 32 | public $view; |
||
| 33 | |||
| 34 | /** @var string $body */ |
||
| 35 | private $body; |
||
| 36 | |||
| 37 | /** @var bool */ |
||
| 38 | private $layoutEnabled; |
||
| 39 | |||
| 40 | /** @var bool */ |
||
| 41 | private $viewEnabled; |
||
| 42 | |||
| 43 | /** @var array $headers */ |
||
| 44 | private $headers; |
||
| 45 | |||
| 46 | /** @var int $statusCode */ |
||
| 47 | private $statusCode = 200; |
||
| 48 | |||
| 49 | /** @var MailService $mailService */ |
||
| 50 | private $mailService; |
||
| 51 | |||
| 52 | /** @var array $params */ |
||
| 53 | public $params; |
||
| 54 | |||
| 55 | /** @var array $post */ |
||
| 56 | protected $post = []; |
||
| 57 | |||
| 58 | /** @var MySQL */ |
||
| 59 | protected $_db; |
||
| 60 | |||
| 61 | /** @var Environment $serverEnvironment */ |
||
| 62 | protected $serverEnvironment; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Controller constructor. |
||
| 66 | * @param ServerRequestInterface $request |
||
| 67 | */ |
||
| 68 | 27 | public function __construct(ServerRequestInterface $request) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @return void |
||
| 87 | */ |
||
| 88 | 1 | protected function setDB() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @return void |
||
| 96 | */ |
||
| 97 | 27 | protected function initViewEngine() |
|
| 103 | |||
| 104 | 1 | public function setViewEngine(ViewEngine $engine) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @return PDO |
||
| 111 | */ |
||
| 112 | 1 | public function getDbAdapter() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @return MailService |
||
| 123 | */ |
||
| 124 | 1 | public function getMailService() |
|
| 132 | |||
| 133 | 1 | private function initMailService() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @return ViewEngine |
||
| 147 | */ |
||
| 148 | 4 | public function getViewEngine() |
|
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * runs before th' controller action |
||
| 156 | */ |
||
| 157 | 18 | public function init() |
|
| 161 | |||
| 162 | 1 | public function getParams() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @param $param |
||
| 169 | * @return mixed |
||
| 170 | */ |
||
| 171 | 1 | public function getParam($param, $default = null) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @param $key |
||
| 184 | * @param $val |
||
| 185 | * @return $this |
||
| 186 | */ |
||
| 187 | 16 | public function setParam($key, $val) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * runs after yer work is done |
||
| 195 | */ |
||
| 196 | 18 | public function postDispatch() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * For loadin' th' cannon, so t' speak |
||
| 203 | * |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | 6 | public function getHeaders() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | 4 | public function hasLayoutEnabled() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Enables the layout |
||
| 221 | */ |
||
| 222 | 1 | public function enableLayout() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Disables the layout |
||
| 229 | */ |
||
| 230 | 5 | public function disableLayout() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | 4 | public function hasViewEnabled() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Enables the view |
||
| 245 | */ |
||
| 246 | 1 | public function enableView() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Disables the view |
||
| 253 | */ |
||
| 254 | 5 | public function disableView() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | 5 | public function getBody() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Only used if Layout & View disabled |
||
| 269 | * |
||
| 270 | * @param $body |
||
| 271 | */ |
||
| 272 | 2 | public function setBody($body) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @return array |
||
| 279 | */ |
||
| 280 | 1 | public function indexAction() |
|
| 284 | |||
| 285 | 2 | public function errorAction() |
|
| 292 | |||
| 293 | 1 | public function notFoundAction() |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @return ServerRequestInterface |
||
| 302 | */ |
||
| 303 | 1 | public function getRequest() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * @param ServerRequestInterface $request |
||
| 310 | * @return Controller |
||
| 311 | */ |
||
| 312 | 1 | public function setRequest(ServerRequestInterface $request) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * @param string $key |
||
| 320 | * @param string $value |
||
| 321 | * @return $this |
||
| 322 | */ |
||
| 323 | 3 | public function setHeader($key, $value) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * @param array $headers |
||
| 331 | */ |
||
| 332 | 2 | public function setHeaders(array $headers) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * @param int $statusCode |
||
| 339 | */ |
||
| 340 | 2 | public function setStatusCode($statusCode) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * @return int |
||
| 347 | */ |
||
| 348 | 4 | public function getStatusCode() |
|
| 352 | |||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * @param $key |
||
| 357 | * @return string|null |
||
| 358 | */ |
||
| 359 | 3 | public function getHeader($key) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * @param array $data |
||
| 366 | * @param int $statusCode |
||
| 367 | */ |
||
| 368 | 1 | public function sendJsonResponse(array $data, $statusCode = 200) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * @param null $key |
||
| 382 | * @param string $default |
||
| 383 | * @return array|string|null |
||
| 384 | */ |
||
| 385 | 1 | public function getPost($key = null, $default = null) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * @return Environment |
||
| 396 | */ |
||
| 397 | public function getServerEnvironment(): Environment |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param Environment $serverEnvironment |
||
| 404 | */ |
||
| 405 | 3 | public function setServerEnvironment(Environment $serverEnvironment) |
|
| 409 | } |
||
| 410 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.