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 int $pid |
||
75 | */ |
||
76 | public function __construct($pid = null) { |
||
81 | |||
82 | protected function verify_id($pid) { |
||
85 | |||
86 | /** |
||
87 | * Initialises Instance based on provided post id |
||
88 | */ |
||
89 | protected function init() { |
||
94 | |||
95 | /** |
||
96 | * Returns post object |
||
97 | * |
||
98 | * @return object |
||
99 | */ |
||
100 | |||
101 | public function get_object() { |
||
106 | |||
107 | /** |
||
108 | * Checks if current user can edit this post |
||
109 | * |
||
110 | * @return boolean |
||
111 | */ |
||
112 | public function can_edit() { |
||
121 | |||
122 | /** |
||
123 | * Returns the Post Edit url |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | public function get_edit_url() { |
||
132 | |||
133 | |||
134 | /** |
||
135 | * Returns post thumbnail |
||
136 | * |
||
137 | * @return ClassyImage |
||
138 | */ |
||
139 | public function get_thumbnail() { |
||
148 | |||
149 | |||
150 | /** |
||
151 | * Returns post title with filters applied |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | public function get_title() { |
||
158 | |||
159 | /** |
||
160 | * Alias for get_title |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | public function title() { |
||
167 | |||
168 | |||
169 | /** |
||
170 | * Returns the post content with filters applied. |
||
171 | * |
||
172 | * @param integer $page Page number, in case our post has <!--nextpage--> tags |
||
173 | * @return string Post content |
||
174 | */ |
||
175 | public function get_content( $page = 0 ) { |
||
196 | |||
197 | |||
198 | /** |
||
199 | * Alias for get_content |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | public function content() { |
||
206 | |||
207 | /** |
||
208 | * Returns post type object for current post |
||
209 | * |
||
210 | * @return object |
||
211 | */ |
||
212 | public function get_post_type() { |
||
215 | |||
216 | /** |
||
217 | * Returns post permalink |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | public function get_permalink() { |
||
230 | |||
231 | |||
232 | /** |
||
233 | * Alias for get_permalink |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | public function permalink() { |
||
240 | |||
241 | /** |
||
242 | * Returns post preview of requested length. |
||
243 | * It will look for post_excerpt and will return it. |
||
244 | * If post contains <!-- more --> tag it will return content until it |
||
245 | * |
||
246 | * @param integer $len Number of words |
||
247 | * @param boolean $force If is set to true it will cut your post_excerpt to desired $len length |
||
248 | * @param string $readmore The text for 'readmore' link |
||
249 | * @param boolean $strip Should we strip tags? |
||
250 | * @return string Post preview |
||
251 | */ |
||
252 | public function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true) { |
||
333 | |||
334 | } |
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.