Complex classes like user_data 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 user_data, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class user_data |
||
| 13 | { |
||
| 14 | /** @var \phpbb\auth\auth */ |
||
| 15 | protected $auth; |
||
| 16 | |||
| 17 | /** @var \phpbb\config\config */ |
||
| 18 | protected $config; |
||
| 19 | |||
| 20 | /** @var \phpbb\db\driver\driver_interface */ |
||
| 21 | protected $db; |
||
| 22 | |||
| 23 | /** @var \phpbb\profilefields\manager */ |
||
| 24 | protected $profile_fields; |
||
| 25 | |||
| 26 | /** @var \phpbb\language\language */ |
||
| 27 | protected $translator; |
||
| 28 | |||
| 29 | /** @var \phpbb\user */ |
||
| 30 | protected $user; |
||
| 31 | |||
| 32 | /** @var string */ |
||
| 33 | protected $phpbb_root_path; |
||
| 34 | |||
| 35 | /** @var string */ |
||
| 36 | protected $php_ext; |
||
| 37 | |||
| 38 | /** @var array */ |
||
| 39 | protected $user_cache = array(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Constructor |
||
| 43 | |||
| 44 | * |
||
| 45 | * @param \phpbb\auth\auth $auth Auth object |
||
| 46 | * @param \phpbb\config\config $config Config object |
||
| 47 | * @param \phpbb\db\driver\driver_interface $db Database connection |
||
| 48 | * @param \phpbb\profilefields\manager $profile_fields Profile fields manager |
||
| 49 | * @param \phpbb\language\language $translator Language object |
||
| 50 | * @param \phpbb\user $user User Object |
||
| 51 | * @param string $phpbb_root_path Path to the phpbb includes directory. |
||
| 52 | * @param string $php_ext php file extension |
||
| 53 | */ |
||
| 54 | public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\profilefields\manager $profile_fields, \phpbb\language\language $translator, \phpbb\user $user, $phpbb_root_path, $php_ext) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param array $user_ids |
||
| 68 | * @param string $sql_where |
||
| 69 | * @return array |
||
| 70 | */ |
||
| 71 | public function get_users(array $user_ids, $sql_where = '') |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param string $sql_where |
||
| 86 | * @param string $order_by |
||
| 87 | * @param int|bool $limit |
||
| 88 | * @return array|false |
||
| 89 | */ |
||
| 90 | public function query($sql_where = '', $order_by = '', $limit = false) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param array $row |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | public function get_data($row) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | public function get_profile_fields() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param array $row |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | protected function get_avatar(array $row) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param int $user_posts |
||
| 182 | * @return int|mixed |
||
| 183 | */ |
||
| 184 | protected function calculate_percent_posts($user_posts) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param int $last_visited |
||
| 191 | * @param string $date_format |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | protected function get_last_visit_date($last_visited, $date_format) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param int $user_id |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | protected function get_search_url($user_id) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param array $row |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | public function get_rank(array $row) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param array $users |
||
| 241 | */ |
||
| 242 | protected function get_additional_fields(array $users) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param array $row |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | protected function get_email_contact(array &$row) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param array $row |
||
| 287 | * @param array $can_receive_pm_list |
||
| 288 | * @param array $permanently_banned_users |
||
| 289 | * @return array |
||
| 290 | */ |
||
| 291 | protected function get_pm_contact(array &$row, array $can_receive_pm_list, array $permanently_banned_users) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param array $row |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | protected function get_jabber_contact(array &$row) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | protected function user_can_pm() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param array $row |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | protected function user_can_jabber(array $row) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param array $row |
||
| 344 | * @param array $can_receive_pm_list |
||
| 345 | * @param array $permanently_banned_users |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | protected function can_receive_pm(array $row, array $can_receive_pm_list, array $permanently_banned_users) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param int $user_id |
||
| 370 | * @param array $profile_fields_cache |
||
| 371 | */ |
||
| 372 | protected function get_custom_profile_fields($user_id, array $profile_fields_cache) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Get the list of users who can receive private messages |
||
| 397 | * |
||
| 398 | * @param array $user_ids |
||
| 399 | * @return array |
||
| 400 | */ |
||
| 401 | protected function get_can_receive_pm_list(array $user_ids) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get the list of permanently banned users |
||
| 409 | * |
||
| 410 | * @param array $user_ids |
||
| 411 | * @return array |
||
| 412 | */ |
||
| 413 | protected function get_banned_users_list(array $user_ids) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param array $ids |
||
|
|
|||
| 425 | * @param string $sql_where |
||
| 426 | * @param string $order_by |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | protected function get_sql_statement($sql_where = '', $order_by = '') |
||
| 436 | } |
||
| 437 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.