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 ) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Comment sort comparator: comment_date_gmt |
||
| 138 | * |
||
| 139 | * @since JetpackComments (1.4) |
||
| 140 | * @param object $a |
||
| 141 | * @param object $b |
||
| 142 | * @return int |
||
| 143 | */ |
||
| 144 | public function sort_comments_by_comment_date_gmt( $a, $b ) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get the current commenter's information from their cookie |
||
| 154 | * |
||
| 155 | * @since JetpackComments (1.4) |
||
| 156 | * @return array Commenters information from cookie |
||
| 157 | */ |
||
| 158 | protected function get_current_commenter() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Allows a logged out user to leave a comment as a facebook or twitter credentialed user. |
||
| 187 | * Overrides WordPress' core comment_registration option to treat these commenters as "registered" (verified) users. |
||
| 188 | * |
||
| 189 | * @since JetpackComments (1.4) |
||
| 190 | * @return If no |
||
| 191 | */ |
||
| 192 | function allow_logged_out_user_to_comment_as_external() { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Allow a logged in user to post as a guest, FB, or twitter credentialed request. |
||
| 202 | * Bypasses WordPress' core overrides that force a logged in user to comment as that user. |
||
| 203 | * Respects comment_registration option. |
||
| 204 | * |
||
| 205 | * @since JetpackComments (1.4) |
||
| 206 | * @param array $comment_data |
||
| 207 | * @return int |
||
| 208 | */ |
||
| 209 | function allow_logged_in_user_to_comment_as_guest( $comment_data ) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Set the comment cookies or bail if comment is invalid |
||
| 267 | * |
||
| 268 | * @since JetpackComments (1.4) |
||
| 269 | * @param type $comment_id |
||
| 270 | * @return If comment is invalid |
||
| 271 | */ |
||
| 272 | public function set_comment_cookies( $comment_id ) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get an avatar from Photon |
||
| 297 | * |
||
| 298 | * @since JetpackComments (1.4) |
||
| 299 | * @param string $url |
||
| 300 | * @param int $size |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | protected function photon_avatar( $url, $size ) { |
||
| 308 | } |
||
| 309 |
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: