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 |
||
26 | class FileService { |
||
27 | |||
28 | /** |
||
29 | * |
||
30 | * @var ILogger |
||
31 | */ |
||
32 | private $logger; |
||
33 | |||
34 | /** |
||
35 | * |
||
36 | * @var FileMapper |
||
37 | */ |
||
38 | private $fileMapper; |
||
39 | |||
40 | /** |
||
41 | * |
||
42 | * @var ShareMapper |
||
43 | */ |
||
44 | private $shareMapper; |
||
45 | |||
46 | /** |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | private $userId; |
||
51 | |||
52 | /** |
||
53 | * |
||
54 | * @var IL10N |
||
55 | */ |
||
56 | private $l10n; |
||
57 | 6 | public function __construct(IL10N $l10n, ILogger $logger, $userId, FileMapper $fileMapper, ShareMapper $shareMapper) { |
|
64 | |||
65 | /** |
||
66 | * Checks if shared with the process initiator |
||
67 | * |
||
68 | * @param File $fileInfo |
||
69 | * @return boolean|null |
||
70 | */ |
||
71 | public function checkSharedWithInitiator($fileInfo) { |
||
81 | |||
82 | /** |
||
83 | * Builds the target name. |
||
84 | * |
||
85 | * @param File $fileInfo |
||
86 | * @param boolean $shared |
||
87 | * @return string |
||
88 | */ |
||
89 | public function buildTarget($fileInfo, $shared) { |
||
97 | |||
98 | /** |
||
99 | * Builds the source name. |
||
100 | * |
||
101 | * @param File $fileInfo |
||
102 | * @param boolean $shared |
||
103 | * @return string |
||
104 | */ |
||
105 | public function buildSource($fileInfo, $shared) { |
||
114 | |||
115 | /** |
||
116 | * Returns the fileInfo for each file in files and checks |
||
117 | * if it has a allowed MIME type and some other conditions. |
||
118 | * |
||
119 | * @param array $files |
||
120 | * @return File[] |
||
121 | * @throws NotFoundException |
||
122 | */ |
||
123 | public function buildFileInfo($files) { |
||
139 | |||
140 | /** |
||
141 | * Determines the correct type for the ocr process worker. |
||
142 | * |
||
143 | * @param File $fileInfo |
||
144 | * @return integer |
||
145 | */ |
||
146 | public function getCorrectType($fileInfo) { |
||
153 | |||
154 | /** |
||
155 | * Executes the exec function with a remove statement for a given file path. |
||
156 | * |
||
157 | * @codeCoverageIgnore |
||
158 | * |
||
159 | * @param string $pathToFile |
||
160 | */ |
||
161 | public function execRemove($pathToFile) { |
||
164 | |||
165 | /** |
||
166 | * Wraps the static file_get_contents method of php. |
||
167 | * |
||
168 | * @codeCoverageIgnore |
||
169 | * |
||
170 | * @param string $pathToFile |
||
171 | * @return string |
||
172 | */ |
||
173 | public function getFileContents($pathToFile) { |
||
176 | |||
177 | /** |
||
178 | * Wraps the static file_exists method of php. |
||
179 | * |
||
180 | * @codeCoverageIgnore |
||
181 | * |
||
182 | * @param string $pathToFile |
||
183 | * @return boolean |
||
184 | */ |
||
185 | public function fileExists($pathToFile) { |
||
188 | |||
189 | /** |
||
190 | * Wraps the static function \OCP\Files::buildNotExistingFileName() in order to be able to test everything else. |
||
191 | * |
||
192 | * @codeCoverageIgnore |
||
193 | * |
||
194 | * @param string $filePath |
||
195 | * @param string $fileName |
||
196 | * @return string |
||
197 | */ |
||
198 | public function buildNotExistingFilename($filePath, $fileName) { |
||
201 | |||
202 | /** |
||
203 | * Returns a not existing file name for pdf or image processing |
||
204 | * protected as of testing issues with static methods. |
||
205 | * (Actually |
||
206 | * it will be mocked partially) FIXME: Change this behaviour as soon as the buidlNotExistingFileName function is not static anymore |
||
207 | * @codeCoverageIgnore |
||
208 | * |
||
209 | * @param File $fileInfo |
||
210 | * @return string |
||
211 | */ |
||
212 | private function buildTargetForShared(File $fileInfo) { |
||
238 | |||
239 | /** |
||
240 | * Returns a not existing file name for PDF or image processing |
||
241 | * protected as of testing issues with static methods. |
||
242 | * (Actually |
||
243 | * it will be mocked partially) FIXME: Change this behaviour as soon as the buidlNotExistingFileName function is not static anymore |
||
244 | * @codeCoverageIgnore |
||
245 | * |
||
246 | * @param File $fileInfo |
||
247 | * @return string |
||
248 | */ |
||
249 | private function buildTargetNotForShared(File $fileInfo) { |
||
276 | |||
277 | /** |
||
278 | * Checks a MIME type for a specifically given FileInfo. |
||
279 | * |
||
280 | * @param File $fileInfo |
||
281 | */ |
||
282 | private function checkMimeType(File $fileInfo) { |
||
288 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.