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 |
||
| 3 | class ClassyPost extends ClassyBasis { |
||
|
|
|||
| 4 | |||
| 5 | /** |
||
| 6 | * Current post id |
||
| 7 | * @var int |
||
| 8 | */ |
||
| 9 | public $ID; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Stores current post object |
||
| 13 | * @var object |
||
| 14 | */ |
||
| 15 | protected $object; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Post title |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | public $post_title; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Post name |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public $post_name; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Post content (raw) |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | public $post_content; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Post type |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | public $post_type; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Post author id |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | public $post_author; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Post date. String as stored in the WP database, ex: 2012-04-23 08:11:23 |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | public $post_date; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Post excerpt (raw) |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | public $post_excerpt; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Post status. It can be draft, publish, pending, private, trash, etc. |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | public $post_status; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Post permalink |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | public $permalink; |
||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * Main constructor function. If ID won't be provided we will try to find it, based on your query |
||
| 74 | * @param object/int $post |
||
| 75 | */ |
||
| 76 | public function __construct( $post = null ) { |
||
| 77 | if ( is_integer( $post ) ) { |
||
| 78 | $this->ID = $post; |
||
| 79 | $this->init(); |
||
| 80 | } elseif ( is_a( $post, 'WP_Post' ) ) { |
||
| 81 | $this->import( $post ); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Initialises Instance based on provided post id |
||
| 87 | */ |
||
| 88 | protected function init() { |
||
| 89 | $post = $this->get_object(); |
||
| 90 | |||
| 91 | if ( is_a( $post, 'WP_Post' ) ) { |
||
| 92 | $this->import( $post ); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Returns post object |
||
| 98 | * |
||
| 99 | * @return object |
||
| 100 | */ |
||
| 101 | |||
| 102 | public function get_object() { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Checks if current user can edit this post |
||
| 110 | * |
||
| 111 | * @return boolean |
||
| 112 | */ |
||
| 113 | public function can_edit() { |
||
| 114 | if ( ! function_exists( 'current_user_can' ) ) { |
||
| 115 | return false; |
||
| 116 | } |
||
| 117 | if ( current_user_can( 'edit_post', $this->ID ) ) { |
||
| 118 | return true; |
||
| 119 | } |
||
| 120 | return false; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns post edit url |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function get_edit_url() { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns array of attached image ids |
||
| 136 | * |
||
| 137 | * @return array of ids |
||
| 138 | */ |
||
| 139 | public function get_attached_images() { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Returns array of attached images as ClassyImage objects |
||
| 164 | * |
||
| 165 | * @return array of ClassyImage |
||
| 166 | */ |
||
| 167 | public function attached_images() { |
||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * Returns first attached image id |
||
| 188 | * |
||
| 189 | * @return int/boolean |
||
| 190 | */ |
||
| 191 | public function get_first_attached_image_id() { |
||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * Returns first attached image |
||
| 219 | * |
||
| 220 | * @return ClassyImage |
||
| 221 | */ |
||
| 222 | public function first_attached_image( $size = 'thumbnail' ) { |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns post thumbnail |
||
| 236 | * |
||
| 237 | * @return ClassyImage |
||
| 238 | */ |
||
| 239 | public function thumbnail() { |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns post title with filters applied |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | public function get_title() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Alias for get_title |
||
| 267 | * |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | public function title() { |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * Returns the post content with filters applied. |
||
| 277 | * |
||
| 278 | * @param integer $page Page number, in case our post has <!--nextpage--> tags |
||
| 279 | * @return string Post content |
||
| 280 | */ |
||
| 281 | public function get_content( $page = 0 ) { |
||
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * Alias for get_content |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function content() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Returns post type object for current post |
||
| 315 | * |
||
| 316 | * @return object |
||
| 317 | */ |
||
| 318 | public function get_post_type() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns post permalink |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function get_permalink() { |
||
| 336 | |||
| 337 | |||
| 338 | /** |
||
| 339 | * Alias for get_permalink |
||
| 340 | * |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | public function permalink() { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns post preview of requested length. |
||
| 349 | * It will look for post_excerpt and will return it. |
||
| 350 | * If post contains <!-- more --> tag it will return content until it |
||
| 351 | * |
||
| 352 | * @param integer $len Number of words |
||
| 353 | * @param boolean $force If is set to true it will cut your post_excerpt to desired $len length |
||
| 354 | * @param string $readmore The text for 'readmore' link |
||
| 355 | * @param boolean $strip Should we strip tags? |
||
| 356 | * @return string Post preview |
||
| 357 | */ |
||
| 358 | public function get_preview( $len = 50, $force = false, $readmore = 'Read More', $strip = true ) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Returns comments array |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | public function get_comments( $status = 'approve', $order = 'DESC' ) { |
||
| 474 | } |
||
| 475 |
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.