Complex classes like XMPPRoster 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 XMPPRoster, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class XMPPRoster |
||
7 | { |
||
8 | use EventHandlers; |
||
9 | use \PHPDaemon\Traits\ClassWatchdog; |
||
10 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
11 | |||
12 | /** |
||
13 | * @var Connection |
||
14 | */ |
||
15 | public $xmpp; |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | public $roster_array = []; |
||
21 | |||
22 | /** |
||
23 | * @var boolean |
||
24 | */ |
||
25 | public $track_presence = true; |
||
26 | |||
27 | /** |
||
28 | * @var boolean |
||
29 | */ |
||
30 | public $auto_subscribe = true; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | public $ns = 'jabber:iq:roster'; |
||
36 | |||
37 | /** |
||
38 | * Constructor |
||
39 | * @param Connection $xmpp |
||
40 | */ |
||
41 | public function __construct($xmpp) |
||
74 | |||
75 | /** |
||
76 | * @TODO |
||
77 | * @param string $xml |
||
78 | * @param callable $cb |
||
79 | * @callback $cb ( ) |
||
80 | */ |
||
81 | public function rosterSet($xml, $cb = null) |
||
85 | |||
86 | /** |
||
87 | * @TODO |
||
88 | * @param string $jid |
||
89 | * @param string $type |
||
90 | * @param callable $cb |
||
91 | * @callback $cb ( ) |
||
92 | */ |
||
93 | public function setSubscription($jid, $type, $cb = null) |
||
97 | |||
98 | /** |
||
99 | * @TODO |
||
100 | * @param callable $cb |
||
101 | * @callback $cb ( ) |
||
102 | */ |
||
103 | public function fetch($cb = null) |
||
138 | |||
139 | /** |
||
140 | * Add given contact to roster |
||
141 | * @param string $jid |
||
142 | * @param string $subscription |
||
143 | * @param string $name |
||
144 | * @param array $groups |
||
145 | */ |
||
146 | public function _addContact($jid, $subscription, $name = '', $groups = []) |
||
155 | |||
156 | /** |
||
157 | * Retrieve contact via jid |
||
158 | * @param string $jid |
||
159 | * @return array|null |
||
160 | */ |
||
161 | public function getContact($jid) |
||
168 | |||
169 | /** |
||
170 | * Discover if a contact exists in the roster via jid |
||
171 | * @param string $jid |
||
172 | * @return boolean |
||
173 | */ |
||
174 | public function isContact($jid) |
||
178 | |||
179 | /** |
||
180 | * Set presence |
||
181 | * @param string $presence |
||
182 | * @param integer $priority |
||
183 | * @param string $show |
||
184 | * @param string $status |
||
185 | */ |
||
186 | public function setPresence($presence, $priority, $show, $status) |
||
198 | |||
199 | /** |
||
200 | * Return best presence for jid |
||
201 | * @param string $jid |
||
202 | * @return array|false |
||
203 | */ |
||
204 | public function getPresence($jid) |
||
221 | } |
||
222 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.