Complex classes like Comments 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 Comments, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Comments { |
||
| 25 | use |
||
| 26 | Accessor, |
||
| 27 | Singleton; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Cache_prefix |
||
| 31 | */ |
||
| 32 | protected $cache; |
||
| 33 | /** |
||
| 34 | * @var int Avatar size in px, can be redefined |
||
| 35 | */ |
||
| 36 | public $avatar_size = 36; |
||
| 37 | |||
| 38 | protected function construct () { |
||
| 41 | /** |
||
| 42 | * Returns database index |
||
| 43 | * |
||
| 44 | * @return int |
||
| 45 | */ |
||
| 46 | protected function cdb () { |
||
| 49 | /** |
||
| 50 | * Get comment data |
||
| 51 | * |
||
| 52 | * @param int $id Comment id |
||
| 53 | * |
||
| 54 | * @return array|false Array of comment data on success or <b>false</b> on failure |
||
| 55 | */ |
||
| 56 | function get ($id) { |
||
| 75 | /** |
||
| 76 | * Add new comment |
||
| 77 | * |
||
| 78 | * @param int $item Item id |
||
| 79 | * @param string $module Module name |
||
| 80 | * @param string $text Comment text |
||
| 81 | * @param int $parent Parent comment id |
||
| 82 | * |
||
| 83 | * @return false|int |
||
| 84 | */ |
||
| 85 | function add ($item, $module, $text, $parent = 0) { |
||
| 143 | /** |
||
| 144 | * Set comment text |
||
| 145 | * |
||
| 146 | * @param int $id Comment id |
||
| 147 | * @param string $text New comment text |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | function set ($id, $text) { |
||
| 175 | /** |
||
| 176 | * Delete comment |
||
| 177 | * |
||
| 178 | * @param int $id Comment id |
||
| 179 | * |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | function del ($id) { |
||
| 209 | /** |
||
| 210 | * Delete all comments of specified item |
||
| 211 | * |
||
| 212 | * @param int $item Item id |
||
| 213 | * @param string $module Module name |
||
| 214 | * |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | function del_all ($item, $module) { |
||
| 232 | /** |
||
| 233 | * Count of comments for specified item |
||
| 234 | * |
||
| 235 | * @param int $item Item id |
||
| 236 | * @param string $module Module name |
||
| 237 | * |
||
| 238 | * @return int |
||
| 239 | */ |
||
| 240 | function count ($item, $module) { |
||
| 257 | /** |
||
| 258 | * Get comments tree in html format for specified item (look at ::block() method before usage) |
||
| 259 | * |
||
| 260 | * @param int $item Item id |
||
| 261 | * @param string $module Module name |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | function tree ($item, $module) { |
||
| 268 | /** |
||
| 269 | * Get comments structure of specified item |
||
| 270 | * |
||
| 271 | * @param int $item |
||
| 272 | * @param string $module |
||
| 273 | * @param int $parent |
||
| 274 | * |
||
| 275 | * @return false|array |
||
| 276 | */ |
||
| 277 | function tree_data ($item, $module, $parent = 0) { |
||
| 315 | /** |
||
| 316 | * Get comments tree in html format for given data structure (usually uses ::tree_data() method) |
||
| 317 | * |
||
| 318 | * @param array[] $comments |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | function tree_html ($comments) { |
||
| 379 | /** |
||
| 380 | * Get comments block with comments tree and comments sending form |
||
| 381 | * |
||
| 382 | * @param int $item Item id |
||
| 383 | * @param string $module Module name |
||
| 384 | * |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | function block ($item, $module) { |
||
| 426 | } |
||
| 427 |