Complex classes like contacts 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 contacts, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | abstract class contacts |
||
13 | { |
||
14 | /** @var \phpbb\auth\auth */ |
||
15 | protected $auth; |
||
16 | |||
17 | /** @var \phpbb\config\config */ |
||
18 | protected $config; |
||
19 | |||
20 | /** @var \phpbb\language\language */ |
||
21 | protected $translator; |
||
22 | |||
23 | /** @var \phpbb\user */ |
||
24 | protected $user; |
||
25 | |||
26 | /** @var string */ |
||
27 | protected $phpbb_root_path; |
||
28 | |||
29 | /** @var string */ |
||
30 | protected $php_ext; |
||
31 | |||
32 | /** @var bool */ |
||
33 | protected $mailto_allowed; |
||
34 | |||
35 | /** @var bool */ |
||
36 | protected $email_form_allowed; |
||
37 | |||
38 | /** @var bool */ |
||
39 | protected $jabber_allowed; |
||
40 | |||
41 | /** |
||
42 | * Constructor |
||
43 | * |
||
44 | * @param \phpbb\auth\auth $auth Auth object |
||
45 | * @param \phpbb\config\config $config Config object |
||
46 | * @param \phpbb\language\language $translator Language object |
||
47 | * @param \phpbb\user $user User Object |
||
48 | * @param string $phpbb_root_path Path to the phpbb includes directory. |
||
49 | * @param string $php_ext php file extension |
||
50 | */ |
||
51 | 89 | public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\language\language $translator, \phpbb\user $user, $phpbb_root_path, $php_ext) |
|
64 | |||
65 | /** |
||
66 | * @param array $row |
||
67 | * @return array |
||
68 | */ |
||
69 | 8 | protected function get_email_contact(array $row) |
|
83 | |||
84 | /** |
||
85 | * @param array $row |
||
86 | * @return bool |
||
87 | */ |
||
88 | 8 | protected function user_email_allowed(array $row) |
|
92 | |||
93 | /** |
||
94 | * @param array $row |
||
95 | * @return string |
||
96 | */ |
||
97 | 8 | protected function get_email_url(array $row) |
|
101 | |||
102 | /** |
||
103 | * @return bool |
||
104 | */ |
||
105 | 89 | protected function get_email_form_allowed() |
|
109 | |||
110 | /** |
||
111 | * @return bool |
||
112 | */ |
||
113 | 89 | protected function get_mailto_allowed() |
|
117 | |||
118 | /** |
||
119 | * @param array $row |
||
120 | * @return array |
||
121 | */ |
||
122 | 8 | protected function get_jabber_contact(array $row) |
|
123 | { |
||
124 | 8 | $jabber = array(); |
|
125 | 8 | if ($this->jabber_allowed && $row['user_jabber']) |
|
126 | 8 | { |
|
127 | $jabber = array( |
||
128 | 4 | 'ID' => 'jabber', |
|
129 | 4 | 'NAME' => $this->translator->lang('JABBER'), |
|
130 | 4 | 'U_CONTACT' => append_sid("{$this->phpbb_root_path}memberlist.{$this->php_ext}", 'mode=contact&action=jabber&u=' . $row['user_id']), |
|
131 | 4 | ); |
|
132 | 4 | } |
|
133 | |||
134 | 8 | return $jabber; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return bool |
||
139 | */ |
||
140 | 89 | protected function get_jabber_allowed() |
|
144 | |||
145 | /** |
||
146 | * @param array $row |
||
147 | * @param array $can_receive_pm_list |
||
148 | * @param array $permanently_banned_users |
||
149 | * @return array |
||
150 | */ |
||
151 | 8 | protected function get_pm_contact(array $row, array $can_receive_pm_list, array $permanently_banned_users) |
|
165 | |||
166 | /** |
||
167 | * @return bool |
||
168 | */ |
||
169 | 8 | protected function user_can_pm() |
|
173 | |||
174 | /** |
||
175 | * @param array $row |
||
176 | * @param array $can_receive_pm_list |
||
177 | * @param array $permanently_banned_users |
||
178 | * @return bool |
||
179 | */ |
||
180 | 8 | protected function can_receive_pm(array $row, array $can_receive_pm_list, array $permanently_banned_users) |
|
199 | |||
200 | /** |
||
201 | * Get the list of users who can receive private messages |
||
202 | * |
||
203 | * @param array $user_ids |
||
204 | * @return array |
||
205 | */ |
||
206 | 8 | protected function get_can_receive_pm_list(array $user_ids) |
|
211 | |||
212 | /** |
||
213 | * Get the list of permanently banned users |
||
214 | * |
||
215 | * @param array $user_ids |
||
216 | * @return array |
||
217 | */ |
||
218 | 8 | protected function get_banned_users_list(array $user_ids) |
|
227 | } |
||
228 |