Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Comment 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 Comment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Comment extends Basis { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Comment ID. |
||
| 17 | * |
||
| 18 | * @var int |
||
| 19 | */ |
||
| 20 | public $comment_ID; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Comment post ID. |
||
| 24 | * |
||
| 25 | * @var int |
||
| 26 | */ |
||
| 27 | public $comment_post_ID; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Comment Author name. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | public $comment_author; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Comment author email. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | public $comment_author_email; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Comment author link. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | public $comment_author_url; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Comment. |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | public $comment_content; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Comment approved. |
||
| 59 | * |
||
| 60 | * @var boolean |
||
| 61 | */ |
||
| 62 | public $comment_approved; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Comment date. |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | public $comment_date; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * User ID. |
||
| 73 | * |
||
| 74 | * @var int |
||
| 75 | */ |
||
| 76 | public $user_id; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Comment nested level. |
||
| 80 | * |
||
| 81 | * @var int |
||
| 82 | */ |
||
| 83 | public $level; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Comment Parent ID. |
||
| 87 | * |
||
| 88 | * @var int |
||
| 89 | */ |
||
| 90 | public $comment_parent; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Child comments. |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | protected $children = array(); |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Checks if provided arg is instance of WP_Comment and init it. |
||
| 101 | * |
||
| 102 | * @param \WP_Comment $item WP_Comment object. |
||
| 103 | */ |
||
| 104 | public function __construct( $item ) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns User object of comment author. |
||
| 112 | * |
||
| 113 | * @return \Classy\Models\User |
||
| 114 | */ |
||
| 115 | public function author() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns comment content. |
||
| 132 | * |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function content() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Return true if comment is approved. |
||
| 141 | * |
||
| 142 | * @return boolean |
||
| 143 | */ |
||
| 144 | public function approved() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Returns comment date. |
||
| 150 | * |
||
| 151 | * @param string $date_format Optional. PHP date format defaults to the date_format option if not specified. |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function date( $date_format = '' ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Returns true if comment has parent. |
||
| 164 | * |
||
| 165 | * @return boolean |
||
| 166 | */ |
||
| 167 | public function has_parent() { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Shows gravatar. |
||
| 173 | * |
||
| 174 | * @param integer $size avatar image size |
||
| 175 | * @param string $default |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function avatar( $size = 92, $default = '' ) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Returns email address that will be used for generating avatar. |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | protected function get_avatar_email() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Returns default avatar url. |
||
| 225 | * |
||
| 226 | * @param string $default |
||
| 227 | * @param string $email |
||
| 228 | * @param int $size |
||
| 229 | * @param string $host |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | protected function get_default_avatar( $default, $email, $size, $host ) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Returns gravatar host. |
||
| 264 | * |
||
| 265 | * @param string $email_hash |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | protected function get_avatar_host( $email_hash ) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns avatar url |
||
| 285 | * |
||
| 286 | * @param string $default |
||
| 287 | * @param string $host |
||
| 288 | * @param string $email_hash |
||
| 289 | * @param int $size |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | protected function get_avatar_url( $default, $host, $email_hash, $size ) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Adds child to current Comment |
||
| 305 | * |
||
| 306 | * @param Comment $comment |
||
| 307 | */ |
||
| 308 | public function add_child( $comment ) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Updates children nesting level param |
||
| 319 | * |
||
| 320 | * @return boolean |
||
| 321 | */ |
||
| 322 | View Code Duplication | protected function update_child_levels() { |
|
| 334 | } |
||
| 335 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.