Complex classes like Posts 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 Posts, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Posts { |
||
27 | use |
||
28 | CRUD, |
||
29 | Singleton; |
||
30 | protected $data_model = [ |
||
31 | 'id' => 'int:0', |
||
32 | 'user' => 'int:0', |
||
33 | 'date' => 'int:0', |
||
34 | 'title' => 'ml:text', |
||
35 | 'path' => 'ml:text', |
||
36 | 'content' => 'ml:html', |
||
37 | 'draft' => 'int:0..1' |
||
38 | ]; |
||
39 | protected $table = '[prefix]blogs_posts'; |
||
40 | protected $data_model_ml_group = 'Blogs/posts'; |
||
41 | protected $data_model_files_tag_prefix = 'Blogs/posts'; |
||
42 | protected $table_sections = '[prefix]blogs_posts_sections'; |
||
43 | protected $table_tags = '[prefix]blogs_posts_tags'; |
||
44 | /** |
||
45 | * @var Cache_prefix |
||
46 | */ |
||
47 | protected $cache; |
||
48 | |||
49 | protected function construct () { |
||
55 | /** |
||
56 | * Returns database index |
||
57 | * |
||
58 | * @return int |
||
59 | */ |
||
60 | protected function cdb () { |
||
63 | /** |
||
64 | * Get data of specified post |
||
65 | * |
||
66 | * @param int|int[] $id |
||
67 | * |
||
68 | * @return array|false |
||
69 | */ |
||
70 | function get ($id) { |
||
136 | /** |
||
137 | * Get data of specified post |
||
138 | * |
||
139 | * @param int|int[] $id |
||
140 | * |
||
141 | * @return array|false |
||
142 | */ |
||
143 | function get_as_json_ld ($id) { |
||
150 | /** |
||
151 | * @param array|array[] $post |
||
152 | * |
||
153 | * @return array |
||
1 ignored issue
–
show
|
|||
154 | */ |
||
155 | function post_to_jsonld ($post) { |
||
226 | /** |
||
227 | * Get latest posts |
||
228 | * |
||
229 | * @param int $page |
||
230 | * @param int $number |
||
231 | * |
||
232 | * @return int[] |
||
233 | */ |
||
234 | function get_latest_posts ($page, $number) { |
||
245 | /** |
||
246 | * Get posts for section |
||
247 | * |
||
248 | * @param int $section |
||
249 | * @param int $page |
||
250 | * @param int $number |
||
251 | * |
||
252 | * @return int[] |
||
253 | */ |
||
254 | function get_for_section ($section, $page, $number) { |
||
270 | /** |
||
271 | * Get posts for tag |
||
272 | * |
||
273 | * @param int $tag |
||
274 | * @param string $lang |
||
275 | * @param int $page |
||
276 | * @param int $number |
||
277 | * |
||
278 | * @return int[] |
||
279 | */ |
||
280 | function get_for_tag ($tag, $lang, $page, $number) { |
||
300 | /** |
||
301 | * Get number of posts for tag |
||
302 | * |
||
303 | * @param int $tag |
||
304 | * @param string $lang |
||
305 | * |
||
306 | * @return int |
||
307 | */ |
||
308 | function get_for_tag_count ($tag, $lang) { |
||
324 | /** |
||
325 | * Get drafts |
||
326 | * |
||
327 | * @param int $user |
||
328 | * @param int $page |
||
329 | * @param int $number |
||
330 | * |
||
331 | * @return int[] |
||
332 | */ |
||
333 | function get_drafts ($user, $page, $number) { |
||
349 | /** |
||
350 | * Get number of drafts |
||
351 | * |
||
352 | * @param int $user |
||
353 | * |
||
354 | * @return int |
||
355 | */ |
||
356 | function get_drafts_count ($user) { |
||
368 | /** |
||
369 | * Add new post |
||
370 | * |
||
371 | * @param string $title |
||
372 | * @param string $path |
||
373 | * @param string $content |
||
374 | * @param int[] $sections |
||
375 | * @param string[] $tags |
||
376 | * @param bool $draft |
||
377 | * |
||
378 | * @return false|int Id of created post on success of <b>false</> on failure |
||
1 ignored issue
–
show
|
|||
379 | */ |
||
380 | function add ($title, $path, $content, $sections, $tags, $draft) { |
||
399 | /** |
||
400 | * @param string $content |
||
401 | * @param int[] $sections |
||
402 | * @param string[] $tags |
||
403 | * |
||
404 | * @return bool |
||
405 | */ |
||
406 | protected function check_arguments ($content, &$sections, $tags) { |
||
417 | /** |
||
418 | * @param int $id |
||
419 | * @param int[] $sections |
||
420 | * @param string[] $tags |
||
421 | */ |
||
422 | protected function final_updates_and_cache_cleanups ($id, $sections, $tags) { |
||
432 | /** |
||
433 | * Remove existing sections and set as specified |
||
434 | * |
||
435 | * @param int $id |
||
436 | * @param int[] $sections Empty array to just remove all existing sections |
||
437 | */ |
||
438 | protected function update_sections ($id, $sections = []) { |
||
461 | /** |
||
462 | * Remove existing tags and set as specified |
||
463 | * |
||
464 | * @param int $id |
||
465 | * @param string[] $tags Empty array to just remove all existing tags |
||
466 | */ |
||
467 | protected function update_tags ($id, $tags = []) { |
||
502 | /** |
||
503 | * Set data of specified post |
||
504 | * |
||
505 | * @param int $id |
||
506 | * @param string $title |
||
507 | * @param string $path |
||
508 | * @param string $content |
||
509 | * @param int[] $sections |
||
510 | * @param string[] $tags |
||
511 | * @param bool $draft |
||
512 | * |
||
513 | * @return bool |
||
514 | */ |
||
515 | function set ($id, $title, $path, $content, $sections, $tags, $draft) { |
||
536 | /** |
||
537 | * Delete specified post |
||
538 | * |
||
539 | * @param int $id |
||
540 | * |
||
541 | * @return bool |
||
542 | */ |
||
543 | function del ($id) { |
||
564 | /** |
||
565 | * Get total count of posts |
||
566 | * |
||
567 | * @return int |
||
568 | */ |
||
569 | function get_total_count () { |
||
581 | } |
||
582 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.