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) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Initialises Instance based on provided post id |
||
| 87 | */ |
||
| 88 | protected function init() { |
||
| 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() { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns the Post Edit url |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function get_edit_url() { |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * Returns post thumbnail |
||
| 137 | * |
||
| 138 | * @return ClassyImage |
||
| 139 | */ |
||
| 140 | public function get_thumbnail() { |
||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * Returns post title with filters applied |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public function get_title() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Alias for get_title |
||
| 162 | * |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | public function title() { |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * Returns the post content with filters applied. |
||
| 172 | * |
||
| 173 | * @param integer $page Page number, in case our post has <!--nextpage--> tags |
||
| 174 | * @return string Post content |
||
| 175 | */ |
||
| 176 | public function get_content( $page = 0 ) { |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * Alias for get_content |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function content() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Returns post type object for current post |
||
| 210 | * |
||
| 211 | * @return object |
||
| 212 | */ |
||
| 213 | public function get_post_type() { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Returns post permalink |
||
| 219 | * |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | public function get_permalink() { |
||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * Alias for get_permalink |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | public function permalink() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Returns post preview of requested length. |
||
| 244 | * It will look for post_excerpt and will return it. |
||
| 245 | * If post contains <!-- more --> tag it will return content until it |
||
| 246 | * |
||
| 247 | * @param integer $len Number of words |
||
| 248 | * @param boolean $force If is set to true it will cut your post_excerpt to desired $len length |
||
| 249 | * @param string $readmore The text for 'readmore' link |
||
| 250 | * @param boolean $strip Should we strip tags? |
||
| 251 | * @return string Post preview |
||
| 252 | */ |
||
| 253 | public function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Returns comments array |
||
| 337 | * @return array |
||
| 338 | */ |
||
| 339 | public function get_comments($status = 'approve', $order = 'DESC') { |
||
| 372 | |||
| 373 | } |
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.