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 |
||
11 | class PhpMdLoader implements FileChecker |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $file; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $errors = []; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $errorRanges = []; |
||
27 | |||
28 | /** |
||
29 | * PhpMdLoader constructor. |
||
30 | * @param string $file the path to the phpmd xml file |
||
31 | */ |
||
32 | public function __construct($file) |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | public function getLines() |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | public function isValidLine($file, $lineNumber) |
||
74 | |||
75 | /** |
||
76 | * @param XMLReader $reader |
||
77 | * @param string $currentFile |
||
78 | */ |
||
79 | protected function checkForViolation(XMLReader $reader, $currentFile) |
||
97 | |||
98 | /** |
||
99 | * @param XMLReader $reader |
||
100 | * @param string $currentFile |
||
101 | * @return string the currentFileName |
||
102 | */ |
||
103 | View Code Duplication | protected function checkForNewFile(XMLReader $reader, $currentFile) |
|
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function handleNotFoundFile() |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public static function getDescription() |
||
134 | |||
135 | protected function addForAllLines($currentFile, $start, $end, $error) |
||
146 | } |
||
147 |
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.