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 |
||
13 | class Controller |
||
14 | { |
||
15 | /** @var ServerRequestInterface */ |
||
16 | protected $request; |
||
17 | |||
18 | /** @var ViewEngine $plates */ |
||
19 | protected $viewEngine; |
||
20 | |||
21 | /** @var string $controller */ |
||
22 | protected $controller; |
||
23 | |||
24 | /** @var string $action */ |
||
25 | protected $action; |
||
26 | |||
27 | /** @var stdClass $view */ |
||
28 | public $view; |
||
29 | |||
30 | /** @var string $body */ |
||
31 | private $body; |
||
32 | |||
33 | /** @var bool */ |
||
34 | private $layoutEnabled; |
||
35 | |||
36 | /** @var bool */ |
||
37 | private $viewEnabled; |
||
38 | |||
39 | /** @var array $headers */ |
||
40 | private $headers; |
||
41 | |||
42 | /** @var int $statusCode */ |
||
43 | private $statusCode = 200; |
||
44 | |||
45 | /** @var array $params */ |
||
46 | public $params; |
||
47 | |||
48 | /** @var array $post */ |
||
49 | protected $post = []; |
||
50 | |||
51 | /** @var MySQL */ |
||
52 | protected $_db; |
||
53 | |||
54 | /** |
||
55 | * Controller constructor. |
||
56 | * @param ServerRequestInterface $request |
||
57 | */ |
||
58 | 26 | public function __construct(ServerRequestInterface $request) |
|
59 | 26 | { |
|
60 | 26 | $this->request = $request; |
|
61 | 26 | $this->headers = []; |
|
62 | 26 | $this->params = $this->request->getQueryParams(); |
|
63 | |||
64 | 26 | if ($this->request->getMethod() == 'POST') { |
|
65 | $this->post = $this->request->getParsedBody(); |
||
|
|||
66 | } |
||
67 | |||
68 | 26 | $this->initViewEngine(); |
|
69 | 26 | $this->view = new stdClass(); |
|
70 | 26 | $this->layoutEnabled = true; |
|
71 | 26 | $this->viewEnabled = true; |
|
72 | 26 | } |
|
73 | |||
74 | /** |
||
75 | * @return void |
||
76 | */ |
||
77 | 1 | protected function setDB() |
|
82 | |||
83 | /** |
||
84 | * @return void |
||
85 | */ |
||
86 | 26 | protected function initViewEngine() |
|
92 | |||
93 | public function setViewEngine(ViewEngine $engine) |
||
97 | |||
98 | /** |
||
99 | * @return PDO |
||
100 | */ |
||
101 | 2 | public function getDbAdapter() |
|
102 | 1 | { |
|
103 | 1 | if(!$this->_db) |
|
104 | 1 | { |
|
105 | 2 | $this->setDB(); |
|
106 | 1 | } |
|
107 | 1 | return $this->_db->getConnection(); |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return ViewEngine |
||
112 | */ |
||
113 | 4 | public function getViewEngine() |
|
117 | |||
118 | |||
119 | /** |
||
120 | * runs before th' controller action |
||
121 | */ |
||
122 | 17 | public function init() |
|
126 | |||
127 | 1 | public function getParams() |
|
131 | |||
132 | /** |
||
133 | * @param $param |
||
134 | * @return mixed |
||
135 | */ |
||
136 | 2 | public function getParam($param, $default = null) |
|
146 | |||
147 | /** |
||
148 | * @param $key |
||
149 | * @param $val |
||
150 | * @return $this |
||
151 | */ |
||
152 | 15 | public function setParam($key, $val) |
|
157 | |||
158 | /** |
||
159 | * runs after yer work is done |
||
160 | */ |
||
161 | 17 | public function postDispatch() |
|
165 | |||
166 | /** |
||
167 | * For loadin' th' cannon, so t' speak |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | 6 | public function getHeaders() |
|
175 | |||
176 | /** |
||
177 | * @return bool |
||
178 | */ |
||
179 | 4 | public function hasLayoutEnabled() |
|
183 | |||
184 | /** |
||
185 | * Enables the layout |
||
186 | */ |
||
187 | 1 | public function enableLayout() |
|
191 | |||
192 | /** |
||
193 | * Disables the layout |
||
194 | */ |
||
195 | 5 | public function disableLayout() |
|
199 | |||
200 | /** |
||
201 | * @return bool |
||
202 | */ |
||
203 | 4 | public function hasViewEnabled() |
|
207 | |||
208 | /** |
||
209 | * Enables the view |
||
210 | */ |
||
211 | 1 | public function enableView() |
|
215 | |||
216 | /** |
||
217 | * Disables the view |
||
218 | */ |
||
219 | 5 | public function disableView() |
|
223 | |||
224 | /** |
||
225 | * @return string |
||
226 | */ |
||
227 | 5 | public function getBody() |
|
231 | |||
232 | /** |
||
233 | * Only used if Layout & View disabled |
||
234 | * |
||
235 | * @param $body |
||
236 | */ |
||
237 | 2 | public function setBody($body) |
|
241 | |||
242 | /** |
||
243 | * @return array |
||
244 | */ |
||
245 | 1 | public function indexAction() |
|
249 | |||
250 | 2 | public function errorAction() |
|
257 | |||
258 | 1 | public function notFoundAction() |
|
264 | |||
265 | /** |
||
266 | * @return ServerRequestInterface |
||
267 | */ |
||
268 | 1 | public function getRequest() |
|
272 | |||
273 | /** |
||
274 | * @param ServerRequestInterface $request |
||
275 | * @return Controller |
||
276 | */ |
||
277 | 1 | public function setRequest(ServerRequestInterface $request) |
|
282 | |||
283 | /** |
||
284 | * @param string $key |
||
285 | * @param string $value |
||
286 | * @return $this |
||
287 | */ |
||
288 | 3 | public function setHeader($key, $value) |
|
293 | |||
294 | /** |
||
295 | * @param array $headers |
||
296 | */ |
||
297 | 2 | public function setHeaders(array $headers) |
|
301 | |||
302 | /** |
||
303 | * @param int $statusCode |
||
304 | */ |
||
305 | 2 | public function setStatusCode($statusCode) |
|
309 | |||
310 | /** |
||
311 | * @return int |
||
312 | */ |
||
313 | 4 | public function getStatusCode() |
|
317 | |||
318 | |||
319 | |||
320 | /** |
||
321 | * @param $key |
||
322 | * @return string|null |
||
323 | */ |
||
324 | 3 | public function getHeader($key) |
|
328 | |||
329 | /** |
||
330 | * @param array $data |
||
331 | * @param int $statusCode |
||
332 | */ |
||
333 | 1 | public function sendJsonResponse(array $data, $statusCode = 200) |
|
344 | |||
345 | /** |
||
346 | * @param null $key |
||
347 | * @param string $default |
||
348 | * @return array|string|null |
||
349 | */ |
||
350 | 1 | public function getPost($key = null, $default = null) |
|
358 | } |
||
359 |
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
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. 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.