Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
8 | class AttachmentsController extends AppController |
||
9 | { |
||
10 | |||
11 | /** |
||
12 | * Display all attachments. |
||
13 | * |
||
14 | * @return \Cake\Network\Response |
||
15 | */ |
||
16 | public function index() |
||
44 | |||
45 | /** |
||
46 | * Add an attachment to an article. |
||
47 | * |
||
48 | * @return \Cake\Network\Response|void |
||
49 | */ |
||
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 | |||
102 | /** |
||
103 | * Edit an attachment. |
||
104 | * |
||
105 | * @return \Cake\Network\Response|void |
||
106 | */ |
||
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 | |||
174 | /** |
||
175 | * Delete an Attachment. |
||
176 | * |
||
177 | * @return \Cake\Network\Response |
||
178 | */ |
||
179 | public function delete() |
||
207 | } |
||
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.