Complex classes like ClassyPost 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 ClassyPost, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class ClassyPost extends ClassyBasis { |
||
|
|
|||
| 7 | |||
| 8 | /** |
||
| 9 | * Current post id. |
||
| 10 | * |
||
| 11 | * @var int |
||
| 12 | */ |
||
| 13 | public $ID; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Stores current post object. |
||
| 17 | * |
||
| 18 | * @var WP_Post |
||
| 19 | */ |
||
| 20 | protected $object; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Post title. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public $post_title; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Post name. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | public $post_name; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Post content (raw). |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | public $post_content; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Post type. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | public $post_type; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Post author id. |
||
| 52 | * |
||
| 53 | * @var int |
||
| 54 | */ |
||
| 55 | public $post_author; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Post date. String as stored in the WP database, ex: 2012-04-23 08:11:23. |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | public $post_date; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Post excerpt (raw). |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | public $post_excerpt; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Post status. It can be draft, publish, pending, private, trash, etc. |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | public $post_status; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Post permalink. |
||
| 80 | * |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | public $permalink; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Main constructor function. If ID won't be provided we will try to find it, based on your query. |
||
| 87 | * |
||
| 88 | * @param object|int $post WP_Post or WP_Post.ID. |
||
| 89 | */ |
||
| 90 | public function __construct( $post = null ) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Initialises Instance based on provided post id. |
||
| 101 | */ |
||
| 102 | protected function init() { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns post object. |
||
| 112 | * |
||
| 113 | * @return WP_Post |
||
| 114 | */ |
||
| 115 | public function get_object() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Checks if current user can edit this post. |
||
| 123 | * |
||
| 124 | * @return boolean |
||
| 125 | */ |
||
| 126 | public function can_edit() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns post edit url. |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public function get_edit_url() { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Returns array of attached image ids. |
||
| 152 | * |
||
| 153 | * @return false|array of ids |
||
| 154 | */ |
||
| 155 | public function get_attached_images() { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Returns array of attached images as ClassyImage objects. |
||
| 178 | * |
||
| 179 | * @return array of ClassyImage |
||
| 180 | */ |
||
| 181 | public function attached_images() { |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns first attached image id. |
||
| 201 | * |
||
| 202 | * @return int|boolean |
||
| 203 | */ |
||
| 204 | public function get_first_attached_image_id() { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Returns first attached image. |
||
| 229 | * |
||
| 230 | * @return ClassyImage |
||
| 231 | */ |
||
| 232 | public function first_attached_image() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Returns post thumbnail. |
||
| 245 | * |
||
| 246 | * @return ClassyImage |
||
| 247 | */ |
||
| 248 | public function thumbnail() { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Returns post title with filters applied. |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function get_title() { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Alias for get_title. |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function title() { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Returns the post content with filters applied. |
||
| 282 | * |
||
| 283 | * @param integer $page Page number, in case our post has <!--nextpage--> tags. |
||
| 284 | * |
||
| 285 | * @return string Post content |
||
| 286 | */ |
||
| 287 | public function get_content( $page = 0 ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Alias for get_content. |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function content() { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns post type object for current post. |
||
| 320 | * |
||
| 321 | * @return object |
||
| 322 | */ |
||
| 323 | public function get_post_type() { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Returns post permalink. |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public function get_permalink() { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Alias for get_permalink |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function permalink() { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Returns post preview of requested length. |
||
| 353 | * It will look for post_excerpt and will return it. |
||
| 354 | * If post contains <!-- more --> tag it will return content until it |
||
| 355 | * |
||
| 356 | * @param integer $len Number of words. |
||
| 357 | * @param boolean $force If is set to true it will cut your post_excerpt to desired $len length. |
||
| 358 | * @param string $readmore The text for 'readmore' link. |
||
| 359 | * @param boolean $strip Should we strip tags. |
||
| 360 | * |
||
| 361 | * @return string Post preview. |
||
| 362 | */ |
||
| 363 | public function get_preview( $len = 50, $force = false, $readmore = 'Read More', $strip = true ) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Returns comments array |
||
| 445 | * |
||
| 446 | * @param string $status Comment status. |
||
| 447 | * @param string $order Order for comments query. |
||
| 448 | * |
||
| 449 | * @return array |
||
| 450 | */ |
||
| 451 | public function get_comments( $status = 'approve', $order = 'DESC' ) { |
||
| 482 | } |
||
| 483 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.