| Conditions | 8 |
| Paths | 7 |
| Total Lines | 58 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 37 | public function download() |
||
| 38 | { |
||
| 39 | $this->loadModel('Users'); |
||
| 40 | |||
| 41 | $user = $this->Users |
||
|
|
|||
| 42 | ->find() |
||
| 43 | ->where([ |
||
| 44 | 'Users.id' => $this->request->session()->read('Auth.User.id') |
||
| 45 | ]) |
||
| 46 | ->contain([ |
||
| 47 | 'Groups' => function ($q) { |
||
| 48 | return $q->select(['id', 'is_staff']); |
||
| 49 | } |
||
| 50 | ]) |
||
| 51 | ->first(); |
||
| 52 | |||
| 53 | if (is_null($user)) { |
||
| 54 | throw new ForbiddenException(); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (!isset($this->request->type)) { |
||
| 58 | throw new NotFoundException(); |
||
| 59 | } |
||
| 60 | |||
| 61 | switch ($this->request->type) { |
||
| 62 | case "blog": |
||
| 63 | if (!$user->premium && !$user->group->is_staff) { |
||
| 64 | throw new ForbiddenException(); |
||
| 65 | } |
||
| 66 | $this->loadModel('BlogAttachments'); |
||
| 67 | |||
| 68 | $attachment = $this->BlogAttachments->get($this->request->id); |
||
| 69 | |||
| 70 | if (!$attachment) { |
||
| 71 | throw new NotFoundException(); |
||
| 72 | } |
||
| 73 | |||
| 74 | $file = new File($attachment->url); |
||
| 75 | |||
| 76 | if (!$file->exists()) { |
||
| 77 | throw new NotFoundException(); |
||
| 78 | } |
||
| 79 | |||
| 80 | $this->response->file( |
||
| 81 | $file->path, |
||
| 82 | ['download' => true, 'name' => $attachment->name] |
||
| 83 | ); |
||
| 84 | |||
| 85 | $this->BlogAttachments->patchEntity($attachment, ['download' => $attachment->download + 1]); |
||
| 86 | $this->BlogAttachments->save($attachment); |
||
| 87 | break; |
||
| 88 | |||
| 89 | default: |
||
| 90 | throw new NotFoundException(); |
||
| 91 | } |
||
| 92 | |||
| 93 | return $this->response; |
||
| 94 | } |
||
| 95 | } |
||
| 96 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.