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 | * Set presence |
||
77 | * @param string $presence |
||
78 | * @param integer $priority |
||
79 | * @param string $show |
||
80 | * @param string $status |
||
81 | */ |
||
82 | public function setPresence($presence, $priority, $show, $status) |
||
98 | |||
99 | /** |
||
100 | * Discover if a contact exists in the roster via jid |
||
101 | * @param string $jid |
||
102 | * @return boolean |
||
103 | */ |
||
104 | public function isContact($jid) |
||
108 | |||
109 | /** |
||
110 | * Add given contact to roster |
||
111 | * @param string $jid |
||
112 | * @param string $subscription |
||
113 | * @param string $name |
||
114 | * @param array $groups |
||
115 | */ |
||
116 | public function _addContact($jid, $subscription, $name = '', $groups = []) |
||
125 | |||
126 | /** |
||
127 | * @TODO |
||
128 | * @param callable $cb |
||
|
|||
129 | * @callback $cb ( ) |
||
130 | */ |
||
131 | public function fetch($cb = null) |
||
166 | |||
167 | /** |
||
168 | * @TODO |
||
169 | * @param string $jid |
||
170 | * @param string $type |
||
171 | * @param callable $cb |
||
172 | * @callback $cb ( ) |
||
173 | */ |
||
174 | public function setSubscription($jid, $type, $cb = null) |
||
179 | |||
180 | /** |
||
181 | * @TODO |
||
182 | * @param string $xml |
||
183 | * @param callable $cb |
||
184 | * @callback $cb ( ) |
||
185 | */ |
||
186 | public function rosterSet($xml, $cb = null) |
||
190 | |||
191 | /** |
||
192 | * Retrieve contact via jid |
||
193 | * @param string $jid |
||
194 | * @return array|null |
||
195 | */ |
||
196 | public function getContact($jid) |
||
203 | |||
204 | /** |
||
205 | * Return best presence for jid |
||
206 | * @param string $jid |
||
207 | * @return array|false |
||
208 | */ |
||
209 | public function getPresence($jid) |
||
232 | } |
||
233 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.