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 |
||
| 22 | class Item { |
||
| 23 | /** |
||
| 24 | * file handle, user to read from the file |
||
| 25 | * @var resource |
||
| 26 | */ |
||
| 27 | protected $fileHandle; |
||
| 28 | |||
| 29 | /** @var AppConfig */ |
||
| 30 | private $config; |
||
| 31 | |||
| 32 | /** @var ActivityManager */ |
||
| 33 | private $activityManager; |
||
| 34 | |||
| 35 | /** @var ItemMapper */ |
||
| 36 | private $itemMapper; |
||
| 37 | |||
| 38 | /** @var ILogger */ |
||
| 39 | private $logger; |
||
| 40 | |||
| 41 | /** @var IRootFolder */ |
||
| 42 | private $rootFolder; |
||
| 43 | |||
| 44 | /** @var File */ |
||
| 45 | private $file; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Item constructor. |
||
| 49 | * |
||
| 50 | * @param AppConfig $appConfig |
||
| 51 | * @param ActivityManager $activityManager |
||
| 52 | * @param ItemMapper $itemMapper |
||
| 53 | * @param ILogger $logger |
||
| 54 | * @param IRootFolder $rootFolder |
||
| 55 | * @param File $file |
||
| 56 | */ |
||
| 57 | View Code Duplication | public function __construct(AppConfig $appConfig, |
|
|
|
|||
| 58 | ActivityManager $activityManager, |
||
| 59 | ItemMapper $itemMapper, |
||
| 60 | ILogger $logger, |
||
| 61 | IRootFolder $rootFolder, |
||
| 62 | File $file) { |
||
| 63 | $this->config = $appConfig; |
||
| 64 | $this->activityManager = $activityManager; |
||
| 65 | $this->itemMapper = $itemMapper; |
||
| 66 | $this->logger = $logger; |
||
| 67 | $this->rootFolder = $rootFolder; |
||
| 68 | $this->file = $file; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Reads a file portion by portion until the very end |
||
| 73 | * @return string|boolean |
||
| 74 | */ |
||
| 75 | public function fread() { |
||
| 76 | if (!($this->file->getSize() > 0)) { |
||
| 77 | return false; |
||
| 78 | } |
||
| 79 | |||
| 80 | if (is_null($this->fileHandle)) { |
||
| 81 | $this->getFileHandle(); |
||
| 82 | } |
||
| 83 | |||
| 84 | if (!is_null($this->fileHandle) && !$this->feof()) { |
||
| 85 | return fread($this->fileHandle, $this->config->getAvChunkSize()); |
||
| 86 | } |
||
| 87 | return false; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Action to take if this item is infected |
||
| 92 | */ |
||
| 93 | public function processInfected(Status $status) { |
||
| 94 | $infectedAction = $this->config->getAvInfectedAction(); |
||
| 95 | |||
| 96 | $shouldDelete = $infectedAction === 'delete'; |
||
| 97 | |||
| 98 | $message = $shouldDelete ? Provider::MESSAGE_FILE_DELETED : ''; |
||
| 99 | |||
| 100 | $userFolder = $this->rootFolder->getUserFolder($this->file->getOwner()->getUID()); |
||
| 101 | $path = $userFolder->getRelativePath($this->file->getPath()); |
||
| 102 | |||
| 103 | $activity = $this->activityManager->generateEvent(); |
||
| 104 | $activity->setApp(Application::APP_NAME) |
||
| 105 | ->setSubject(Provider::SUBJECT_VIRUS_DETECTED_SCAN, [$status->getDetails()]) |
||
| 106 | ->setMessage($message) |
||
| 107 | ->setObject('file', $this->file->getId(), $path) |
||
| 108 | ->setAffectedUser($this->file->getOwner()->getUID()) |
||
| 109 | ->setType(Provider::TYPE_VIRUS_DETECTED); |
||
| 110 | $this->activityManager->publish($activity); |
||
| 111 | |||
| 112 | if ($shouldDelete) { |
||
| 113 | $this->logError('Infected file deleted. ' . $status->getDetails()); |
||
| 114 | $this->deleteFile(); |
||
| 115 | } else { |
||
| 116 | $this->logError('File is infected. ' . $status->getDetails()); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Action to take if this item status is unclear |
||
| 122 | * @param Status $status |
||
| 123 | */ |
||
| 124 | public function processUnchecked(Status $status) { |
||
| 125 | //TODO: Show warning to the user: The file can not be checked |
||
| 126 | $this->logError('Not Checked. ' . $status->getDetails()); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Action to take if this item status is not infected |
||
| 131 | */ |
||
| 132 | public function processClean() { |
||
| 133 | try { |
||
| 134 | try { |
||
| 135 | $item = $this->itemMapper->findByFileId($this->file->getId()); |
||
| 136 | $this->itemMapper->delete($item); |
||
| 137 | } catch (DoesNotExistException $e) { |
||
| 138 | //Just ignore |
||
| 139 | } |
||
| 140 | |||
| 141 | $item = new \OCA\Files_Antivirus\Db\Item(); |
||
| 142 | $item->setFileid($this->file->getId()); |
||
| 143 | $item->setCheckTime(time()); |
||
| 144 | $this->itemMapper->insert($item); |
||
| 145 | } catch(\Exception $e) { |
||
| 146 | $this->logger->error(__METHOD__.', exception: '.$e->getMessage(), ['app' => 'files_antivirus']); |
||
| 147 | } |
||
| 148 | } |
||
| 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.