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) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Return stack trace line number |
||
| 225 | * |
||
| 226 | * @param array $trace |
||
| 227 | * @return mixed |
||
| 228 | */ |
||
| 229 | protected function getStackTraceLine(array $trace) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Return stack trace file |
||
| 239 | * |
||
| 240 | * @param array $trace |
||
| 241 | * @return mixed |
||
| 242 | */ |
||
| 243 | protected function getStackTraceFile(array $trace) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Return call type |
||
| 253 | * |
||
| 254 | * @param array $trace |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | protected function stackTraceCallToString(array $trace) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Serialize stack trace function arguments |
||
| 277 | * |
||
| 278 | * @param array $trace |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | protected function stackTraceArgsToArray(array $trace) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param $path |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | protected function removeProjectRoot($path) |
||
| 340 | } |
||
| 341 |