Complex classes like ExceptionEncoder 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 ExceptionEncoder, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Understand\UnderstandLaravel5; |
||
8 | class ExceptionEncoder |
||
9 | { |
||
10 | |||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $projectRoot; |
||
15 | |||
16 | /** |
||
17 | * @param string $projectRoot |
||
18 | */ |
||
19 | public function setProjectRoot($projectRoot) |
||
23 | |||
24 | /** |
||
25 | * Serialize exception object |
||
26 | * |
||
27 | * @param mixed $exception |
||
28 | * @return array |
||
29 | */ |
||
30 | public function exceptionToArray($exception) |
||
50 | |||
51 | /** |
||
52 | * @param array $errorLog |
||
53 | * @return array |
||
54 | */ |
||
55 | public function setCurrentStackTrace(array $errorLog) |
||
79 | |||
80 | /** |
||
81 | * @return array |
||
82 | */ |
||
83 | protected function getCurrentStackTrace() |
||
119 | |||
120 | /** |
||
121 | * Serialize stack trace to array |
||
122 | * |
||
123 | * @param array $stackTrace |
||
124 | * @return array |
||
125 | */ |
||
126 | public function stackTraceToArray(array $stackTrace, $topFile = null, $topLine = null) |
||
158 | |||
159 | /** |
||
160 | * @param $relativePath |
||
161 | * @param $line |
||
162 | * @param int $linesAround |
||
163 | * @return array|void |
||
164 | */ |
||
165 | public function getCode($relativePath, $line, $linesAround = 6) |
||
201 | |||
202 | /** |
||
203 | * Return stack trace line number |
||
204 | * |
||
205 | * @param array $trace |
||
206 | * @return mixed |
||
207 | */ |
||
208 | protected function getStackTraceLine(array $trace) |
||
215 | |||
216 | /** |
||
217 | * Return stack trace file |
||
218 | * |
||
219 | * @param array $trace |
||
220 | * @return mixed |
||
221 | */ |
||
222 | protected function getStackTraceFile(array $trace) |
||
229 | |||
230 | /** |
||
231 | * Return call type |
||
232 | * |
||
233 | * @param array $trace |
||
234 | * @return string |
||
235 | */ |
||
236 | protected function stackTraceCallToString(array $trace) |
||
253 | |||
254 | /** |
||
255 | * Serialize stack trace function arguments |
||
256 | * |
||
257 | * @param array $trace |
||
258 | * @return array |
||
259 | */ |
||
260 | protected function stackTraceArgsToArray(array $trace) |
||
307 | |||
308 | /** |
||
309 | * @param $path |
||
310 | * @return string |
||
311 | */ |
||
312 | protected function removeProjectRoot($path) |
||
319 | } |
||
320 |