Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ErrorPrinter 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 ErrorPrinter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class ErrorPrinter |
||
9 | { |
||
10 | public $errorsList = [ |
||
11 | 'total' => 0, |
||
12 | ]; |
||
13 | |||
14 | public $printer; |
||
15 | |||
16 | public $logErrors = true; |
||
17 | |||
18 | public $pended = []; |
||
19 | |||
20 | public function view($absPath, $message, $lineNumber, $fileName) |
||
21 | { |
||
22 | $this->simplePendError($fileName.'.blade.php', $absPath, $lineNumber, 'view', \trim($message), ' does not exist'); |
||
23 | } |
||
24 | |||
25 | public function printFixation($absPath, $wrongClass, $lineNumber, $correct) |
||
26 | { |
||
27 | $header = $wrongClass.' <=== Did not exist'; |
||
28 | $msg = 'Auto-corrected to: '.substr($correct[0], 0, 55); |
||
29 | |||
30 | $this->simplePendError($msg, $absPath, $lineNumber, 'ns_replacement', $header); |
||
31 | } |
||
32 | |||
33 | View Code Duplication | public function route($path, $errorIt, $errorTxt, $absPath = null, $lineNumber = 0) |
|
|
|||
34 | { |
||
35 | if (LaravelPaths::isIgnored($absPath)) { |
||
36 | return; |
||
37 | } |
||
38 | |||
39 | ($this->errorsList['route'][] = (new PendingError('route')) |
||
40 | ->header($errorIt) |
||
41 | ->errorData($errorTxt.$this->yellow($path)) |
||
42 | ->link($absPath, $lineNumber)); |
||
43 | } |
||
44 | |||
45 | public function authConf() |
||
49 | |||
50 | public function badRelation($absPath, $lineNumber, $relatedModel) |
||
56 | |||
57 | public function doesNotExist($yellowText, $absPath, $lineNumber, $key, $header) |
||
58 | { |
||
59 | $this->simplePendError($yellowText, $absPath, $lineNumber, $key, $header, ' <==== does not exist'); |
||
61 | |||
62 | public function routelessAction($absPath, $lineNumber, $msg) |
||
66 | |||
67 | View Code Duplication | public function simplePendError($yellowText, $absPath, $lineNumber, $key, $header, $rest = '') |
|
78 | |||
79 | public function wrongImport($absPath, $class, $lineNumber) |
||
83 | |||
84 | View Code Duplication | public function compactError($path, $lineNumber, $absent, $key, $header) |
|
95 | |||
96 | public function routeDefinitionConflict($route1, $route2, $info) |
||
132 | |||
133 | public function wrongUsedClassError($absPath, $class, $lineNumber) |
||
137 | |||
138 | View Code Duplication | public function queryInBlade($absPath, $class, $lineNumber) |
|
150 | |||
151 | public function wrongMethodError($absPath, $class, $lineNumber) |
||
155 | |||
156 | public function yellow($msg) |
||
160 | |||
161 | public function badNamespace($absPath, $correctNamespace, $incorrectNamespace, $lineNumber = 4) |
||
172 | |||
173 | public function print($msg, $path = '| ', $len = null) |
||
187 | |||
188 | public function printHeader($msg) |
||
200 | |||
201 | public function end() |
||
206 | |||
207 | public function printLink($path, $lineNumber = 4) |
||
214 | |||
215 | /** |
||
216 | * Checks for errors for the run command. |
||
217 | * |
||
218 | * @return int |
||
219 | */ |
||
220 | public function hasErrors() |
||
228 | |||
229 | public function logErrors() |
||
245 | |||
246 | private static function possibleFixMsg($pieces) |
||
253 | |||
254 | public function wrongImportPossibleFixes($absPath, $class, $line, $fixes) |
||
259 | |||
260 | public function getCount($key) |
||
264 | |||
265 | public function printTime() |
||
269 | |||
270 | public static function thanks($command) |
||
281 | |||
282 | public static function warnIncorrectNamespace($currentNamespace, $relativePath, $class) |
||
295 | |||
296 | public static function ask($command, $correctNamespace) |
||
304 | } |
||
305 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.