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 |
||
19 | final class Run implements RunInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var bool |
||
23 | */ |
||
24 | private $isRegistered; |
||
25 | |||
26 | /** |
||
27 | * @var bool |
||
28 | */ |
||
29 | private $allowQuit = true; |
||
30 | |||
31 | /** |
||
32 | * @var bool |
||
33 | */ |
||
34 | private $sendOutput = true; |
||
35 | |||
36 | /** |
||
37 | * @var integer|false |
||
38 | */ |
||
39 | private $sendHttpCode = 500; |
||
40 | |||
41 | /** |
||
42 | * @var integer|false |
||
43 | */ |
||
44 | private $sendExitCode = 1; |
||
45 | |||
46 | /** |
||
47 | * @var HandlerInterface[] |
||
48 | */ |
||
49 | private $handlerStack = []; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | * @psalm-var list<array{patterns: string, levels: int}> |
||
54 | */ |
||
55 | private $silencedPatterns = []; |
||
56 | |||
57 | /** |
||
58 | * @var SystemFacade |
||
59 | */ |
||
60 | private $system; |
||
61 | |||
62 | /** |
||
63 | * In certain scenarios, like in shutdown handler, we can not throw exceptions. |
||
64 | * |
||
65 | * @var bool |
||
66 | */ |
||
67 | private $canThrowExceptions = true; |
||
68 | |||
69 | 6 | public function __construct(SystemFacade $system = null) |
|
70 | { |
||
71 | 6 | $this->system = $system ?: new SystemFacade; |
|
72 | 6 | } |
|
73 | |||
74 | /** |
||
75 | * Explicitly request your handler runs as the last of all currently registered handlers. |
||
76 | * |
||
77 | * @param HandlerInterface $handler |
||
78 | * |
||
79 | * @return Run |
||
80 | */ |
||
81 | public function appendHandler($handler) |
||
82 | { |
||
83 | array_unshift($this->handlerStack, $this->resolveHandler($handler)); |
||
84 | return $this; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Explicitly request your handler runs as the first of all currently registered handlers. |
||
89 | * |
||
90 | * @param HandlerInterface $handler |
||
91 | * |
||
92 | * @return Run |
||
93 | */ |
||
94 | public function prependHandler($handler) |
||
95 | { |
||
96 | return $this->pushHandler($handler); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Register your handler as the last of all currently registered handlers (to be executed first). |
||
101 | * Prefer using appendHandler and prependHandler for clarity. |
||
102 | * |
||
103 | * @param Callable|HandlerInterface $handler |
||
104 | * |
||
105 | * @return Run |
||
106 | * |
||
107 | * @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface. |
||
108 | */ |
||
109 | 10 | public function pushHandler($handler) |
|
110 | { |
||
111 | 10 | $this->handlerStack[] = $this->resolveHandler($handler); |
|
|
|||
112 | 9 | return $this; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * Removes and returns the last handler pushed to the handler stack. |
||
117 | * |
||
118 | * @see Run::removeFirstHandler(), Run::removeLastHandler() |
||
119 | * |
||
120 | * @return HandlerInterface|null |
||
121 | */ |
||
122 | 1 | public function popHandler() |
|
123 | { |
||
124 | 1 | return array_pop($this->handlerStack); |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Removes the first handler. |
||
129 | * |
||
130 | * @return void |
||
131 | */ |
||
132 | 1 | public function removeFirstHandler() |
|
133 | { |
||
134 | 1 | array_pop($this->handlerStack); |
|
135 | 1 | } |
|
136 | |||
137 | /** |
||
138 | * Removes the last handler. |
||
139 | * |
||
140 | * @return void |
||
141 | */ |
||
142 | 1 | public function removeLastHandler() |
|
143 | { |
||
144 | 1 | array_shift($this->handlerStack); |
|
145 | 1 | } |
|
146 | |||
147 | /** |
||
148 | * Returns an array with all handlers, in the order they were added to the stack. |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | 3 | public function getHandlers() |
|
153 | { |
||
154 | 3 | return $this->handlerStack; |
|
155 | } |
||
156 | |||
157 | /** |
||
158 | * Clears all handlers in the handlerStack, including the default PrettyPage handler. |
||
159 | * |
||
160 | * @return Run |
||
161 | */ |
||
162 | 1 | public function clearHandlers() |
|
163 | { |
||
164 | 1 | $this->handlerStack = []; |
|
165 | 1 | return $this; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Registers this instance as an error handler. |
||
170 | * |
||
171 | * @return Run |
||
172 | */ |
||
173 | 6 | public function register() |
|
192 | |||
193 | /** |
||
194 | * Unregisters all handlers registered by this Whoops\Run instance. |
||
195 | * |
||
196 | * @return Run |
||
197 | */ |
||
198 | 1 | public function unregister() |
|
199 | { |
||
200 | 1 | if ($this->isRegistered) { |
|
201 | 1 | $this->system->restoreExceptionHandler(); |
|
209 | |||
210 | /** |
||
211 | * Should Whoops allow Handlers to force the script to quit? |
||
212 | * |
||
213 | * @param bool|int $exit |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | 6 | public function allowQuit($exit = null) |
|
225 | |||
226 | /** |
||
227 | * Silence particular errors in particular files. |
||
228 | * |
||
229 | * @param array|string $patterns List or a single regex pattern to match. |
||
230 | * @param int $levels Defaults to E_STRICT | E_DEPRECATED. |
||
231 | * |
||
232 | * @return Run |
||
233 | */ |
||
234 | 1 | public function silenceErrorsInPaths($patterns, $levels = 10240) |
|
251 | |||
252 | /** |
||
253 | * Returns an array with silent errors in path configuration. |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | public function getSilenceErrorsInPaths() |
||
261 | |||
262 | /** |
||
263 | * Should Whoops send HTTP error code to the browser if possible? |
||
264 | * Whoops will by default send HTTP code 500, but you may wish to |
||
265 | * use 502, 503, or another 5xx family code. |
||
266 | * |
||
267 | * @param bool|int $code |
||
268 | * |
||
269 | * @return int|false |
||
270 | * |
||
271 | * @throws InvalidArgumentException |
||
272 | */ |
||
273 | 6 | public function sendHttpCode($code = null) |
|
295 | |||
296 | /** |
||
297 | * Should Whoops exit with a specific code on the CLI if possible? |
||
298 | * Whoops will exit with 1 by default, but you can specify something else. |
||
299 | * |
||
300 | * @param int $code |
||
301 | * |
||
302 | * @return int |
||
303 | * |
||
304 | * @throws InvalidArgumentException |
||
305 | */ |
||
306 | 2 | public function sendExitCode($code = null) |
|
320 | |||
321 | /** |
||
322 | * Should Whoops push output directly to the client? |
||
323 | * If this is false, output will be returned by handleException. |
||
324 | * |
||
325 | * @param bool|int $send |
||
326 | * |
||
327 | * @return bool |
||
328 | */ |
||
329 | 5 | public function writeToOutput($send = null) |
|
337 | |||
338 | /** |
||
339 | * Handles an exception, ultimately generating a Whoops error page. |
||
340 | * |
||
341 | * @param Throwable $exception |
||
342 | * |
||
343 | * @return string Output generated by handlers. |
||
344 | */ |
||
345 | 7 | public function handleException($exception) |
|
420 | |||
421 | /** |
||
422 | * Converts generic PHP errors to \ErrorException instances, before passing them off to be handled. |
||
423 | * |
||
424 | * This method MUST be compatible with set_error_handler. |
||
425 | * |
||
426 | * @param int $level |
||
427 | * @param string $message |
||
428 | * @param string|null $file |
||
429 | * @param int|null $line |
||
430 | * |
||
431 | * @return bool |
||
432 | * |
||
433 | * @throws ErrorException |
||
434 | */ |
||
435 | 5 | public function handleError($level, $message, $file = null, $line = null) |
|
464 | |||
465 | /** |
||
466 | * Special case to deal with Fatal errors and the like. |
||
467 | * |
||
468 | * @return void |
||
469 | */ |
||
470 | public function handleShutdown() |
||
490 | |||
491 | /** |
||
492 | * @param Throwable $exception |
||
493 | * |
||
494 | * @return Inspector |
||
495 | */ |
||
496 | 3 | private function getInspector($exception) |
|
500 | |||
501 | /** |
||
502 | * Resolves the giving handler. |
||
503 | * |
||
504 | * @param HandlerInterface $handler |
||
505 | * |
||
506 | * @return HandlerInterface |
||
507 | * |
||
508 | * @throws InvalidArgumentException |
||
509 | */ |
||
510 | 6 | private function resolveHandler($handler) |
|
525 | |||
526 | /** |
||
527 | * Echo something to the browser. |
||
528 | * |
||
529 | * @param string $output |
||
530 | * |
||
531 | * @return Run |
||
532 | */ |
||
533 | 3 | private function writeToOutputNow($output) |
|
545 | } |
||
546 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.