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 | |||
| 58 | /** |
||
| 59 | * Supported MIME types for OCR processing |
||
| 60 | */ |
||
| 61 | private static $ALLOWED_MIMETYPES = [ |
||
|
|
|||
| 62 | 'application/pdf', |
||
| 63 | 'image/png', |
||
| 64 | 'image/jpeg', |
||
| 65 | 'image/tiff', |
||
| 66 | 'image/jp2', |
||
| 67 | 'image/jpm', |
||
| 68 | 'image/jpx', |
||
| 69 | 'image/webp', |
||
| 70 | 'image/gif' |
||
| 71 | ]; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The correct MIME type for a PDF file |
||
| 75 | */ |
||
| 76 | private static $MIMETYPE_PDF = 'application/pdf'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The supported image MIME types by tesseract |
||
| 80 | */ |
||
| 81 | private static $MIMETYPES_IMAGE = [ |
||
| 82 | 'image/png', |
||
| 83 | 'image/jpeg', |
||
| 84 | 'image/tiff', |
||
| 85 | 'image/jp2', |
||
| 86 | 'image/jpm', |
||
| 87 | 'image/jpx', |
||
| 88 | 'image/webp', |
||
| 89 | 'image/gif' |
||
| 90 | ]; |
||
| 91 | public function __construct(IL10N $l10n, ILogger $logger, $userId, FileMapper $fileMapper, ShareMapper $shareMapper) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Checks if shared with the process initiator |
||
| 101 | * |
||
| 102 | * @param File $fileInfo |
||
| 103 | * @return boolean|null |
||
| 104 | */ |
||
| 105 | public function checkSharedWithInitiator($fileInfo) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Builds the target name. |
||
| 122 | * |
||
| 123 | * @param File $fileInfo |
||
| 124 | * @param boolean $shared |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function buildTarget($fileInfo, $shared) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Builds the source name. |
||
| 138 | * |
||
| 139 | * @param File $fileInfo |
||
| 140 | * @param boolean $shared |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function buildSource($fileInfo, $shared) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Returns the fileInfo for each file in files and checks |
||
| 155 | * if it has a allowed MIME type and some other conditions. |
||
| 156 | * |
||
| 157 | * @param array $files |
||
| 158 | * @return File[] |
||
| 159 | * @throws NotFoundException |
||
| 160 | */ |
||
| 161 | public function buildFileInfo($files) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Removes ".txt" from the newName of a OCR job |
||
| 184 | * |
||
| 185 | * @param $job OcrJob |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function removeFileExtension($job) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Determines the correct type for the ocr process worker. |
||
| 194 | * TODO: adjust according to expected |
||
| 195 | * |
||
| 196 | * @param File $fileInfo |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function getCorrectType($fileInfo) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Executes the exec function with a remove statement for a given file path. |
||
| 209 | * |
||
| 210 | * @codeCoverageIgnore |
||
| 211 | * |
||
| 212 | * @param string $pathToFile |
||
| 213 | */ |
||
| 214 | public function execRemove($pathToFile) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Wraps the static file_get_contents method of php. |
||
| 220 | * |
||
| 221 | * @codeCoverageIgnore |
||
| 222 | * |
||
| 223 | * @param string $pathToFile |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function getFileContents($pathToFile) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Wraps the static file_exists method of php. |
||
| 232 | * |
||
| 233 | * @codeCoverageIgnore |
||
| 234 | * |
||
| 235 | * @param string $pathToFile |
||
| 236 | * @return boolean |
||
| 237 | */ |
||
| 238 | public function fileExists($pathToFile) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Returns a not existing file name for pdf or image processing |
||
| 244 | * protected as of testing issues with static methods. |
||
| 245 | * (Actually |
||
| 246 | * it will be mocked partially) FIXME: Change this behaviour as soon as the buidlNotExistingFileName function is not static anymore |
||
| 247 | * @codeCoverageIgnore |
||
| 248 | * |
||
| 249 | * @param File $fileInfo |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | protected function buildTargetForShared(File $fileInfo) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns a not existing file name for PDF or image processing |
||
| 285 | * protected as of testing issues with static methods. |
||
| 286 | * (Actually |
||
| 287 | * it will be mocked partially) FIXME: Change this behaviour as soon as the buidlNotExistingFileName function is not static anymore |
||
| 288 | * @codeCoverageIgnore |
||
| 289 | * |
||
| 290 | * @param File $fileInfo |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | protected function buildTargetNotForShared(File $fileInfo) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Checks a MIME type for a specifically given FileInfo. |
||
| 327 | * |
||
| 328 | * @param File $fileInfo |
||
| 329 | */ |
||
| 330 | private function checkMimeType(File $fileInfo) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Handle the possible thrown Exceptions from all methods of this class. |
||
| 345 | * |
||
| 346 | * @param Exception $e |
||
| 347 | * @throws Exception |
||
| 348 | * @throws NotFoundException |
||
| 349 | */ |
||
| 350 | View Code Duplication | private function handleException($e) { |
|
| 361 | } |
This check marks private properties in classes that are never used. Those properties can be removed.