Complex classes like Highlander_Comments_Base 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 Highlander_Comments_Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class Highlander_Comments_Base { |
||
| 7 | function __construct() { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Set any global variables or class variables |
||
| 15 | * @since JetpackComments (1.4) |
||
| 16 | */ |
||
| 17 | protected function setup_globals() {} |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Setup actions for methods in this class |
||
| 21 | * @since JetpackComments (1.4) |
||
| 22 | */ |
||
| 23 | protected function setup_actions() { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Setup filters for methods in this class |
||
| 33 | * @since JetpackComments (1.4) |
||
| 34 | */ |
||
| 35 | protected function setup_filters() { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Is this a Highlander POST request? |
||
| 42 | * Optionally restrict to one or more credentials slug (facebook, twitter, ...) |
||
| 43 | * |
||
| 44 | * @param string Comment credentials slug |
||
| 45 | * @param ... |
||
| 46 | * @return false|string false if it's not a Highlander POST request. The matching credentials slug if it is. |
||
| 47 | */ |
||
| 48 | function is_highlander_comment_post() { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Signs an array of scalars with the self-hosted blog's Jetpack Token |
||
| 67 | * |
||
| 68 | * @param array $parameters |
||
| 69 | * @param string $key |
||
| 70 | * @return string HMAC |
||
| 71 | */ |
||
| 72 | static function sign_remote_comment_parameters( $parameters, $key ) { |
||
| 91 | |||
| 92 | /* |
||
| 93 | * After commenting as a guest while logged in, the user needs to see both: |
||
| 94 | * |
||
| 95 | * ( user_id = blah AND comment_approved = 0 ) |
||
| 96 | * and |
||
| 97 | * ( comment_author_email = blah AND comment_approved = 0 ) |
||
| 98 | * |
||
| 99 | * Core only does the first since the user is logged in. |
||
| 100 | * |
||
| 101 | * Add the second to the comments array. |
||
| 102 | */ |
||
| 103 | function comments_array( $comments ) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Comment sort comparator: comment_date_gmt |
||
| 133 | * |
||
| 134 | * @since JetpackComments (1.4) |
||
| 135 | * @param object $a |
||
| 136 | * @param object $b |
||
| 137 | * @return int |
||
| 138 | */ |
||
| 139 | public function sort_comments_by_comment_date_gmt( $a, $b ) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get the current commenter's information from their cookie |
||
| 149 | * |
||
| 150 | * @since JetpackComments (1.4) |
||
| 151 | * @return array Commenters information from cookie |
||
| 152 | */ |
||
| 153 | protected function get_current_commenter() { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Allows a logged out user to leave a comment as a facebook or twitter credentialed user. |
||
| 182 | * Overrides WordPress' core comment_registration option to treat these commenters as "registered" (verified) users. |
||
| 183 | * |
||
| 184 | * @since JetpackComments (1.4) |
||
| 185 | * @return If no |
||
| 186 | */ |
||
| 187 | function allow_logged_out_user_to_comment_as_external() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Allow a logged in user to post as a guest, FB, or twitter credentialed request. |
||
| 197 | * Bypasses WordPress' core overrides that force a logged in user to comment as that user. |
||
| 198 | * Respects comment_registration option. |
||
| 199 | * |
||
| 200 | * @since JetpackComments (1.4) |
||
| 201 | * @param array $comment_data |
||
| 202 | * @return int |
||
| 203 | */ |
||
| 204 | function allow_logged_in_user_to_comment_as_guest( $comment_data ) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Set the comment cookies or bail if comment is invalid |
||
| 254 | * |
||
| 255 | * @since JetpackComments (1.4) |
||
| 256 | * @param type $comment_id |
||
| 257 | * @return If comment is invalid |
||
| 258 | */ |
||
| 259 | public function set_comment_cookies( $comment_id ) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get an avatar from Photon |
||
| 283 | * |
||
| 284 | * @since JetpackComments (1.4) |
||
| 285 | * @param string $url |
||
| 286 | * @param int $size |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | protected function photon_avatar( $url, $size ) { |
||
| 294 | } |
||
| 295 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: