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:
Complex classes like ManageController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ManageController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class ManageController extends AppController |
||
34 | { |
||
35 | |||
36 | /** |
||
37 | * An array containing the names of helpers controllers uses. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | public $helpers = ['Paginator']; |
||
42 | |||
43 | /** |
||
44 | * Shows a list of all the contents. |
||
45 | * |
||
46 | * @return void |
||
47 | */ |
||
48 | public function index() |
||
49 | { |
||
50 | $this->loadModel('Content.Contents'); |
||
51 | $this->Contents->CreatedBy->fieldable(false); |
||
52 | $this->Contents->ModifiedBy->fieldable(false); |
||
53 | $contents = $this->Contents |
||
54 | ->find('all', ['fieldable' => false]) |
||
55 | ->contain(['ContentTypes', 'CreatedBy', 'ModifiedBy']); |
||
56 | |||
57 | if (!empty($this->request->query['filter']) && |
||
58 | $contents instanceof Query |
||
59 | ) { |
||
60 | $this->Contents->search($this->request->query['filter'], $contents); |
||
61 | } |
||
62 | |||
63 | $this->title(__d('content', 'Contents List')); |
||
64 | $this->set('contents', $this->paginate($contents)); |
||
65 | $this->Breadcrumb->push('/admin/content/manage'); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Content-type selection screen. |
||
70 | * |
||
71 | * User must select which content type wish to create. |
||
72 | * |
||
73 | * @return void |
||
74 | */ |
||
75 | View Code Duplication | public function create() |
|
88 | |||
89 | /** |
||
90 | * Shows the "new content" form. |
||
91 | * |
||
92 | * @param string $typeSlug Content type slug. e.g.: "article", "product-info" |
||
93 | * @return void |
||
94 | * @throws \Content\Error\ContentTypeNotFoundException When content type was not |
||
95 | * found |
||
96 | * @throws \Content\Error\ContentCreateException When current user is not allowed |
||
97 | * to create contents of this type |
||
98 | */ |
||
99 | public function add($typeSlug) |
||
150 | |||
151 | /** |
||
152 | * Edit form for the given content. |
||
153 | * |
||
154 | * @param int $id Content's ID |
||
155 | * @param false|int $revisionId Fill form with content's revision information |
||
156 | * @return void |
||
157 | * @throws \Content\Error\ContentNotFoundException When content type, or when |
||
158 | * content content was not found |
||
159 | * @throws \Content\Error\ContentEditException When user is not allowed to edit |
||
160 | * contents of this type |
||
161 | */ |
||
162 | public function edit($id, $revisionId = false) |
||
163 | { |
||
164 | $this->loadModel('Content.Contents'); |
||
165 | $this->Contents->unbindComments(); |
||
166 | $content = false; |
||
167 | |||
168 | if (intval($revisionId) > 0 && !$this->request->data()) { |
||
169 | $this->loadModel('Content.ContentRevisions'); |
||
170 | $revision = $this->ContentRevisions->find() |
||
171 | ->where(['id' => $revisionId, 'content_id' => $id]) |
||
172 | ->first(); |
||
173 | |||
174 | if ($revision) { |
||
175 | $content = $revision->data; |
||
176 | |||
177 | if (!empty($content->_fields)) { |
||
178 | // Merge previous data for each field, we just load the data (metadata keeps to the latests configured). |
||
179 | $_fieldsRevision = $content->_fields; |
||
180 | $content = $this->Contents->attachFields($content); |
||
181 | $content->_fields = $content->_fields->map(function ($field, $key) use ($_fieldsRevision) { |
||
182 | $fieldRevision = $_fieldsRevision[$field->name]; |
||
183 | if ($fieldRevision) { |
||
184 | $field->set('value', $fieldRevision->value); |
||
185 | $field->set('extra', $fieldRevision->extra); |
||
186 | } |
||
187 | |||
188 | return $field; |
||
189 | }); |
||
190 | } |
||
191 | } |
||
192 | } else { |
||
193 | $content = $this->Contents |
||
194 | ->find() |
||
195 | ->where(['Contents.id' => $id]) |
||
196 | ->contain([ |
||
197 | 'Roles', |
||
198 | 'Translations', |
||
199 | 'ContentRevisions', |
||
200 | 'ContentTypes', |
||
201 | 'TranslationOf', |
||
202 | ]) |
||
203 | ->first(); |
||
204 | } |
||
205 | |||
206 | if (!$content || empty($content->content_type)) { |
||
207 | throw new ContentNotFoundException(__d('content', 'The requested page was not found.')); |
||
208 | } |
||
209 | |||
210 | if (!$content->content_type->userAllowed('edit')) { |
||
211 | throw new ContentEditException(__d('content', 'You are not allowed to create contents of this type ({0}).', $content->content_type->name)); |
||
212 | } |
||
213 | |||
214 | if (!empty($this->request->data)) { |
||
215 | View Code Duplication | if (empty($this->request->data['regenerate_slug'])) { |
|
216 | $this->Contents->behaviors()->Sluggable->config(['on' => 'create']); |
||
217 | } else { |
||
218 | unset($this->request->data['regenerate_slug']); |
||
219 | } |
||
220 | |||
221 | $content->accessible([ |
||
222 | 'id', |
||
223 | 'content_type_id', |
||
224 | 'content_type_slug', |
||
225 | 'translation_for', |
||
226 | 'created_by', |
||
227 | ], false); |
||
228 | |||
229 | $content = $this->Contents->patchEntity($content, $this->request->data()); |
||
230 | if ($this->Contents->save($content, ['atomic' => true, 'associated' => ['Roles']])) { |
||
231 | $this->Flash->success(__d('content', 'Content updated!')); |
||
232 | $this->redirect("/admin/content/manage/edit/{$id}"); |
||
233 | } else { |
||
234 | $this->Flash->danger(__d('content', 'Something went wrong, please check your information.')); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | $languages = LocaleToolbox::languagesList(); |
||
239 | $roles = $this->Contents->Roles->find('list'); |
||
240 | |||
241 | $this->title(__d('content', 'Editing Content: {0} <small>({1})</small>', $content->title, $content->content_type_slug)); |
||
242 | $this->set(compact('content', 'languages', 'roles')); |
||
243 | $this->Breadcrumb |
||
244 | ->push('/admin/content/manage/index') |
||
245 | ->push(__d('content', 'Editing content'), '#'); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Translate the given content to a different language. |
||
250 | * |
||
251 | * @param int $contentId Content's ID |
||
252 | * @return void |
||
253 | * @throws \Content\Error\ContentNotFoundException When content type, or when |
||
254 | * content content was not found |
||
255 | * @throws \Content\Error\ContentTranslateException When user is not allowed to |
||
256 | * translate contents of this type |
||
257 | */ |
||
258 | public function translate($contentId) |
||
322 | |||
323 | /** |
||
324 | * Deletes the given content by ID. |
||
325 | * |
||
326 | * @param int $contentId Content's ID |
||
327 | * @return void |
||
328 | */ |
||
329 | public function delete($contentId) |
||
351 | |||
352 | /** |
||
353 | * Removes the given revision of the given content. |
||
354 | * |
||
355 | * @param int $contentId Content's ID |
||
356 | * @param int $revisionId Revision's ID |
||
357 | * @return void Redirects to previous page |
||
358 | */ |
||
359 | View Code Duplication | public function deleteRevision($contentId, $revisionId) |
|
375 | } |
||
376 |