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 |
||
21 | class Item implements IScannable{ |
||
22 | /** |
||
23 | * file handle, user to read from the file |
||
24 | * @var resource |
||
25 | */ |
||
26 | protected $fileHandle; |
||
27 | |||
28 | /** @var IL10N */ |
||
29 | private $l10n; |
||
30 | |||
31 | /** @var AppConfig */ |
||
32 | private $config; |
||
33 | |||
34 | /** @var ActivityManager */ |
||
35 | private $activityManager; |
||
36 | |||
37 | /** @var ItemMapper */ |
||
38 | private $itemMapper; |
||
39 | |||
40 | /** @var ILogger */ |
||
41 | private $logger; |
||
42 | |||
43 | /** @var File */ |
||
44 | private $file; |
||
45 | |||
46 | View Code Duplication | public function __construct(IL10N $l10n, |
|
59 | |||
60 | /** |
||
61 | * Is this file good for scanning? |
||
62 | * @return boolean |
||
63 | */ |
||
64 | public function isValid() { |
||
67 | |||
68 | /** |
||
69 | * Reads a file portion by portion until the very end |
||
70 | * @return string|boolean |
||
71 | */ |
||
72 | public function fread() { |
||
85 | |||
86 | /** |
||
87 | * Action to take if this item is infected |
||
88 | * @param Status $status |
||
89 | */ |
||
90 | public function processInfected(Status $status) { |
||
113 | |||
114 | /** |
||
115 | * Action to take if this item status is unclear |
||
116 | * @param Status $status |
||
117 | * @param boolean $isBackground |
||
118 | */ |
||
119 | public function processUnchecked(Status $status) { |
||
123 | |||
124 | /** |
||
125 | * Action to take if this item status is not infected |
||
126 | * @param Status $status |
||
127 | * @param boolean $isBackground |
||
128 | */ |
||
129 | public function processClean(Status $status, $isBackground) { |
||
149 | |||
150 | /** |
||
151 | * Check if the end of file is reached |
||
152 | * @return boolean |
||
153 | */ |
||
154 | private function feof() { |
||
163 | |||
164 | /** |
||
165 | * Opens a file for reading |
||
166 | * @throws \RuntimeException |
||
167 | */ |
||
168 | private function getFileHandle() { |
||
178 | |||
179 | /** |
||
180 | * Delete infected file |
||
181 | */ |
||
182 | private function deleteFile() { |
||
192 | |||
193 | /** |
||
194 | * @param string $message |
||
195 | */ |
||
196 | public function logDebug($message) { |
||
202 | |||
203 | /** |
||
204 | * @param string $message |
||
205 | */ |
||
206 | public function logError($message) { |
||
213 | } |
||
214 |
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.