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 |
||
24 | class Posts { |
||
25 | use |
||
26 | CRUD_helpers, |
||
27 | Singleton; |
||
28 | protected $data_model = [ |
||
29 | 'id' => 'int:0', |
||
30 | 'user' => 'int:0', |
||
31 | 'date' => 'int:0', |
||
32 | 'title' => 'ml:text', |
||
33 | 'path' => 'ml:text', |
||
34 | 'content' => 'ml:html', |
||
35 | 'draft' => 'int:0..1', |
||
36 | 'sections' => [ |
||
37 | 'data_model' => [ |
||
38 | 'id' => 'int:0', |
||
39 | 'section' => 'int:0' |
||
40 | ] |
||
41 | ], |
||
42 | 'tags' => [ |
||
43 | 'data_model' => [ |
||
44 | 'id' => 'int:0', |
||
45 | 'tag' => 'int:0' |
||
46 | ], |
||
47 | 'language_field' => 'lang' |
||
48 | ] |
||
49 | ]; |
||
50 | protected $table = '[prefix]blogs_posts'; |
||
51 | protected $data_model_ml_group = 'Blogs/posts'; |
||
52 | protected $data_model_files_tag_prefix = 'Blogs/posts'; |
||
53 | /** |
||
54 | * @var Cache_prefix |
||
55 | */ |
||
56 | protected $cache; |
||
57 | |||
58 | protected function construct () { |
||
64 | /** |
||
65 | * Returns database index |
||
66 | * |
||
67 | * @return int |
||
68 | */ |
||
69 | protected function cdb () { |
||
72 | /** |
||
73 | * Get data of specified post |
||
74 | * |
||
75 | * @param int|int[] $id |
||
76 | * |
||
77 | * @return array|false |
||
78 | */ |
||
79 | function get ($id) { |
||
115 | /** |
||
116 | * @param int $page |
||
117 | * @param int $count |
||
118 | * |
||
119 | * @return int[] |
||
1 ignored issue
–
show
|
|||
120 | */ |
||
121 | function get_all ($page, $count) { |
||
124 | /** |
||
125 | * Transform tags ids back into array of strings |
||
126 | * |
||
127 | * @param int[] $tags |
||
128 | * |
||
129 | * @return string[] |
||
130 | */ |
||
131 | protected function read_tags_processing ($tags) { |
||
134 | /** |
||
135 | * Get data of specified post |
||
136 | * |
||
137 | * @param int|int[] $id |
||
138 | * |
||
139 | * @return array|false |
||
140 | */ |
||
141 | function get_as_json_ld ($id) { |
||
148 | /** |
||
149 | * @param array|array[] $post |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | function post_to_jsonld ($post) { |
||
224 | /** |
||
225 | * Get latest posts |
||
226 | * |
||
227 | * @param int $page |
||
228 | * @param int $count |
||
229 | * |
||
230 | * @return int[] |
||
1 ignored issue
–
show
|
|||
231 | */ |
||
232 | function get_latest_posts ($page, $count) { |
||
238 | /** |
||
239 | * Get posts for section |
||
240 | * |
||
241 | * @param int $section |
||
242 | * @param int $page |
||
243 | * @param int $count |
||
244 | * |
||
245 | * @return int[] |
||
1 ignored issue
–
show
|
|||
246 | */ |
||
247 | function get_for_section ($section, $page, $count) { |
||
256 | /** |
||
257 | * Get number of posts for section |
||
258 | * |
||
259 | * @param int $section |
||
260 | * |
||
261 | * @return int |
||
262 | */ |
||
263 | function get_for_section_count ($section) { |
||
273 | /** |
||
274 | * Get posts for tag |
||
275 | * |
||
276 | * @param int $tag |
||
277 | * @param string $lang |
||
278 | * @param int $page |
||
279 | * @param int $count |
||
280 | * |
||
281 | * @return int[] |
||
1 ignored issue
–
show
|
|||
282 | */ |
||
283 | function get_for_tag ($tag, $lang, $page, $count) { |
||
293 | /** |
||
294 | * Get number of posts for tag |
||
295 | * |
||
296 | * @param int $tag |
||
297 | * @param string $lang |
||
298 | * |
||
299 | * @return int |
||
300 | */ |
||
301 | function get_for_tag_count ($tag, $lang) { |
||
312 | /** |
||
313 | * Get drafts |
||
314 | * |
||
315 | * @param int $user |
||
316 | * @param int $page |
||
317 | * @param int $count |
||
318 | * |
||
319 | * @return int[] |
||
1 ignored issue
–
show
|
|||
320 | */ |
||
321 | function get_drafts ($user, $page, $count) { |
||
328 | /** |
||
329 | * Get number of drafts |
||
330 | * |
||
331 | * @param int $user |
||
332 | * |
||
333 | * @return int |
||
334 | */ |
||
335 | function get_drafts_count ($user) { |
||
343 | /** |
||
344 | * Add new post |
||
345 | * |
||
346 | * @param string $title |
||
347 | * @param string $path |
||
348 | * @param string $content |
||
349 | * @param int[] $sections |
||
350 | * @param string[] $tags |
||
351 | * @param bool $draft |
||
352 | * |
||
353 | * @return false|int Id of created post on success of <b>false</> on failure |
||
1 ignored issue
–
show
|
|||
354 | */ |
||
355 | function add ($title, $path, $content, $sections, $tags, $draft) { |
||
374 | /** |
||
375 | * Transform array of string tags into array of their ids |
||
376 | * |
||
377 | * @param string[] $tags |
||
378 | * |
||
379 | * @return int[] |
||
1 ignored issue
–
show
|
|||
380 | */ |
||
381 | protected function prepare_tags ($tags) { |
||
384 | /** |
||
385 | * @param string $content |
||
386 | * @param int[] $sections |
||
387 | * @param string[] $tags |
||
388 | * |
||
389 | * @return bool |
||
390 | */ |
||
391 | protected function check_arguments ($content, &$sections, $tags) { |
||
401 | /** |
||
402 | * @param int $id |
||
403 | */ |
||
404 | protected function cache_cleanups ($id) { |
||
412 | /** |
||
413 | * Set data of specified post |
||
414 | * |
||
415 | * @param int $id |
||
416 | * @param string $title |
||
417 | * @param string $path |
||
418 | * @param string $content |
||
419 | * @param int[] $sections |
||
420 | * @param string[] $tags |
||
421 | * @param bool $draft |
||
422 | * |
||
423 | * @return bool |
||
424 | */ |
||
425 | function set ($id, $title, $path, $content, $sections, $tags, $draft) { |
||
444 | /** |
||
445 | * Delete specified post |
||
446 | * |
||
447 | * @param int $id |
||
448 | * |
||
449 | * @return bool |
||
450 | */ |
||
451 | function del ($id) { |
||
472 | /** |
||
473 | * Get total count of posts |
||
474 | * |
||
475 | * @return int |
||
476 | */ |
||
477 | function get_total_count () { |
||
489 | } |
||
490 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]
orarray<String>
.