| Conditions | 8 |
| Paths | 5 |
| Total Lines | 67 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 60 | public function fopen($path, $mode){ |
||
| 61 | $stream = $this->storage->fopen($path, $mode); |
||
| 62 | if (is_resource($stream) |
||
| 63 | && $this->isWritingMode($mode) |
||
| 64 | && strpos($path, 'uploads/') !== 0 |
||
| 65 | ) { |
||
| 66 | try { |
||
| 67 | $scanner = $this->scannerFactory->getScanner(); |
||
| 68 | $scanner->initScanner(); |
||
| 69 | return CallBackWrapper::wrap( |
||
| 70 | $stream, |
||
| 71 | null, |
||
| 72 | function ($data) use ($scanner){ |
||
| 73 | $scanner->onAsyncData($data); |
||
| 74 | }, |
||
| 75 | function () use ($scanner, $path) { |
||
| 76 | $status = $scanner->completeAsyncScan(); |
||
| 77 | if (intval($status->getNumericStatus()) === \OCA\Files_Antivirus\Status::SCANRESULT_INFECTED){ |
||
| 78 | //prevent from going to trashbin |
||
| 79 | if (App::isEnabled('files_trashbin')) { |
||
| 80 | \OCA\Files_Trashbin\Storage::preRenameHook([ |
||
| 81 | Filesystem::signal_param_oldpath => '', |
||
| 82 | Filesystem::signal_param_newpath => '' |
||
| 83 | ]); |
||
| 84 | } |
||
| 85 | |||
| 86 | $owner = $this->getOwner($path); |
||
| 87 | $this->unlink($path); |
||
| 88 | |||
| 89 | if (App::isEnabled('files_trashbin')) { |
||
| 90 | \OCA\Files_Trashbin\Storage::postRenameHook([]); |
||
| 91 | } |
||
| 92 | $this->logger->warning( |
||
| 93 | 'Infected file deleted. ' . $status->getDetails() |
||
| 94 | . ' Account: ' . $owner . ' Path: ' . $path, |
||
| 95 | ['app' => 'files_antivirus'] |
||
| 96 | ); |
||
| 97 | |||
| 98 | \OC::$server->getActivityManager()->publishActivity( |
||
| 99 | 'files_antivirus', |
||
| 100 | Activity::SUBJECT_VIRUS_DETECTED, |
||
| 101 | [$path, $status->getDetails()], |
||
| 102 | Activity::MESSAGE_FILE_DELETED, |
||
| 103 | [], |
||
| 104 | $path, |
||
| 105 | '', |
||
| 106 | $owner, |
||
| 107 | Activity::TYPE_VIRUS_DETECTED, |
||
| 108 | Activity::PRIORITY_HIGH |
||
| 109 | ); |
||
| 110 | |||
| 111 | throw new InvalidContentException( |
||
| 112 | $this->l10n->t( |
||
| 113 | 'Virus %s is detected in the file. Upload cannot be completed.', |
||
| 114 | $status->getDetails() |
||
| 115 | ) |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | ); |
||
| 120 | } catch (\Exception $e){ |
||
| 121 | $message = implode(' ', [ __CLASS__, __METHOD__, $e->getMessage()]); |
||
| 122 | $this->logger->warning($message); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | return $stream; |
||
| 126 | } |
||
| 127 | |||
| 143 |