Complex classes like WP2D_API 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 WP2D_API, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class WP2D_API { |
||
28 | |||
29 | /** |
||
30 | * The provider name to display when posting to diaspora*. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | public $provider = 'WP to diaspora*'; |
||
35 | |||
36 | /** |
||
37 | * The last http request error that occurred. |
||
38 | * |
||
39 | * @var WP_Error |
||
40 | */ |
||
41 | private $_last_error; |
||
42 | |||
43 | /** |
||
44 | * Security token to be used for making requests. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | private $_token; |
||
49 | |||
50 | /** |
||
51 | * Save the cookies for the requests. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | private $_cookies; |
||
56 | |||
57 | /** |
||
58 | * The last http request made to diaspora*. |
||
59 | * Contains the response and request infos. |
||
60 | * |
||
61 | * @var object |
||
62 | */ |
||
63 | private $_last_request; |
||
64 | |||
65 | /** |
||
66 | * Is this a secure server, use HTTPS instead of HTTP? |
||
67 | * |
||
68 | * @var boolean |
||
69 | */ |
||
70 | private $_is_secure; |
||
71 | |||
72 | /** |
||
73 | * The pod domain to make the http requests to. |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | private $_pod; |
||
78 | |||
79 | /** |
||
80 | * Username to use when logging in to diaspora*. |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | private $_username; |
||
85 | |||
86 | /** |
||
87 | * Password to use when logging in to diaspora*. |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | private $_password; |
||
92 | |||
93 | /** |
||
94 | * Remember the current login state. |
||
95 | * |
||
96 | * @var boolean |
||
97 | */ |
||
98 | private $_is_logged_in = false; |
||
99 | |||
100 | /** |
||
101 | * The list of user's aspects, which get set after ever http request. |
||
102 | * |
||
103 | * @var array |
||
104 | */ |
||
105 | private $_aspects = array(); |
||
106 | |||
107 | /** |
||
108 | * The list of user's connected services, which get set after ever http request. |
||
109 | * |
||
110 | * @var array |
||
111 | */ |
||
112 | private $_services = array(); |
||
113 | |||
114 | /** |
||
115 | * List of regex expressions used to filter out details from http request responses. |
||
116 | * |
||
117 | * @var array |
||
118 | */ |
||
119 | private $_regexes = array( |
||
120 | 'token' => '/content="(.*?)" name="csrf-token"|name="csrf-token" content="(.*?)"/', |
||
121 | 'aspects' => '/"aspects"\:(\[.*?\])/', |
||
122 | 'services' => '/"configured_services"\:(\[.*?\])/', |
||
123 | ); |
||
124 | |||
125 | /** |
||
126 | * The full pod url, with the used protocol. |
||
127 | * |
||
128 | * @param string $path Path to add to the pod url. |
||
129 | * @return string Full pod url. |
||
130 | */ |
||
131 | public function get_pod_url( $path = '' ) { |
||
132 | $path = trim( $path, ' /' ); |
||
133 | |||
134 | // Add a slash to the beginning? |
||
135 | if ( '' !== $path ) { |
||
136 | $path = '/' . $path; |
||
137 | } |
||
138 | |||
139 | return sprintf( 'http%s://%s%s', ( $this->_is_secure ) ? 's' : '', $this->_pod, $path ); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Constructor to initialise the connection to diaspora*. |
||
144 | * |
||
145 | * @param string $pod The pod domain to connect to. |
||
146 | * @param boolean $is_secure Is this a secure server? (Default: true). |
||
147 | */ |
||
148 | public function __construct( $pod, $is_secure = true ) { |
||
153 | |||
154 | /** |
||
155 | * Initialise the connection to diaspora*. The pod and protocol can be changed by passing new parameters. |
||
156 | * Check if we can connect to the pod to retrieve the token. |
||
157 | * |
||
158 | * @param string $pod Pod domain to connect to, if it should be changed. |
||
159 | * @param boolean $is_secure Is this a secure server? (Default: true). |
||
160 | * @return boolean True if we could get the token, else false. |
||
161 | */ |
||
162 | public function init( $pod = null, $is_secure = true ) { |
||
192 | |||
193 | /** |
||
194 | * Check if there is an API error around. |
||
195 | * |
||
196 | * @return boolean If there is an API error around. |
||
197 | */ |
||
198 | public function has_last_error() { |
||
201 | |||
202 | /** |
||
203 | * Get the last API error object. |
||
204 | * |
||
205 | * @param boolean $clear If the error should be cleared after returning it. |
||
206 | * @return WP_Error|null The last API error object or null. |
||
207 | */ |
||
208 | public function get_last_error_object( $clear = true ) { |
||
213 | |||
214 | /** |
||
215 | * Get the last API error message. |
||
216 | * |
||
217 | * @param boolean $clear If the error should be cleared after returning it. |
||
218 | * @return string The last API error message. |
||
219 | */ |
||
220 | public function get_last_error( $clear = false ) { |
||
225 | |||
226 | /** |
||
227 | * Fetch the secure token from Diaspora and save it for future use. |
||
228 | * |
||
229 | * @param boolean $force Force to fetch a new token. |
||
230 | * @return string The fetched token. |
||
231 | */ |
||
232 | private function _fetch_token( $force = false ) { |
||
240 | |||
241 | /** |
||
242 | * Check if the API has been initialised. Otherwise set the last error. |
||
243 | * |
||
244 | * @return boolean Has the connection been initialised? |
||
245 | */ |
||
246 | private function _check_init() { |
||
253 | |||
254 | /** |
||
255 | * Check if we're logged in. Otherwise set the last error. |
||
256 | * |
||
257 | * @return boolean Are we logged in already? |
||
258 | */ |
||
259 | private function _check_login() { |
||
269 | |||
270 | /** |
||
271 | * Check if we are logged in. |
||
272 | * |
||
273 | * @return boolean Are we logged in already? |
||
274 | */ |
||
275 | public function is_logged_in() { |
||
278 | |||
279 | /** |
||
280 | * Log in to diaspora*. |
||
281 | * |
||
282 | * @param string $username Username used for login. |
||
283 | * @param string $password Password used for login. |
||
284 | * @param boolean $force Force a new login even if we are already logged in. |
||
285 | * @return boolean Did the login succeed? |
||
286 | */ |
||
287 | public function login( $username, $password, $force = false ) { |
||
343 | |||
344 | /** |
||
345 | * Perform a logout, resetting all login info. |
||
346 | * |
||
347 | * @since 1.6.0 |
||
348 | */ |
||
349 | public function logout() { |
||
356 | |||
357 | /** |
||
358 | * Perform a deinitialisation, resetting all class variables. |
||
359 | * |
||
360 | * @since 1.7.0 |
||
361 | */ |
||
362 | public function deinit() { |
||
369 | |||
370 | /** |
||
371 | * Post to diaspora*. |
||
372 | * |
||
373 | * @param string $text The text to post. |
||
374 | * @param array|string $aspects The aspects to post to. Array or comma seperated ids. |
||
375 | * @param array $extra_data Any extra data to be added to the post call. |
||
376 | * @return boolean|object Return the response data of the new diaspora* post if successfully posted, else false. |
||
377 | */ |
||
378 | public function post( $text, $aspects = 'public', $extra_data = array() ) { |
||
440 | |||
441 | /** |
||
442 | * Delete a post or comment from diaspora*. |
||
443 | * |
||
444 | * @since 1.6.0 |
||
445 | * |
||
446 | * @param string $what What to delete, 'post' or 'comment'. |
||
447 | * @param string $id The ID of the post or comment to delete. |
||
448 | * @return boolean If the deletion was successful. |
||
449 | */ |
||
450 | public function delete( $what, $id ) { |
||
511 | |||
512 | /** |
||
513 | * Get the list of aspects. |
||
514 | * |
||
515 | * @param boolean $force Force to fetch new aspects. |
||
516 | * @return array Array of aspect objects. |
||
517 | */ |
||
518 | public function get_aspects( $force = false ) { |
||
522 | |||
523 | /** |
||
524 | * Get the list of connected services. |
||
525 | * |
||
526 | * @param boolean $force Force to fetch new connected services. |
||
527 | * @return array Array of service objects. |
||
528 | */ |
||
529 | public function get_services( $force = false ) { |
||
533 | |||
534 | /** |
||
535 | * Get the list of aspects or connected services. |
||
536 | * |
||
537 | * @param string $type Type of list to get. |
||
538 | * @param array $list The current list of items. |
||
539 | * @param boolean $force Force to fetch new list. |
||
540 | * @return boolean Was the list fetched successfully? |
||
541 | */ |
||
542 | private function _get_aspects_services( $type, $list, $force ) { |
||
591 | |||
592 | /** |
||
593 | * Send an http(s) request via WP_HTTP API. |
||
594 | * |
||
595 | * @see WP_Http::request() |
||
596 | * |
||
597 | * @param string $url The URL to request. |
||
598 | * @param array $args Arguments to be posted with the request. |
||
599 | * @return object An object containing details about this request. |
||
600 | */ |
||
601 | private function _request( $url, $args = array() ) { |
||
661 | |||
662 | /** |
||
663 | * Helper method to set the last occurred error. |
||
664 | * |
||
665 | * @see WP_Error::__construct() |
||
666 | * @since 1.6.0 |
||
667 | * |
||
668 | * @param string|int $code Error code. |
||
669 | * @param string $message Error message. |
||
670 | * @param mixed $data Error data. |
||
671 | */ |
||
672 | private function _error( $code, $message, $data = '' ) { |
||
680 | |||
681 | /** |
||
682 | * Parse the regex and return the found string. |
||
683 | * |
||
684 | * @param string $regex Shorthand of a saved regex or a custom regex. |
||
685 | * @param string $content Text to parse the regex with. |
||
686 | * @return string The found string, or an empty string. |
||
687 | */ |
||
688 | private function _parse_regex( $regex, $content ) { |
||
697 | } |
||
698 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.