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 | * @var int |
||
18 | */ |
||
19 | protected $stackTraceLimit = 100; |
||
20 | |||
21 | /** |
||
22 | * @param integer $limit |
||
23 | */ |
||
24 | public function setStackTraceLimit($limit) |
||
28 | |||
29 | /** |
||
30 | * @param string $projectRoot |
||
31 | */ |
||
32 | public function setProjectRoot($projectRoot) |
||
36 | |||
37 | /** |
||
38 | * Serialize exception object |
||
39 | * |
||
40 | * @param mixed $exception |
||
41 | * @return array |
||
42 | */ |
||
43 | public function exceptionToArray($exception) |
||
63 | |||
64 | /** |
||
65 | * @param array $errorLog |
||
66 | * @return array |
||
67 | */ |
||
68 | public function setCurrentStackTrace(array $errorLog) |
||
92 | |||
93 | /** |
||
94 | * @return array |
||
95 | */ |
||
96 | protected function getCurrentStackTrace() |
||
132 | |||
133 | /** |
||
134 | * Serialize stack trace to array |
||
135 | * |
||
136 | * @param array $stackTrace |
||
137 | * @return array |
||
138 | */ |
||
139 | public function stackTraceToArray(array $stackTrace, $topFile = null, $topLine = null) |
||
179 | |||
180 | /** |
||
181 | * @param $relativePath |
||
182 | * @param $line |
||
183 | * @param int $linesAround |
||
184 | * @return array|void |
||
185 | */ |
||
186 | public function getCode($relativePath, $line, $linesAround = 6) |
||
224 | |||
225 | /** |
||
226 | * Return stack trace line number |
||
227 | * |
||
228 | * @param array $trace |
||
229 | * @return mixed |
||
230 | */ |
||
231 | protected function getStackTraceLine(array $trace) |
||
238 | |||
239 | /** |
||
240 | * Return stack trace file |
||
241 | * |
||
242 | * @param array $trace |
||
243 | * @return mixed |
||
244 | */ |
||
245 | protected function getStackTraceFile(array $trace) |
||
252 | |||
253 | /** |
||
254 | * Return call type |
||
255 | * |
||
256 | * @param array $trace |
||
257 | * @return string |
||
258 | */ |
||
259 | protected function stackTraceCallToString(array $trace) |
||
276 | |||
277 | /** |
||
278 | * Serialize stack trace function arguments |
||
279 | * |
||
280 | * @param array $trace |
||
281 | * @return array |
||
282 | */ |
||
283 | protected function stackTraceArgsToArray(array $trace) |
||
284 | { |
||
285 | $params = []; |
||
286 | |||
287 | if (! isset($trace['args'])) |
||
288 | { |
||
289 | return $params; |
||
290 | } |
||
291 | |||
292 | foreach ($trace['args'] as $arg) |
||
293 | { |
||
294 | if (is_array($arg)) |
||
295 | { |
||
296 | $params[] = 'array(' . count($arg) . ')'; |
||
297 | } |
||
298 | else if (is_object($arg)) |
||
299 | { |
||
300 | $params[] = get_class($arg); |
||
301 | } |
||
302 | else if (is_string($arg)) |
||
303 | { |
||
304 | $params[] = 'string(' . (strlen($arg) > 70 ? substr($arg, 0, 70) . '...' : $arg) . ')'; |
||
305 | } |
||
306 | else if (is_int($arg)) |
||
307 | { |
||
308 | $params[] = 'int(' . $arg . ')'; |
||
309 | } |
||
310 | else if (is_float($arg)) |
||
311 | { |
||
312 | $params[] = 'float(' . $arg . ')'; |
||
313 | } |
||
314 | else if (is_bool($arg)) |
||
315 | { |
||
316 | $params[] = 'bool(' . ($arg ? 'true' : 'false') . ')'; |
||
317 | } |
||
318 | else if ($arg instanceof \__PHP_Incomplete_Class) |
||
319 | { |
||
320 | $params[] = 'object(__PHP_Incomplete_Class)'; |
||
321 | } |
||
322 | else |
||
323 | { |
||
324 | $params[] = gettype($arg); |
||
325 | } |
||
326 | } |
||
327 | |||
328 | return $params; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @param $path |
||
333 | * @return string |
||
334 | */ |
||
335 | protected function removeProjectRoot($path) |
||
342 | } |
||
343 |