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:
1 | <?php |
||
7 | class ErrorPrinter |
||
8 | { |
||
9 | public $counts |
||
10 | = [ |
||
11 | 'view' => [], |
||
12 | 'route' => [], |
||
13 | 'total' => 0, |
||
14 | 'bladeImport' => [], |
||
15 | 'badRelation' => [], |
||
16 | 'wrongImport' => [], |
||
17 | 'wrongUsedClassError' => [], |
||
18 | 'badNamespace' => [], |
||
19 | 'others' => [], |
||
20 | ]; |
||
21 | |||
22 | public $printer; |
||
23 | |||
24 | public $logErrors = true; |
||
25 | |||
26 | View Code Duplication | public function view($path, $lineContent, $lineNumber, $fileName) |
|
33 | |||
34 | View Code Duplication | public function others($error, $header = null, $linkPath = null, $linkLineNum = 0) |
|
41 | |||
42 | View Code Duplication | public function route($path, $errorIt, $errorTxt, $linkPath = null, $linkLineNum = 0) |
|
49 | |||
50 | public function bladeImport($class, $blade) |
||
57 | |||
58 | public function authConf() |
||
62 | |||
63 | public function badRelation(\ReflectionClass $ref, \ReflectionMethod $method, $relatedModel) |
||
70 | |||
71 | View Code Duplication | public function wrongImport($absPath, $class, $line) |
|
78 | |||
79 | public function wrongUsedClassError($absFilePath, $nonImportedClass) |
||
86 | |||
87 | public function yellow($msg) |
||
91 | |||
92 | /** |
||
93 | * @param string $classPath |
||
94 | * @param string $correctNamespace |
||
95 | * @param string $incorrectNamespace |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | public function badNamespace($classPath, $correctNamespace, $incorrectNamespace) |
||
107 | |||
108 | public function print($msg, $path = ' | ', $len = 81) |
||
121 | |||
122 | public function printHeader($msg) |
||
131 | |||
132 | public function end() |
||
137 | |||
138 | public function printLink($path, $lineNumber = 4) |
||
145 | |||
146 | public function fixedNamespace($correctNamespace) |
||
152 | |||
153 | /** |
||
154 | * Checks for errors for the run command. |
||
155 | * |
||
156 | * @return int |
||
157 | */ |
||
158 | public function hasErrors() |
||
168 | |||
169 | /** |
||
170 | * Logs the errors to the console. |
||
171 | */ |
||
172 | public function logErrors() |
||
186 | } |
||
187 |
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.