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 | 23 | public function __construct(IL10N $l10n, ILogger $logger, $userId, FileMapper $fileMapper, ShareMapper $shareMapper, |
|
75 | |||
76 | /** |
||
77 | * Checks if shared with the process initiator |
||
78 | * |
||
79 | * @param File $fileInfo |
||
80 | * @return boolean |
||
81 | */ |
||
82 | 2 | public function checkSharedWithInitiator($fileInfo) { |
|
92 | |||
93 | /** |
||
94 | * Builds the target name. |
||
95 | * |
||
96 | * @param File $fileInfo |
||
97 | * @param boolean $shared |
||
98 | * @param boolean $replace |
||
99 | * @return string |
||
100 | */ |
||
101 | 8 | public function buildTarget($fileInfo, $shared, $replace) { |
|
109 | |||
110 | /** |
||
111 | * Builds the source name. |
||
112 | * |
||
113 | * @param File $fileInfo |
||
114 | * @param boolean $shared |
||
115 | * @return string |
||
116 | */ |
||
117 | 2 | public function buildSource($fileInfo, $shared) { |
|
126 | |||
127 | /** |
||
128 | * Returns the fileInfo for each file in files and checks |
||
129 | * if it has a allowed MIME type and some other conditions. |
||
130 | * |
||
131 | * @param array $files |
||
132 | * @return File[] |
||
133 | * @throws NotFoundException |
||
134 | */ |
||
135 | 4 | public function buildFileInfo($files) { |
|
149 | |||
150 | /** |
||
151 | * Determines the correct type for the ocr process worker. |
||
152 | * |
||
153 | * @param File $fileInfo |
||
154 | * @return integer |
||
155 | */ |
||
156 | 2 | public function getCorrectType($fileInfo) { |
|
163 | |||
164 | /** |
||
165 | * Returns a not existing file name for pdf or image processing. |
||
166 | * |
||
167 | * @param File $fileInfo |
||
168 | * @param boolean $replace |
||
169 | * @return string |
||
170 | */ |
||
171 | 4 | private function buildTargetForShared(File $fileInfo, $replace) { |
|
204 | |||
205 | /** |
||
206 | * Returns a not existing file name for PDF or image processing. |
||
207 | * |
||
208 | * @param File $fileInfo |
||
209 | * @param boolean $replace |
||
210 | * @return string |
||
211 | */ |
||
212 | 4 | private function buildTargetNotForShared(File $fileInfo, $replace) { |
|
246 | |||
247 | /** |
||
248 | * Checks a MIME type for a specifically given FileInfo. |
||
249 | * |
||
250 | * @param File $fileInfo |
||
251 | */ |
||
252 | 3 | private function checkMimeType(File $fileInfo) { |
|
258 | } |
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.