Complex classes like PacketHandler 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 PacketHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class PacketHandler |
||
14 | { |
||
15 | /** |
||
16 | * @var StreamHandler[] |
||
17 | */ |
||
18 | private $providers; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $handlers; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $sortedHandlers; |
||
29 | |||
30 | 12 | public function __construct() |
|
36 | |||
37 | /** |
||
38 | * @param StreamHandler $streamHandler |
||
39 | * @param string $identifier |
||
40 | * |
||
41 | * @return PacketHandler |
||
42 | */ |
||
43 | 6 | public function addProvider(StreamHandler $streamHandler, $identifier = null) : self |
|
57 | |||
58 | 3 | public function removeProvider(StreamHandler $streamHandler = null, $identifier = null) |
|
74 | |||
75 | 4 | public function hasProvider($identifier) : bool |
|
79 | |||
80 | 4 | public function getProvider($identifier) : StreamHandler |
|
88 | |||
89 | /** |
||
90 | * @return StreamHandler[] |
||
91 | */ |
||
92 | 5 | public function getProviders() : array |
|
96 | |||
97 | /** |
||
98 | * @param PacketInterface $packet |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | 1 | public function dispatch(PacketInterface $packet) |
|
110 | |||
111 | /** |
||
112 | * @param Package $package |
||
113 | */ |
||
114 | 3 | public function onPackage(Package $package, StreamHandler $streamHandler) |
|
135 | |||
136 | /** |
||
137 | * @param string $packetType |
||
138 | * @param bool $withPriorities |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | 3 | public function getHandlers(string $packetType = null, bool $withPriorities = false) : array |
|
164 | |||
165 | /** |
||
166 | * @param string $packetType |
||
167 | * |
||
168 | * @return bool |
||
169 | */ |
||
170 | 2 | public function hasHandlers(string $packetType = null) : bool |
|
174 | |||
175 | /** |
||
176 | * @param string $packetType |
||
177 | * @param callable $handler |
||
178 | * @param int $priority |
||
179 | * |
||
180 | * @return $this |
||
181 | */ |
||
182 | 3 | public function addHandler(string $packetType, callable $handler, int $priority = 0) : self |
|
190 | |||
191 | /** |
||
192 | * @param string $packetType |
||
193 | * @param callable $handler |
||
194 | * |
||
195 | * @return $this |
||
196 | */ |
||
197 | 2 | public function removeHandler(string $packetType, callable $handler) : self |
|
219 | |||
220 | /** |
||
221 | * @param PacketSubscriberInterface $subscriber |
||
222 | * |
||
223 | * @return $this |
||
224 | */ |
||
225 | 1 | public function addSubscriber(PacketSubscriberInterface $subscriber) : self |
|
241 | |||
242 | |||
243 | /** |
||
244 | * @param PacketSubscriberInterface $subscriber |
||
245 | * |
||
246 | * @return $this |
||
247 | */ |
||
248 | 1 | public function removeSubscriber(PacketSubscriberInterface $subscriber) |
|
261 | |||
262 | /** |
||
263 | * @param array $handlers |
||
264 | * @param PacketInterface $packet |
||
265 | */ |
||
266 | 1 | private function doDispatch($handlers, PacketInterface $packet) |
|
276 | |||
277 | /** |
||
278 | * @param string $packetType |
||
279 | */ |
||
280 | 3 | private function sortHandlers(string $packetType) |
|
289 | } |
||
290 |