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 |
||
15 | class PhpCsLoader implements FileChecker |
||
16 | { |
||
17 | /** |
||
18 | * @var stdClass |
||
19 | */ |
||
20 | protected $json; |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $invalidLines; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | |||
30 | protected $failOnTypes = [ |
||
31 | 'ERROR', |
||
32 | ]; |
||
33 | |||
34 | protected $lookupErrorPrefix = [ |
||
35 | 'Squiz.Commenting.FileComment', |
||
36 | 'Squiz.Commenting.ClassComment', |
||
37 | 'Squiz.Commenting.FunctionComment', |
||
38 | ]; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $wholeFileErrors = [ |
||
44 | 'PSR1.Files.SideEffects.FoundWithSymbols', |
||
45 | 'Generic.Files.LineEndings.InvalidEOLChar', |
||
46 | ]; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $invalidFiles = []; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $invalidRanges = []; |
||
58 | |||
59 | |||
60 | /** |
||
61 | * @var FileParser[] |
||
62 | */ |
||
63 | protected $parsedFiles = []; |
||
64 | |||
65 | /** |
||
66 | * PhpCsLoader constructor. |
||
67 | * @param string $filePath the file path to the json output from phpcs |
||
68 | */ |
||
69 | View Code Duplication | public function __construct($filePath) |
|
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | public function parseLines() |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | public function getErrorsOnLine($file, $lineNumber) |
||
116 | |||
117 | /** |
||
118 | * @param string $file |
||
119 | * @param stdClass $message |
||
120 | */ |
||
121 | protected function addInvalidLine($file, $message) |
||
144 | |||
145 | /** |
||
146 | * @param $message |
||
147 | * @param array $list |
||
148 | * @return bool|string |
||
149 | */ |
||
150 | protected function messageStartsWith($message, array $list) |
||
159 | |||
160 | protected function handleLookupError($file, $message, $error) |
||
174 | |||
175 | protected function getFileParser($filename) |
||
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | public function handleNotFoundFile() |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public static function getDescription() |
||
206 | |||
207 | protected function addRangeError($file, $lookup, $message) |
||
220 | |||
221 | protected function getMessageRanges($error, $fileParser) |
||
229 | |||
230 | /** |
||
231 | * @param string $file |
||
232 | * @param int $lineNumber |
||
233 | * @return array errors on the line |
||
234 | */ |
||
235 | protected function getRangeErrors($file, $lineNumber) |
||
251 | } |
||
252 |
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.