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 |
||
28 | class FileService { |
||
29 | |||
30 | /** |
||
31 | * |
||
32 | * @var ILogger |
||
33 | */ |
||
34 | private $logger; |
||
35 | |||
36 | /** |
||
37 | * |
||
38 | * @var FileMapper |
||
39 | */ |
||
40 | private $fileMapper; |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | * @var ShareMapper |
||
45 | */ |
||
46 | private $shareMapper; |
||
47 | |||
48 | /** |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | private $userId; |
||
53 | |||
54 | /** |
||
55 | * |
||
56 | * @var IL10N |
||
57 | */ |
||
58 | private $l10n; |
||
59 | |||
60 | /** |
||
61 | * |
||
62 | * @var FileUtil |
||
63 | */ |
||
64 | private $fileUtil; |
||
65 | |||
66 | 19 | public function __construct(IL10N $l10n, ILogger $logger, $userId, FileMapper $fileMapper, ShareMapper $shareMapper, FileUtil $fileUtil) { |
|
74 | |||
75 | /** |
||
76 | * Checks if shared with the process initiator |
||
77 | * |
||
78 | * @param File $fileInfo |
||
79 | * @return boolean |
||
80 | */ |
||
81 | 2 | public function checkSharedWithInitiator($fileInfo) { |
|
91 | |||
92 | /** |
||
93 | * Builds the target name. |
||
94 | * |
||
95 | * @param File $fileInfo |
||
96 | * @param boolean $shared |
||
97 | * @return string |
||
98 | */ |
||
99 | 4 | public function buildTarget($fileInfo, $shared) { |
|
107 | |||
108 | /** |
||
109 | * Builds the source name. |
||
110 | * |
||
111 | * @param File $fileInfo |
||
112 | * @param boolean $shared |
||
113 | * @return string |
||
114 | */ |
||
115 | 2 | public function buildSource($fileInfo, $shared) { |
|
124 | |||
125 | /** |
||
126 | * Returns the fileInfo for each file in files and checks |
||
127 | * if it has a allowed MIME type and some other conditions. |
||
128 | * |
||
129 | * @param array $files |
||
130 | * @return File[] |
||
131 | * @throws NotFoundException |
||
132 | */ |
||
133 | 4 | public function buildFileInfo($files) { |
|
147 | |||
148 | /** |
||
149 | * Determines the correct type for the ocr process worker. |
||
150 | * |
||
151 | * @param File $fileInfo |
||
152 | * @return integer |
||
153 | */ |
||
154 | 2 | public function getCorrectType($fileInfo) { |
|
161 | |||
162 | /** |
||
163 | * Returns a not existing file name for pdf or image processing. |
||
164 | * |
||
165 | * @param File $fileInfo |
||
166 | * @return string |
||
167 | */ |
||
168 | 2 | private function buildTargetForShared(File $fileInfo) { |
|
194 | |||
195 | /** |
||
196 | * Returns a not existing file name for PDF or image processing. |
||
197 | * |
||
198 | * @param File $fileInfo |
||
199 | * @return string |
||
200 | */ |
||
201 | 2 | private function buildTargetNotForShared(File $fileInfo) { |
|
228 | |||
229 | /** |
||
230 | * Checks a MIME type for a specifically given FileInfo. |
||
231 | * |
||
232 | * @param File $fileInfo |
||
233 | */ |
||
234 | 3 | private function checkMimeType(File $fileInfo) { |
|
240 | } |
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.