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 |
||
| 23 | class Posts { |
||
| 24 | use |
||
| 25 | CRUD_helpers, |
||
| 26 | Singleton; |
||
| 27 | protected $data_model = [ |
||
| 28 | 'id' => 'int:0', |
||
| 29 | 'user' => 'int:0', |
||
| 30 | 'date' => 'int:0', |
||
| 31 | 'title' => 'ml:text', |
||
| 32 | 'path' => 'ml:text', |
||
| 33 | 'content' => 'ml:html', |
||
| 34 | 'draft' => 'int:0..1', |
||
| 35 | 'sections' => [ |
||
| 36 | 'data_model' => [ |
||
| 37 | 'id' => 'int:0', |
||
| 38 | 'section' => 'int:0' |
||
| 39 | ] |
||
| 40 | ], |
||
| 41 | 'tags' => [ |
||
| 42 | 'data_model' => [ |
||
| 43 | 'id' => 'int:0', |
||
| 44 | 'tag' => 'int:0' |
||
| 45 | ], |
||
| 46 | 'language_field' => 'lang' |
||
| 47 | ] |
||
| 48 | ]; |
||
| 49 | protected $table = '[prefix]blogs_posts'; |
||
| 50 | protected $data_model_ml_group = 'Blogs/posts'; |
||
| 51 | protected $data_model_files_tag_prefix = 'Blogs/posts'; |
||
| 52 | /** |
||
| 53 | * @var Cache\Prefix |
||
| 54 | */ |
||
| 55 | protected $cache; |
||
| 56 | |||
| 57 | protected function construct () { |
||
| 63 | /** |
||
| 64 | * Returns database index |
||
| 65 | * |
||
| 66 | * @return int |
||
| 67 | */ |
||
| 68 | protected function cdb () { |
||
| 71 | /** |
||
| 72 | * Get data of specified post |
||
| 73 | * |
||
| 74 | * @param int|int[] $id |
||
| 75 | * |
||
| 76 | * @return array|false |
||
| 77 | */ |
||
| 78 | public function get ($id) { |
||
| 97 | /** |
||
| 98 | * @param int $page |
||
| 99 | * @param int $count |
||
| 100 | * |
||
| 101 | * @return int[] |
||
| 102 | */ |
||
| 103 | public function get_all ($page, $count) { |
||
| 106 | /** |
||
| 107 | * Transform tags ids back into array of strings |
||
| 108 | * |
||
| 109 | * @param int[] $tags |
||
| 110 | * |
||
| 111 | * @return string[] |
||
| 112 | */ |
||
| 113 | protected function read_tags_processing ($tags) { |
||
| 116 | /** |
||
| 117 | * Get data of specified post |
||
| 118 | * |
||
| 119 | * @param int|int[] $id |
||
| 120 | * |
||
| 121 | * @return array|false |
||
| 122 | */ |
||
| 123 | public function get_as_json_ld ($id) { |
||
| 130 | /** |
||
| 131 | * @param array|array[] $post |
||
| 132 | * |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | public function post_to_jsonld ($post) { |
||
| 205 | /** |
||
| 206 | * Get latest posts |
||
| 207 | * |
||
| 208 | * @param int $page |
||
| 209 | * @param int $count |
||
| 210 | * |
||
| 211 | * @return int[] |
||
| 212 | */ |
||
| 213 | public function get_latest_posts ($page, $count) { |
||
| 219 | /** |
||
| 220 | * Get posts for section |
||
| 221 | * |
||
| 222 | * @param int $section |
||
| 223 | * @param int $page |
||
| 224 | * @param int $count |
||
| 225 | * |
||
| 226 | * @return int[] |
||
| 227 | */ |
||
| 228 | public function get_for_section ($section, $page, $count) { |
||
| 237 | /** |
||
| 238 | * Get number of posts for section |
||
| 239 | * |
||
| 240 | * @param int $section |
||
| 241 | * |
||
| 242 | * @return int |
||
| 243 | */ |
||
| 244 | public function get_for_section_count ($section) { |
||
| 254 | /** |
||
| 255 | * Get posts for tag |
||
| 256 | * |
||
| 257 | * @param int $tag |
||
| 258 | * @param int $page |
||
| 259 | * @param int $count |
||
| 260 | * |
||
| 261 | * @return int[] |
||
| 262 | */ |
||
| 263 | public function get_for_tag ($tag, $page, $count) { |
||
| 272 | /** |
||
| 273 | * Get number of posts for tag |
||
| 274 | * |
||
| 275 | * @param int $tag |
||
| 276 | * |
||
| 277 | * @return int |
||
| 278 | */ |
||
| 279 | public function get_for_tag_count ($tag) { |
||
| 289 | /** |
||
| 290 | * Get drafts |
||
| 291 | * |
||
| 292 | * @param int $user |
||
| 293 | * @param int $page |
||
| 294 | * @param int $count |
||
| 295 | * |
||
| 296 | * @return int[] |
||
| 297 | */ |
||
| 298 | public function get_drafts ($user, $page, $count) { |
||
| 305 | /** |
||
| 306 | * Get number of drafts |
||
| 307 | * |
||
| 308 | * @param int $user |
||
| 309 | * |
||
| 310 | * @return int |
||
| 311 | */ |
||
| 312 | public function get_drafts_count ($user) { |
||
| 320 | /** |
||
| 321 | * Add new post |
||
| 322 | * |
||
| 323 | * @param string $title |
||
| 324 | * @param string $path |
||
| 325 | * @param string $content |
||
| 326 | * @param int[] $sections |
||
| 327 | * @param string[] $tags |
||
| 328 | * @param bool $draft |
||
| 329 | * |
||
| 330 | * @return false|int Id of created post on success of <b>false</> on failure |
||
| 331 | */ |
||
| 332 | public function add ($title, $path, $content, $sections, $tags, $draft) { |
||
| 351 | /** |
||
| 352 | * Transform array of string tags into array of their ids |
||
| 353 | * |
||
| 354 | * @param string[] $tags |
||
| 355 | * |
||
| 356 | * @return int[] |
||
| 357 | */ |
||
| 358 | protected function prepare_tags ($tags) { |
||
| 361 | /** |
||
| 362 | * @param string $content |
||
| 363 | * @param int[] $sections |
||
| 364 | * @param string[] $tags |
||
| 365 | * |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | protected function check_arguments ($content, &$sections, $tags) { |
||
| 379 | /** |
||
| 380 | * @param int $id |
||
| 381 | */ |
||
| 382 | protected function cache_cleanups ($id) { |
||
| 390 | /** |
||
| 391 | * Set data of specified post |
||
| 392 | * |
||
| 393 | * @param int $id |
||
| 394 | * @param string $title |
||
| 395 | * @param string $path |
||
| 396 | * @param string $content |
||
| 397 | * @param int[] $sections |
||
| 398 | * @param string[] $tags |
||
| 399 | * @param bool $draft |
||
| 400 | * |
||
| 401 | * @return bool |
||
| 402 | */ |
||
| 403 | public function set ($id, $title, $path, $content, $sections, $tags, $draft) { |
||
| 422 | /** |
||
| 423 | * Delete specified post |
||
| 424 | * |
||
| 425 | * @param int $id |
||
| 426 | * |
||
| 427 | * @return bool |
||
| 428 | */ |
||
| 429 | public function del ($id) { |
||
| 444 | /** |
||
| 445 | * Get total count of posts |
||
| 446 | * |
||
| 447 | * @return int |
||
| 448 | */ |
||
| 449 | public function get_total_count () { |
||
| 461 | } |
||
| 462 |