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 Pages 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 Pages, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Pages extends Admin |
||
16 | { |
||
17 | /** |
||
18 | * For admin listing and search results |
||
19 | */ |
||
20 | use Traits\SearchController; |
||
21 | |||
22 | protected $template_path = 'cms/admin/pages/'; |
||
23 | |||
24 | /** |
||
25 | * Add default scripts for displaying templates |
||
26 | * |
||
27 | * @return void |
||
28 | * @see app/config/default.ini |
||
29 | */ |
||
30 | protected function addScripts() |
||
35 | |||
36 | |||
37 | /** |
||
38 | * |
||
39 | * |
||
40 | * @param \Base $f3 |
||
41 | * @return void |
||
42 | */ |
||
43 | View Code Duplication | public function listing(\Base $f3) |
|
59 | |||
60 | |||
61 | /** |
||
62 | * |
||
63 | * |
||
64 | * @param \Base $f3 |
||
65 | * @return void |
||
66 | */ |
||
67 | View Code Duplication | public function search(\Base $f3) |
|
84 | |||
85 | |||
86 | /** |
||
87 | * |
||
88 | * |
||
89 | * @param \Base $f3 |
||
90 | * @return void |
||
91 | */ |
||
92 | public function edit(\Base $f3) |
||
138 | |||
139 | |||
140 | /** |
||
141 | * Set default values in page data where not set already |
||
142 | * @param \Base $f3 |
||
143 | * @param \FFCMS\Mappers\Pages $mapper |
||
144 | * @return $data array |
||
|
|||
145 | */ |
||
146 | protected function filterPageInput(\Base $f3, \FFCMS\Mappers\Pages &$mapper): array |
||
147 | { |
||
148 | // only allow updating of these fields |
||
149 | $data = $f3->get('REQUEST'); |
||
150 | |||
151 | $fields = [ |
||
152 | 'key', |
||
153 | 'author', |
||
154 | 'language', |
||
155 | 'status', |
||
156 | 'slug', |
||
157 | 'path', |
||
158 | 'keywords', |
||
159 | 'description', |
||
160 | 'title', |
||
161 | 'summary', |
||
162 | 'body', |
||
163 | 'scopes', |
||
164 | 'category', |
||
165 | 'tags', |
||
166 | 'metadata', |
||
167 | 'expires', |
||
168 | 'published', |
||
169 | 'robots', |
||
170 | ]; |
||
171 | |||
172 | // check input data has values set for the above fields |
||
173 | foreach ($fields as $k => $field) { |
||
174 | if (!array_key_exists($field, $data) || empty($data[$field])) { |
||
175 | $data[$field] = null; |
||
176 | } |
||
177 | } |
||
178 | // then remove any input data fields that aren't in the above fields |
||
179 | foreach ($data as $field => $v) { |
||
180 | if (!in_array($field, $fields)) { |
||
181 | unset($data[$field]); |
||
182 | } |
||
183 | } |
||
184 | |||
185 | // http://php.net/manual/en/function.strtotime.php |
||
186 | $data['expires'] = empty($data['expires']) ? null : Helpers\Time::database(strtotime($data['expires'])); |
||
187 | $data['published'] = empty($data['published']) ? null : Helpers\Time::database(strtotime($data['published'])); |
||
188 | |||
189 | // update required fields to check from ones which changed |
||
190 | // validate the entered data |
||
191 | $data['users_uuid'] = $f3->get('uuid'); |
||
192 | |||
193 | // set default key |
||
194 | if (empty($data['key'])) { |
||
195 | $data['key'] = $data['title']; |
||
196 | } |
||
197 | |||
198 | // set default slug |
||
199 | if (empty($data['slug'])) { |
||
200 | $data['slug'] = $data['title']; |
||
201 | } |
||
202 | |||
203 | // url path |
||
204 | if (empty($data['path'])) { |
||
205 | $data['path'] = '/'; |
||
206 | } |
||
207 | |||
208 | // publish status |
||
209 | if (empty($data['status'])) { |
||
210 | $data['status'] = 'draft'; |
||
211 | } |
||
212 | |||
213 | // language |
||
214 | if (empty($data['language'])) { |
||
215 | $data['language'] = 'en'; |
||
216 | } |
||
217 | |||
218 | // author |
||
219 | if (empty($data['author'])) { |
||
220 | $usersMapper = $f3->get('usersMapper'); |
||
221 | $data['author'] = $usersMapper->fullName(); |
||
222 | } |
||
223 | |||
224 | // category |
||
225 | if (empty($data['category'])) { |
||
226 | $data['category'] = 'page'; |
||
227 | } |
||
228 | |||
229 | // scope |
||
230 | if (empty($data['scopes'])) { |
||
231 | $data['scopes'] = 'public'; |
||
232 | } |
||
233 | |||
234 | if (empty($data['scopes'])) { |
||
235 | $data['scopes'] = 'public'; |
||
236 | } |
||
237 | |||
238 | $data['robots'] = (int) !empty($data['robots']); |
||
239 | |||
240 | $mapper->copyfrom($data); |
||
241 | $mapper->copyfrom($mapper->filter()); // filter mapa data |
||
242 | $mapper->validationRequired([ |
||
243 | 'title', |
||
244 | 'summary', |
||
245 | 'body', |
||
246 | ]); |
||
247 | |||
248 | return $mapper->cast(); |
||
249 | } |
||
250 | |||
251 | |||
252 | /** |
||
253 | * |
||
254 | * |
||
255 | * @param \Base $f3 |
||
256 | * @return void |
||
257 | */ |
||
258 | public function editPost(\Base $f3) |
||
326 | |||
327 | |||
328 | /** |
||
329 | * |
||
330 | * |
||
331 | * @param \Base $f3 |
||
332 | * @return void |
||
333 | */ |
||
334 | public function add(\Base $f3) |
||
366 | |||
367 | |||
368 | /** |
||
369 | * |
||
370 | * |
||
371 | * @param \Base $f3 |
||
372 | * @return void |
||
373 | */ |
||
374 | public function addPost(\Base $f3) |
||
435 | |||
436 | |||
437 | /** |
||
438 | * |
||
439 | * |
||
440 | * @param \Base $f3 |
||
441 | * @return void |
||
442 | */ |
||
443 | public function view(\Base $f3) |
||
483 | |||
484 | |||
485 | /** |
||
486 | * |
||
487 | * |
||
488 | * @param \Base $f3 |
||
489 | * @return void |
||
490 | */ |
||
491 | View Code Duplication | public function delete(\Base $f3) |
|
516 | |||
517 | |||
518 | } |
||
519 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.