Complex classes like Framework 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 Framework, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Framework extends \Framework |
||
|
|||
11 | { |
||
12 | use Config\OptionalTrait; |
||
13 | |||
14 | /** |
||
15 | * @var Response |
||
16 | */ |
||
17 | private $response; |
||
18 | /** |
||
19 | * @var Routing |
||
20 | */ |
||
21 | private $routingSystem; |
||
22 | /** |
||
23 | * @var ErrorHandler |
||
24 | */ |
||
25 | private $errorHandler; |
||
26 | |||
27 | /** |
||
28 | * @var Dispatcher |
||
29 | */ |
||
30 | private $preDispatcherSystem; |
||
31 | |||
32 | /** |
||
33 | * @var Dispatcher |
||
34 | */ |
||
35 | private $postDispatcherSystem; |
||
36 | /** |
||
37 | * @var Bootstrap |
||
38 | */ |
||
39 | private $bootstrap; |
||
40 | |||
41 | /** |
||
42 | * @param Routing $routingSystem |
||
43 | * @return $this |
||
44 | */ |
||
45 | 2 | public function setRoutingSystem(Routing $routingSystem) |
|
50 | |||
51 | /** |
||
52 | * @return Routing |
||
53 | */ |
||
54 | 3 | public function getRoutingSystem() |
|
58 | |||
59 | /** |
||
60 | * @return Response |
||
61 | */ |
||
62 | 6 | public function getResponse() |
|
66 | |||
67 | /** |
||
68 | * @param Response $response |
||
69 | * @return $this |
||
70 | */ |
||
71 | 1 | public function setResponse(Response $response) |
|
76 | |||
77 | /** |
||
78 | * @return array |
||
79 | */ |
||
80 | 2 | public function getRoute() |
|
90 | |||
91 | /** |
||
92 | * @param string $directory |
||
93 | * @param string $controller |
||
94 | * Real 404 errors |
||
95 | * @throws NotFound |
||
96 | */ |
||
97 | 2 | public function getRouteError($directory, $controller) |
|
101 | |||
102 | /** |
||
103 | * @param string $controllerName |
||
104 | * @param string $action |
||
105 | * @return Controller |
||
106 | * @throws Exception\Status\NotFound |
||
107 | */ |
||
108 | 4 | protected function instantiate($controllerName, $action) |
|
109 | { |
||
110 | //To be compliant with old system @todo |
||
111 | 4 | if (!class_exists($controllerName)) { |
|
112 | 1 | throw new Exception\Status\NotFound('Controller does not exist'); |
|
113 | } |
||
114 | /* @var $controllerInstance Controller */ |
||
115 | 3 | $controllerInstance = new $controllerName(); |
|
116 | $controllerInstance |
||
117 | 3 | ->setRequest($this->getRequest()) |
|
118 | 3 | ->setResponse($this->getResponse()) |
|
119 | 3 | ->setBootstrap($this->getBootstrap()); |
|
120 | |||
121 | 3 | $controllerInstance->preFilter($action); |
|
122 | 3 | $callable = $controllerInstance->getActionMethod($action); |
|
123 | 3 | $actionReturn = null; |
|
124 | 3 | if (!is_callable(array($controllerInstance, $callable))) { |
|
125 | 1 | throw new Exception\Status\NotFound("Undefined function $callable"); |
|
126 | } |
||
127 | 2 | $actionReturn = call_user_func(array($controllerInstance, $callable)); |
|
128 | 2 | $controllerInstance->postFilter($action); |
|
129 | |||
130 | 2 | if (!is_null($actionReturn)) { |
|
131 | 1 | $controllerInstance->getResponse() |
|
132 | 1 | ->setBody($actionReturn instanceof View ? $actionReturn->render() : $actionReturn); |
|
133 | } |
||
134 | 2 | return $controllerInstance; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return $this |
||
139 | */ |
||
140 | 2 | protected function dispatch() |
|
157 | |||
158 | /** |
||
159 | * @return ErrorHandler\Plugin\Mail |
||
160 | * @codeCoverageIgnore |
||
161 | */ |
||
162 | protected function createPluginMail() |
||
166 | |||
167 | /** |
||
168 | * @param int $code |
||
169 | * @param string $msg |
||
170 | * @param null $errFile |
||
171 | * @param int $errLine |
||
172 | * @param array $errContext |
||
173 | */ |
||
174 | 1 | public function errorHandler($code, $msg, $errFile = null, $errLine = 0, array $errContext = array()) |
|
175 | { |
||
176 | 1 | $block = E_PARSE | E_ERROR | E_USER_ERROR; |
|
177 | 1 | $binary = $code & $block; |
|
178 | 1 | $message = $msg . ' in file ' . $errFile . ' on line ' . $errLine; |
|
179 | 1 | if ($binary) { |
|
180 | 1 | $message .= ' {' . serialize($errContext) . '}'; |
|
181 | 1 | $this->createPluginMail() |
|
182 | 1 | ->setBootstrap($this->getBootstrap()) |
|
183 | 1 | ->setRequest($this->getRequest()) |
|
184 | 1 | ->setException(new Exception($message, $code)) |
|
185 | 1 | ->handle(); |
|
186 | } |
||
187 | |||
188 | $translate = array( |
||
189 | 1 | E_NOTICE => Logger::NOTICE, |
|
190 | 1 | E_WARNING => Logger::WARNING, |
|
191 | 1 | E_ERROR => Logger::ERROR, |
|
192 | 1 | E_PARSE => Logger::CRITICAL, |
|
193 | 1 | E_DEPRECATED => Logger::INFO, |
|
194 | 1 | E_USER_ERROR => Logger::NOTICE, |
|
195 | 1 | E_USER_WARNING => Logger::WARNING, |
|
196 | 1 | E_USER_ERROR => Logger::ERROR, |
|
197 | 1 | E_USER_DEPRECATED => Logger::INFO, |
|
198 | 1 | E_STRICT => Logger::INFO, |
|
199 | 1 | E_RECOVERABLE_ERROR => Logger::ERROR, |
|
200 | 1 | E_ALL => Logger::CRITICAL, |
|
201 | 1 | E_COMPILE_ERROR => Logger::ERROR, |
|
202 | 1 | E_COMPILE_WARNING => Logger::WARNING, |
|
203 | 1 | E_CORE_ERROR => Logger::ERROR, |
|
204 | 1 | E_CORE_WARNING => Logger::WARNING, |
|
205 | 1 | E_USER_NOTICE => Logger::NOTICE, |
|
206 | ); |
||
207 | 1 | $this->getBootstrap()->getLogger()->log(Logger\Channel\System::NAME, $translate[$code], $message, $errContext); |
|
208 | 1 | if ($binary && $this->getSapi()->get() == Sapi::CLI) { |
|
209 | 1 | $this->phpExit($binary); |
|
210 | } |
||
211 | 1 | } |
|
212 | |||
213 | /** |
||
214 | * @param int $code |
||
215 | * @codeCoverageIgnore |
||
216 | */ |
||
217 | protected function phpExit($code) |
||
221 | |||
222 | /** |
||
223 | * @return ErrorHandler |
||
224 | */ |
||
225 | 2 | public function getErrorHandler() |
|
229 | |||
230 | /** |
||
231 | * @param ErrorHandler $errorHandler |
||
232 | * @return $this |
||
233 | */ |
||
234 | 3 | public function setErrorHandler(ErrorHandler $errorHandler) |
|
239 | |||
240 | /** |
||
241 | * PreDispatch Request |
||
242 | * @return $this |
||
243 | */ |
||
244 | 1 | protected function preDispatch() |
|
249 | |||
250 | /** |
||
251 | * Treatment on response if needed |
||
252 | */ |
||
253 | 1 | protected function postDispatch() |
|
258 | |||
259 | /** |
||
260 | * @todo rewrite to be SOLID |
||
261 | */ |
||
262 | 2 | public function shutDown() |
|
263 | { |
||
264 | 2 | $error = $this->errorGetLast(); |
|
265 | 2 | $isDebug = $this->isDebug(); |
|
266 | 2 | $code = E_PARSE | E_ERROR | E_USER_ERROR; |
|
267 | 2 | $canHeader = $this->getSapi()->get() != Sapi::CLI; |
|
268 | 2 | if ($error !== null && ($error['type'] & $code)) { |
|
269 | 1 | $this->errorHandler($code, $error['message'], $error['file'], $error['line']); |
|
270 | 1 | if ($canHeader) { |
|
271 | 1 | $this->getErrorHeader()->render(); |
|
272 | 1 | if (!$isDebug) { |
|
273 | echo "<br/>Une erreur est survenue !<br/>" |
||
274 | . "Le support informatique a été prévenu " |
||
275 | . "et règlera le problême dans les plus brefs délais.<br/>" |
||
276 | . "<br/>" |
||
277 | 1 | . "L'équipe des développeurs vous prie de l'excuser pour le désagrément.<br/>"; |
|
278 | } |
||
279 | } |
||
280 | } |
||
281 | 2 | } |
|
282 | |||
283 | /** |
||
284 | * @return Response\Header\Status |
||
285 | * @codeCoverageIgnore |
||
286 | */ |
||
287 | protected function getErrorHeader() |
||
291 | |||
292 | /** |
||
293 | * @return array |
||
294 | * @codeCoverageIgnore |
||
295 | */ |
||
296 | protected function errorGetLast() |
||
300 | |||
301 | /** |
||
302 | * @return bool |
||
303 | * @codeCoverageIgnore |
||
304 | */ |
||
305 | protected function isDebug() |
||
309 | |||
310 | /** |
||
311 | * @return Dispatcher |
||
312 | */ |
||
313 | 2 | public function getPreDispatcherSystem() |
|
317 | |||
318 | /** |
||
319 | * @param Dispatcher $preDispatch |
||
320 | * @return $this |
||
321 | */ |
||
322 | 1 | public function setPreDispatcherSystem(Dispatcher $preDispatch) |
|
327 | |||
328 | /** |
||
329 | * @return Dispatcher |
||
330 | */ |
||
331 | 2 | public function getPostDispatcherSystem() |
|
335 | |||
336 | /** |
||
337 | * @param Dispatcher $postDispatch |
||
338 | * @return $this |
||
339 | */ |
||
340 | 2 | public function setPostDispatcherSystem(Dispatcher $postDispatch) |
|
345 | |||
346 | /** |
||
347 | * @return Bootstrap |
||
348 | */ |
||
349 | 8 | public function getBootstrap() |
|
353 | |||
354 | /** |
||
355 | * @param Bootstrap $bootstrap |
||
356 | * @return $this |
||
357 | */ |
||
358 | 6 | public function setBootstrap(Bootstrap $bootstrap) |
|
363 | |||
364 | 4 | public function initialize() |
|
365 | { |
||
366 | 4 | if (!$this->getBootstrap()->hasSapi()) { |
|
378 | } |
||
379 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.