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 | public function route($path, $errorIt, $errorTxt, $absPath = null, $lineNumber = 0) |
||
34 | { |
||
35 | $this->simplePendError($path, $absPath, $lineNumber, 'route', $errorIt, '', $errorTxt); |
||
36 | } |
||
37 | |||
38 | public function authConf() |
||
39 | { |
||
40 | $this->print('The model in the "config/auth.php" is not a valid class'); |
||
41 | } |
||
42 | |||
43 | public function badRelation($absPath, $lineNumber, $relatedModel) |
||
44 | { |
||
45 | $header = 'Wrong model is passed in relation:'; |
||
46 | |||
47 | $this->doesNotExist($relatedModel, $absPath, $lineNumber, 'badRelation', $header); |
||
48 | } |
||
49 | |||
50 | public function doesNotExist($yellowText, $absPath, $lineNumber, $key, $header) |
||
51 | { |
||
52 | $this->simplePendError($yellowText, $absPath, $lineNumber, $key, $header, ' <==== does not exist'); |
||
53 | } |
||
54 | |||
55 | public function routelessAction($absPath, $lineNumber, $msg) |
||
56 | { |
||
57 | $this->simplePendError($msg, $absPath, $lineNumber, 'routelessCtrl', 'No route is defined for controller action:'); |
||
58 | } |
||
59 | |||
60 | View Code Duplication | public function simplePendError($yellowText, $absPath, $lineNumber, $key, $header, $rest = '', $pre = '') |
|
|
|||
61 | { |
||
62 | if (LaravelPaths::isIgnored($absPath)) { |
||
63 | return; |
||
64 | } |
||
65 | |||
66 | ($this->errorsList[$key][] = (new PendingError($key)) |
||
67 | ->header($header) |
||
68 | ->errorData($pre.$this->yellow($yellowText). $rest) |
||
69 | ->link($absPath, $lineNumber)); |
||
70 | } |
||
71 | |||
72 | public function wrongImport($absPath, $class, $lineNumber) |
||
73 | { |
||
74 | $this->doesNotExist("use $class;", $absPath, $lineNumber, 'wrongImport', 'Wrong import:'); |
||
75 | } |
||
76 | |||
77 | View Code Duplication | public function compactError($path, $lineNumber, $absent, $key, $header) |
|
78 | { |
||
79 | if (LaravelPaths::isIgnored($path)) { |
||
80 | return; |
||
81 | } |
||
82 | |||
83 | ($this->errorsList[$key][] = (new PendingError($key)) |
||
84 | ->header($header) |
||
85 | ->errorData($this->yellow(\implode(', ', array_keys($absent))).' does not exist') |
||
86 | ->link($path, $lineNumber)); |
||
87 | } |
||
88 | |||
89 | public function routeDefinitionConflict($route1, $route2, $info) |
||
90 | { |
||
91 | if (LaravelPaths::isIgnored($info[0]['file'] ?? 'unknown')) { |
||
92 | return; |
||
93 | } |
||
94 | |||
95 | $key = 'routeDefinitionConflict'; |
||
96 | $routeName = $route1->getName(); |
||
97 | if ($routeName) { |
||
98 | $routeName = $this->yellow($routeName); |
||
99 | $msg = 'Route name: '.$routeName; |
||
100 | } else { |
||
101 | $routeUri = $route1->uri(); |
||
102 | $routeUri = $this->yellow($routeUri); |
||
103 | $msg = 'Route uri: '.$routeUri; |
||
104 | } |
||
105 | |||
106 | $msg .= "\n".' at '.($info[0]['file'] ?? 'unknown').':'.($info[0]['line'] ?? 2); |
||
107 | $msg .= "\n".' is overridden by '; |
||
108 | |||
109 | $routeName = $route2->getName(); |
||
110 | if ($routeName) { |
||
111 | $routeName = $this->yellow($routeName); |
||
112 | $msg .= 'route name: '.$routeName; |
||
113 | } else { |
||
114 | $msg .= 'an other route with same uri.'; |
||
115 | } |
||
116 | |||
117 | $msg .= "\n".' at '.($info[1]['file'] ?? ' ').':'.$info[1]['line']."\n"; |
||
118 | |||
119 | $methods = \implode(',', $route1->methods()); |
||
120 | |||
121 | $this->errorsList[$key][$methods] = (new PendingError($key)) |
||
122 | ->header('Route with uri: '.$this->yellow($methods.': /'.$route1->uri()).' is overridden.') |
||
123 | ->errorData($msg); |
||
124 | } |
||
125 | |||
126 | public function wrongUsedClassError($absPath, $class, $lineNumber) |
||
127 | { |
||
128 | $this->doesNotExist($class, $absPath, $lineNumber, 'wrongUsedClassError', 'Class does not exist:'); |
||
129 | } |
||
130 | |||
131 | View Code Duplication | public function queryInBlade($absPath, $class, $lineNumber) |
|
132 | { |
||
133 | if (LaravelPaths::isIgnored($absPath)) { |
||
134 | return; |
||
135 | } |
||
136 | |||
137 | $key = 'queryInBlade'; |
||
138 | ($this->errorsList[$key][] = (new PendingError($key)) |
||
139 | ->header('Query in blade file: ') |
||
140 | ->errorData($this->yellow($class).' <=== DB query in blade file') |
||
141 | ->link($absPath, $lineNumber)); |
||
142 | } |
||
143 | |||
144 | public function wrongMethodError($absPath, $class, $lineNumber) |
||
145 | { |
||
146 | $this->doesNotExist($class, $absPath, $lineNumber, 'wrongMethodError', 'Method does not exist:'); |
||
147 | } |
||
148 | |||
149 | public function yellow($msg) |
||
150 | { |
||
151 | return "<fg=yellow>$msg</>"; |
||
152 | } |
||
153 | |||
154 | public function badNamespace($absPath, $correctNamespace, $incorrectNamespace, $lineNumber = 4) |
||
155 | { |
||
156 | if (LaravelPaths::isIgnored($absPath)) { |
||
157 | return; |
||
158 | } |
||
159 | |||
160 | ($this->errorsList['badNamespace'][] = (new PendingError('badNamespace')) |
||
161 | ->header('Incorrect namespace: '.$this->yellow("namespace $incorrectNamespace;")) |
||
162 | ->errorData(' namespace fixed to: '.$this->yellow("namespace $correctNamespace;")) |
||
163 | ->link($absPath, $lineNumber)); |
||
164 | } |
||
165 | |||
166 | public function print($msg, $path = '| ', $len = null) |
||
167 | { |
||
168 | ! $len && $len = PendingError::$maxLength + 1; |
||
169 | $msgLen = strlen($msg); |
||
170 | if (strpos($msg, 'yellow')) { |
||
171 | $msgLen = $msgLen - 14; |
||
172 | } |
||
173 | $len = $len - $msgLen; |
||
174 | if ($len < 0) { |
||
175 | $len = 0; |
||
176 | } |
||
177 | |||
178 | $this->printer->writeln($path.$msg.str_repeat(' ', $len).'|'); |
||
179 | } |
||
180 | |||
181 | public function printHeader($msg) |
||
182 | { |
||
183 | $number = ++$this->errorsList['total']; |
||
184 | ($number < 10) && $number = " $number"; |
||
185 | |||
186 | $number = '<fg=yellow>'.$number.' </>'; |
||
187 | $path = "| $number"; |
||
188 | |||
189 | PendingError::$maxLength = max(PendingError::$maxLength, strlen($msg)); |
||
190 | $this->print(''); |
||
191 | $this->print($msg, $path, PendingError::$maxLength - 1); |
||
192 | } |
||
193 | |||
194 | public function end() |
||
199 | |||
200 | public function printLink($path, $lineNumber = 4) |
||
207 | |||
208 | /** |
||
209 | * Checks for errors for the run command. |
||
210 | * |
||
211 | * @return int |
||
212 | */ |
||
213 | public function hasErrors() |
||
221 | |||
222 | public function logErrors() |
||
238 | |||
239 | private static function possibleFixMsg($pieces) |
||
240 | { |
||
241 | $fixes = \implode("\n - ", $pieces); |
||
242 | $fixes && $fixes = "\n Possible fixes:\n - ".$fixes; |
||
243 | |||
244 | return $fixes; |
||
245 | } |
||
246 | |||
247 | public function wrongImportPossibleFixes($absPath, $class, $line, $fixes) |
||
248 | { |
||
249 | $fixes = self::possibleFixMsg($fixes); |
||
250 | $this->wrongUsedClassError($absPath, $class.' <=== \(-_-)/ '.$fixes, $line); |
||
251 | } |
||
252 | |||
253 | public function getCount($key) |
||
254 | { |
||
255 | return \count($this->errorsList[$key] ?? []); |
||
256 | } |
||
257 | |||
258 | public function printTime() |
||
259 | { |
||
262 | |||
263 | public static function thanks($command) |
||
274 | |||
275 | public static function warnIncorrectNamespace($currentNamespace, $relativePath, $class) |
||
288 | |||
289 | public static function ask($command, $correctNamespace) |
||
297 | } |
||
298 |
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.