Complex classes like Post 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 Post, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Post extends Basis { |
||
13 | |||
14 | /** |
||
15 | * Current post id. |
||
16 | * |
||
17 | * @var int |
||
18 | */ |
||
19 | public $ID; |
||
20 | |||
21 | /** |
||
22 | * Stores current post object. |
||
23 | * |
||
24 | * @var \WP_Post |
||
25 | */ |
||
26 | protected $object; |
||
27 | |||
28 | /** |
||
29 | * Post title. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | public $post_title; |
||
34 | |||
35 | /** |
||
36 | * Post name. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | public $post_name; |
||
41 | |||
42 | /** |
||
43 | * Post content (raw). |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | public $post_content; |
||
48 | |||
49 | /** |
||
50 | * Post type. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | public $post_type; |
||
55 | |||
56 | /** |
||
57 | * Post author id. |
||
58 | * |
||
59 | * @var int |
||
60 | */ |
||
61 | public $post_author; |
||
62 | |||
63 | /** |
||
64 | * Post date. String as stored in the WP database, ex: 2012-04-23 08:11:23. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | public $post_date; |
||
69 | |||
70 | /** |
||
71 | * Post excerpt (raw). |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | public $post_excerpt; |
||
76 | |||
77 | /** |
||
78 | * Post status. It can be draft, publish, pending, private, trash, etc. |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | public $post_status; |
||
83 | |||
84 | /** |
||
85 | * Post permalink. |
||
86 | * |
||
87 | * @var string |
||
88 | */ |
||
89 | public $permalink; |
||
90 | |||
91 | /** |
||
92 | * Main constructor function. If ID won't be provided we will try to find it, based on your query. |
||
93 | * |
||
94 | * @param object|int $post WP_Post or WP_Post.ID. |
||
95 | */ |
||
96 | public function __construct( $post = null ) { |
||
104 | |||
105 | /** |
||
106 | * Initialises Instance based on provided post id. |
||
107 | */ |
||
108 | protected function init() { |
||
115 | |||
116 | /** |
||
117 | * Returns post object. |
||
118 | * |
||
119 | * @return \WP_Post |
||
120 | */ |
||
121 | public function get_object() { |
||
126 | |||
127 | /** |
||
128 | * Checks if current user can edit this post. |
||
129 | * |
||
130 | * @return boolean |
||
131 | */ |
||
132 | public function can_edit() { |
||
142 | |||
143 | /** |
||
144 | * Returns post edit url. |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function get_edit_url() { |
||
155 | |||
156 | /** |
||
157 | * Returns array of attached image ids. |
||
158 | * |
||
159 | * @return false|array of ids |
||
160 | */ |
||
161 | public function get_attached_images() { |
||
181 | |||
182 | /** |
||
183 | * Returns array of attached images as Image objects. |
||
184 | * |
||
185 | * @return array of Image |
||
186 | */ |
||
187 | public function attached_images() { |
||
203 | |||
204 | |||
205 | /** |
||
206 | * Returns first attached image id. |
||
207 | * |
||
208 | * @return int|boolean |
||
209 | */ |
||
210 | public function get_first_attached_image_id() { |
||
232 | |||
233 | /** |
||
234 | * Returns first attached image. |
||
235 | * |
||
236 | * @return Image |
||
237 | */ |
||
238 | public function first_attached_image() { |
||
248 | |||
249 | /** |
||
250 | * Returns post thumbnail. |
||
251 | * |
||
252 | * @return Image |
||
253 | */ |
||
254 | public function thumbnail() { |
||
267 | |||
268 | /** |
||
269 | * Returns post title with filters applied. |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | public function get_title() { |
||
276 | |||
277 | /** |
||
278 | * Alias for get_title. |
||
279 | * |
||
280 | * @return string |
||
281 | */ |
||
282 | public function title() { |
||
285 | |||
286 | /** |
||
287 | * Returns the post content with filters applied. |
||
288 | * |
||
289 | * @param integer $page Page number, in case our post has <!--nextpage--> tags. |
||
290 | * |
||
291 | * @return string Post content |
||
292 | */ |
||
293 | public function get_content( $page = 0 ) { |
||
314 | |||
315 | /** |
||
316 | * Alias for get_content. |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | public function content() { |
||
323 | |||
324 | /** |
||
325 | * Returns post type object for current post. |
||
326 | * |
||
327 | * @return object |
||
328 | */ |
||
329 | public function get_post_type() { |
||
332 | |||
333 | /** |
||
334 | * Returns post permalink. |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | public function get_permalink() { |
||
347 | |||
348 | /** |
||
349 | * Alias for get_permalink |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | public function permalink() { |
||
356 | |||
357 | /** |
||
358 | * Returns post preview of requested length. |
||
359 | * It will look for post_excerpt and will return it. |
||
360 | * If post contains <!-- more --> tag it will return content until it |
||
361 | * |
||
362 | * @param integer $len Number of words. |
||
363 | * @param boolean $force If is set to true it will cut your post_excerpt to desired $len length. |
||
364 | * @param string $readmore The text for 'readmore' link. |
||
365 | * @param boolean $strip Should we strip tags. |
||
366 | * |
||
367 | * @return string Post preview. |
||
368 | */ |
||
369 | public function get_preview( $len = 50, $force = false, $readmore = 'Read More', $strip = true ) { |
||
448 | |||
449 | /** |
||
450 | * Returns comments array |
||
451 | * |
||
452 | * @param string $status Comment status. |
||
453 | * @param string $order Order for comments query. |
||
454 | * |
||
455 | * @return array |
||
456 | */ |
||
457 | public function get_comments( $status = 'approve', $order = 'DESC' ) { |
||
488 | } |
||
489 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.