| Conditions | 12 |
| Paths | 13 |
| Total Lines | 41 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 16 | public function index() |
||
| 17 | { |
||
| 18 | $request = $this->getRequest(); |
||
| 19 | $ID = $request->getVar("ID"); |
||
| 20 | $Hash = $request->getVar("Hash"); |
||
| 21 | |||
| 22 | if (!$ID || !$Hash) { |
||
| 23 | return $this->httpError(404); |
||
| 24 | } |
||
| 25 | |||
| 26 | /** @var File|EncryptedDBFile $File */ |
||
| 27 | $File = File::get()->byID($ID); |
||
| 28 | if (!$File) { |
||
|
|
|||
| 29 | $File = Versioned::get_latest_version(File::class, $ID); |
||
| 30 | } |
||
| 31 | if (!$File) { |
||
| 32 | return $this->httpError(404); |
||
| 33 | } |
||
| 34 | |||
| 35 | // Verify hash |
||
| 36 | $FileHash = substr($File->File->Hash, 0, 10); |
||
| 37 | if ($Hash != $FileHash && !Permission::check("CMS_ACCESS")) { |
||
| 38 | return $this->httpError(404); |
||
| 39 | } |
||
| 40 | |||
| 41 | // Check protected |
||
| 42 | $sendProtected = $this->config()->send_protected; |
||
| 43 | $adminSendProtected = $this->config()->admin_send_protected; |
||
| 44 | $currentUserID = Security::getCurrentUser()->ID ?? 0; |
||
| 45 | $isOwner = $File->OwnerID === $currentUserID; |
||
| 46 | if ($File->getVisibility() == "protected") { |
||
| 47 | if (!$sendProtected && !$isOwner) { |
||
| 48 | if ($adminSendProtected && Permission::check("CMS_ACCESS")) { |
||
| 49 | // We can proceed |
||
| 50 | } else { |
||
| 51 | return $this->httpError(404); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | $File->sendDecryptedFile(); |
||
| 57 | } |
||
| 59 |