Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Run 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 Run, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | final class Run implements RunInterface |
||
19 | { |
||
20 | private $isRegistered; |
||
21 | private $allowQuit = true; |
||
22 | private $sendOutput = true; |
||
23 | |||
24 | /** |
||
25 | * @var integer|false |
||
26 | */ |
||
27 | private $sendHttpCode = 500; |
||
28 | |||
29 | /** |
||
30 | * @var HandlerInterface[] |
||
31 | */ |
||
32 | private $handlerStack = []; |
||
33 | |||
34 | private $silencedPatterns = []; |
||
35 | |||
36 | private $system; |
||
37 | |||
38 | 4 | public function __construct(SystemFacade $system = null) |
|
42 | |||
43 | /** |
||
44 | * Pushes a handler to the end of the stack |
||
45 | * |
||
46 | * @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface |
||
47 | * @param Callable|HandlerInterface $handler |
||
48 | * @return Run |
||
49 | */ |
||
50 | 8 | View Code Duplication | public function pushHandler($handler) |
66 | |||
67 | /** |
||
68 | * Prepends a handler to the begining of the stack |
||
69 | * |
||
70 | * @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface |
||
71 | * @param Callable|HandlerInterface $handler |
||
72 | 1 | * @return Run |
|
73 | */ |
||
74 | 1 | View Code Duplication | public function prependHandler($handler) |
90 | |||
91 | /** |
||
92 | 1 | * Removes the last handler in the stack and returns it. |
|
93 | * Returns null if there"s nothing else to pop. |
||
94 | 1 | * @return null|HandlerInterface |
|
95 | 1 | */ |
|
96 | public function popHandler() |
||
100 | |||
101 | /** |
||
102 | 1 | * Returns an array with all handlers, in the |
|
103 | * order they were added to the stack. |
||
104 | 1 | * @return array |
|
105 | */ |
||
106 | public function getHandlers() |
||
110 | |||
111 | 4 | /** |
|
112 | * Clears all handlers in the handlerStack, including |
||
113 | 4 | * the default PrettyPage handler. |
|
114 | * @return Run |
||
115 | */ |
||
116 | 4 | public function clearHandlers() |
|
121 | 4 | ||
122 | 4 | /** |
|
123 | 4 | * @param \Throwable $exception |
|
124 | * @return Inspector |
||
125 | 4 | */ |
|
126 | 4 | private function getInspector($exception) |
|
130 | |||
131 | /** |
||
132 | * Registers this instance as an error handler. |
||
133 | * @return Run |
||
134 | */ |
||
135 | 1 | public function register() |
|
154 | 4 | ||
155 | 1 | /** |
|
156 | * Unregisters all handlers registered by this Whoops\Run instance |
||
157 | * @return Run |
||
158 | 4 | */ |
|
159 | public function unregister() |
||
170 | 1 | ||
171 | 1 | /** |
|
172 | 1 | * Should Whoops allow Handlers to force the script to quit? |
|
173 | * @param bool|int $exit |
||
174 | 1 | * @return bool |
|
175 | 1 | */ |
|
176 | 1 | public function allowQuit($exit = null) |
|
184 | |||
185 | /** |
||
186 | * Silence particular errors in particular files |
||
187 | * @param array|string $patterns List or a single regex pattern to match |
||
188 | * @param int $levels Defaults to E_STRICT | E_DEPRECATED |
||
189 | * @return \Whoops\Run |
||
190 | */ |
||
191 | public function silenceErrorsInPaths($patterns, $levels = 10240) |
||
207 | |||
208 | |||
209 | 3 | /** |
|
210 | 1 | * Returns an array with silent errors in path configuration |
|
211 | * |
||
212 | * @return array |
||
213 | 2 | */ |
|
214 | 1 | public function getSilenceErrorsInPaths() |
|
218 | 1 | ||
219 | 1 | /* |
|
220 | 1 | * Should Whoops send HTTP error code to the browser if possible? |
|
221 | * Whoops will by default send HTTP code 500, but you may wish to |
||
222 | * use 502, 503, or another 5xx family code. |
||
223 | 1 | * |
|
224 | * @param bool|int $code |
||
225 | * @return int|false |
||
226 | */ |
||
227 | public function sendHttpCode($code = null) |
||
249 | |||
250 | /** |
||
251 | * Should Whoops push output directly to the client? |
||
252 | 5 | * If this is false, output will be returned by handleException |
|
253 | * @param bool|int $send |
||
254 | * @return bool |
||
255 | */ |
||
256 | public function writeToOutput($send = null) |
||
264 | 5 | ||
265 | 5 | /** |
|
266 | 5 | * Handles an exception, ultimately generating a Whoops error |
|
267 | * page. |
||
268 | * |
||
269 | * @param \Throwable $exception |
||
270 | * @return string Output generated by handlers |
||
271 | */ |
||
272 | 5 | public function handleException($exception) |
|
343 | |||
344 | /** |
||
345 | 2 | * Converts generic PHP errors to \ErrorException |
|
346 | * instances, before passing them off to be handled. |
||
347 | * |
||
348 | * This method MUST be compatible with set_error_handler. |
||
349 | 2 | * |
|
350 | 2 | * @param int $level |
|
351 | 2 | * @param string $message |
|
352 | * @param string $file |
||
353 | * @param int $line |
||
354 | * |
||
355 | * @return bool |
||
356 | * @throws ErrorException |
||
357 | */ |
||
358 | public function handleError($level, $message, $file = null, $line = null) |
||
387 | |||
388 | /** |
||
389 | * Special case to deal with Fatal errors and the like. |
||
390 | */ |
||
391 | public function handleShutdown() |
||
410 | |||
411 | /** |
||
412 | * In certain scenarios, like in shutdown handler, we can not throw exceptions |
||
413 | * @var bool |
||
414 | */ |
||
415 | private $canThrowExceptions = true; |
||
416 | |||
417 | /** |
||
418 | * Echo something to the browser |
||
419 | * @param string $output |
||
420 | * @return $this |
||
421 | */ |
||
422 | private function writeToOutputNow($output) |
||
434 | } |
||
435 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.