Conditions | 6 |
Paths | 5 |
Total Lines | 66 |
Code Lines | 39 |
Lines | 24 |
Ratio | 36.36 % |
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 |
||
107 | public function edit() |
||
108 | { |
||
109 | $this->loadModel('BlogAttachments'); |
||
110 | $this->loadModel('BlogArticles'); |
||
111 | |||
112 | $attachment = $this->BlogAttachments |
||
113 | ->find() |
||
114 | ->where([ |
||
115 | 'id' => $this->request->getAttribute('params')['id'] |
||
116 | ]) |
||
117 | ->first(); |
||
118 | |||
119 | //Check if the attachment is found. |
||
120 | if (empty($attachment)) { |
||
121 | $this->Flash->error(__d('admin', 'This attachment doesn\'t exist or has been deleted.')); |
||
122 | |||
123 | return $this->redirect(['action' => 'index']); |
||
124 | } |
||
125 | |||
126 | if ($this->request->is(['put', 'post'])) { |
||
127 | $this->BlogAttachments->patchEntity($attachment, $this->request->getParsedBody()); |
||
128 | |||
129 | //Check if the article has already an attachment |
||
130 | $article = $this->BlogArticles |
||
131 | ->find() |
||
132 | ->contain([ |
||
133 | 'BlogAttachments' |
||
134 | ]) |
||
135 | ->where([ |
||
136 | 'BlogArticles.id' => $this->request->getData('article_id') |
||
137 | ]) |
||
138 | ->first(); |
||
139 | |||
140 | View Code Duplication | if (!is_null($article->blog_attachment) && $article->blog_attachment->id != $this->request->id) { |
|
141 | $this->Flash->error( |
||
142 | __d( |
||
143 | 'admin', |
||
144 | 'This article has already an attachment, you can edit it <a href="{0}" class="btn btn-sm btn-danger">here</a>.', |
||
145 | Router::url(['_name' => 'attachments-edit', 'id' => $article->blog_attachment->id]) |
||
146 | ) |
||
147 | ); |
||
148 | |||
149 | return $this->redirect(['action' => 'index']); |
||
150 | } |
||
151 | |||
152 | $attachment->user_id = $this->Auth->user('id'); |
||
153 | $attachment->accessible('url_file', true); |
||
154 | |||
155 | View Code Duplication | if ($editedAttachment = $this->BlogAttachments->save($attachment)) { |
|
156 | $file = new File(WWW_ROOT . $editedAttachment->url); |
||
157 | |||
158 | $editedAttachment->name = $file->name; |
||
159 | $editedAttachment->extension = '.' . $file->info()['extension']; |
||
160 | $editedAttachment->size = $file->info()['filesize']; |
||
161 | |||
162 | $this->BlogAttachments->save($editedAttachment); |
||
163 | |||
164 | $this->Flash->success(__d('admin', 'Your attachment has been edited successfully !')); |
||
165 | |||
166 | return $this->redirect(['action' => 'index']); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | $articles = $this->BlogAttachments->BlogArticles->find('list'); |
||
171 | $this->set(compact('attachment', 'articles')); |
||
172 | } |
||
173 | |||
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@property
annotation 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.