Complex classes like Exception 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.
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 Exception, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Exception extends \Exception |
||
20 | { |
||
21 | /** |
||
22 | * The current file. |
||
23 | * |
||
24 | * @var ImportedFile|FileInfo|string |
||
25 | */ |
||
26 | private $currentFile; |
||
27 | |||
28 | /** |
||
29 | * The current parser index. |
||
30 | * |
||
31 | * @var int |
||
32 | */ |
||
33 | private $index; |
||
34 | |||
35 | /** |
||
36 | * Current line. |
||
37 | * |
||
38 | * @var int|null |
||
39 | */ |
||
40 | private $errorLine; |
||
41 | |||
42 | /** |
||
43 | * Current column. |
||
44 | * |
||
45 | * @var int|null |
||
46 | */ |
||
47 | private $errorColumn; |
||
48 | |||
49 | /** |
||
50 | * Excerpt from the string which contains error. |
||
51 | * |
||
52 | * @var Util\StringExcerpt |
||
53 | */ |
||
54 | private $excerpt; |
||
55 | |||
56 | /** |
||
57 | * File editor link. Allows variable holders:. |
||
58 | * |
||
59 | * * `%file` or `%f` - current file |
||
60 | * * `%line` or `%l` - current line |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected static $fileEditUrlFormat = 'editor://open?file=%f&line=%l'; |
||
65 | |||
66 | /** |
||
67 | * File excerpt line number. |
||
68 | * |
||
69 | * @var int|false |
||
70 | */ |
||
71 | protected static $fileExcerptLineNumber = 3; |
||
72 | |||
73 | /** |
||
74 | * Constructor. |
||
75 | * |
||
76 | * @param string $message The exception message |
||
77 | * @param int $index The current parser index |
||
78 | * @param FileInfo|ImportedFile|string $currentFile The file |
||
79 | * @param \Exception $previous Previous exception |
||
80 | * @param int $code The exception code |
||
81 | */ |
||
82 | public function __construct( |
||
100 | |||
101 | /** |
||
102 | * Formats the message. |
||
103 | * |
||
104 | * @param string $message The exception message |
||
105 | * @param \Exception $previous Previous exception |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | private function formatMessage($message, \Exception $previous = null) |
||
118 | |||
119 | /** |
||
120 | * Returns the current line and column. |
||
121 | * |
||
122 | * @param FileInfo|ImportedFile|string $currentFile The file |
||
123 | * @param int $index Current position index |
||
124 | * @param bool $excerpt Include the string excerpt? |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | protected function getLocation($currentFile, $index, $column = null, $excerpt = true) |
||
153 | |||
154 | /** |
||
155 | * Updates the line, column and excerpt. |
||
156 | */ |
||
157 | protected function updateFileErrorInformation() |
||
163 | |||
164 | /** |
||
165 | * Sets the editor url format. |
||
166 | * |
||
167 | * @param string $format |
||
168 | */ |
||
169 | public static function setFileEditorUrlFormat($format) |
||
173 | |||
174 | /** |
||
175 | * Returns the editor url format. |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | public static function getFileEditorUrlFormat() |
||
183 | |||
184 | /** |
||
185 | * Sets the number of lines to display in file excerpts when an exception is displayed. |
||
186 | * |
||
187 | * @param int|false $number |
||
188 | */ |
||
189 | public static function setFileExcerptLineNumber($number) |
||
193 | |||
194 | /** |
||
195 | * Returns the number of lines to display in file excerpts. |
||
196 | * |
||
197 | * @return int|false |
||
198 | */ |
||
199 | public static function getFileExcerptLineNumber() |
||
203 | |||
204 | /** |
||
205 | * Returns the file. |
||
206 | * |
||
207 | * @return ImportedFile|FileInfo|null |
||
208 | */ |
||
209 | public function getCurrentFile() |
||
213 | |||
214 | /** |
||
215 | * Sets the current file. |
||
216 | * |
||
217 | * @param ImportedFile|FileInfo|string $file |
||
218 | * @param int $index The current index |
||
219 | */ |
||
220 | public function setCurrentFile($file, $index = null) |
||
228 | |||
229 | /** |
||
230 | * Returns the current index. |
||
231 | * |
||
232 | * @return int |
||
233 | */ |
||
234 | final public function getIndex() |
||
238 | |||
239 | /** |
||
240 | * Returns the excerpt from the string which contains the error. |
||
241 | * |
||
242 | * @return Util\StringExcerpt|null |
||
243 | */ |
||
244 | final public function getExcerpt() |
||
248 | |||
249 | /** |
||
250 | * Sets index. |
||
251 | * |
||
252 | * @param int $index |
||
253 | */ |
||
254 | final public function setIndex($index) |
||
259 | |||
260 | /** |
||
261 | * Returns current line from the file. |
||
262 | * |
||
263 | * @return int|null |
||
264 | */ |
||
265 | final public function getErrorLine() |
||
269 | |||
270 | /** |
||
271 | * Returns the error column. |
||
272 | * |
||
273 | * @return int|null |
||
274 | */ |
||
275 | final public function getErrorColumn() |
||
279 | |||
280 | /** |
||
281 | * Returns file editor link. The link format can be customized. |
||
282 | * |
||
283 | * @param FileInfo|string $file The current file |
||
284 | * @param int $line |
||
285 | * |
||
286 | * @return string|void |
||
287 | * |
||
288 | * @see setFileEditorUrlFormat |
||
289 | */ |
||
290 | protected function getFileEditorLink($file, $line = null) |
||
317 | |||
318 | /** |
||
319 | * Converts the exception to string. |
||
320 | * |
||
321 | * @return string |
||
322 | */ |
||
323 | public function __toString() |
||
327 | |||
328 | /** |
||
329 | * Converts the exception to string. |
||
330 | * |
||
331 | * @param bool $includeExcerpt Include excerpt? |
||
332 | * @param bool $html Convert to HTML? |
||
333 | * |
||
334 | * @return string |
||
335 | */ |
||
336 | public function toString($includeExcerpt = true, $html = true) |
||
361 | |||
362 | /** |
||
363 | * @return string |
||
364 | */ |
||
365 | public function prettyPrint($trace = false) |
||
380 | } |
||
381 |
This check looks at variables that 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.