| Conditions | 4 |
| Paths | 4 |
| Total Lines | 51 |
| Code Lines | 31 |
| Lines | 24 |
| Ratio | 47.06 % |
| 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 |
||
| 50 | public function add() |
||
| 51 | { |
||
| 52 | $this->loadModel('BlogAttachments'); |
||
| 53 | $this->loadModel('BlogArticles'); |
||
| 54 | $attachment = $this->BlogAttachments->newEntity($this->request->getParsedBody()); |
||
| 55 | |||
| 56 | if ($this->request->is('post')) { |
||
| 57 | $article = $this->BlogArticles |
||
| 58 | ->find() |
||
| 59 | ->contain([ |
||
| 60 | 'BlogAttachments' |
||
| 61 | ]) |
||
| 62 | ->where([ |
||
| 63 | 'BlogArticles.id' => $this->request->getData('article_id') |
||
| 64 | ]) |
||
| 65 | ->first(); |
||
| 66 | |||
| 67 | //Check if the article has already an attachment |
||
| 68 | View Code Duplication | if (!is_null($article->blog_attachment)) { |
|
| 69 | $this->Flash->error( |
||
| 70 | __d( |
||
| 71 | 'admin', |
||
| 72 | 'This article has already an attachment, you can edit it <a href="{0}" class="btn btn-sm btn-danger">here</a>.', |
||
| 73 | Router::url(['_name' => 'attachments-edit', 'id' => $article->blog_attachment->id]) |
||
| 74 | ) |
||
| 75 | ); |
||
| 76 | |||
| 77 | return $this->redirect(['action' => 'index']); |
||
| 78 | } |
||
| 79 | |||
| 80 | $attachment->user_id = $this->Auth->user('id'); |
||
| 81 | $attachment->accessible('url_file', true); |
||
| 82 | |||
| 83 | View Code Duplication | if ($newAttachment = $this->BlogAttachments->save($attachment)) { |
|
| 84 | $file = new File(WWW_ROOT . $newAttachment->url); |
||
| 85 | |||
| 86 | $newAttachment->name = $file->name; |
||
| 87 | $newAttachment->extension = '.' . $file->info()['extension']; |
||
| 88 | $newAttachment->size = $file->info()['filesize']; |
||
| 89 | |||
| 90 | $this->BlogAttachments->save($newAttachment); |
||
| 91 | |||
| 92 | $this->Flash->success(__d('admin', 'Your attachment has been created successfully !')); |
||
| 93 | |||
| 94 | return $this->redirect(['action' => 'index']); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | $articles = $this->BlogAttachments->BlogArticles->find('list'); |
||
| 99 | $this->set(compact('attachment', 'articles')); |
||
| 100 | } |
||
| 101 | |||
| 208 |
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.