Complex classes like Application 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 Application, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Application extends \PHPDaemon\Core\AppInstance { |
||
16 | protected $redis; |
||
17 | protected $sessions; |
||
18 | |||
19 | public $wss; |
||
20 | |||
21 | /** |
||
22 | * Setting default config options |
||
23 | * @return array|bool |
||
24 | */ |
||
25 | protected function getConfigDefaults() { |
||
55 | |||
56 | /** |
||
57 | * Set Redis instance |
||
58 | * @param \PHPDaemon\Clients\Redis\Pool $redis |
||
59 | * @return $this |
||
60 | */ |
||
61 | public function setRedis(\PHPDaemon\Clients\Redis\Pool $redis) { |
||
65 | |||
66 | /** |
||
67 | * getLocalSubscribersCount |
||
68 | * @param string $chan |
||
69 | * @return integer |
||
70 | */ |
||
71 | public function getLocalSubscribersCount($chan) { |
||
74 | |||
75 | /** |
||
76 | * subscribe |
||
77 | * @param string $chan [@todo description] |
||
78 | * @param callable $cb [@todo description] |
||
79 | * @param callable $opcb [@todo description] |
||
80 | * @callback $cb ( ) |
||
81 | * @callback $opcb ( ) |
||
82 | * @return void |
||
83 | */ |
||
84 | public function subscribe($chan, $cb, $opcb = null) { |
||
87 | |||
88 | /** |
||
89 | * setnx |
||
90 | * @param string $key [@todo description] |
||
91 | * @param mixed $value [@todo description] |
||
92 | * @param callable $cb [@todo description] |
||
93 | * @callback $cb ( ) |
||
94 | * @return void |
||
95 | */ |
||
96 | public function setnx($key, $value, $cb = null) { |
||
99 | |||
100 | /** |
||
101 | * setkey |
||
102 | * @param string $key [@todo description] |
||
103 | * @param mixed $value [@todo description] |
||
104 | * @param callable $cb [@todo description] |
||
105 | * @callback $cb ( ) |
||
106 | * @return void |
||
107 | */ |
||
108 | public function setkey($key, $value, $cb = null) { |
||
111 | |||
112 | /** |
||
113 | * getkey |
||
114 | * @param string $key [@todo description] |
||
115 | * @param callable $cb [@todo description] |
||
116 | * @callback $cb ( ) |
||
117 | * @return void |
||
118 | */ |
||
119 | public function getkey($key, $cb = null) { |
||
122 | |||
123 | /** |
||
124 | * expire |
||
125 | * @param string $key [@todo description] |
||
126 | * @param integer $seconds [@todo description] |
||
127 | * @param callable $cb [@todo description] |
||
128 | * @callback $cb ( ) |
||
129 | * @return void |
||
130 | */ |
||
131 | public function expire($key, $seconds, $cb = null) { |
||
134 | |||
135 | /** |
||
136 | * unsubscribe |
||
137 | * @param string $chan [@todo description] |
||
138 | * @param callable $cb [@todo description] |
||
139 | * @param callable $opcb [@todo description] |
||
140 | * @callback $cb ( ) |
||
141 | * @callback $opcb ( ) |
||
142 | * @return void |
||
143 | */ |
||
144 | public function unsubscribe($chan, $cb, $opcb = null) { |
||
147 | |||
148 | /** |
||
149 | * unsubscribeReal |
||
150 | * @param string $chan [@todo description] |
||
151 | * @param callable $opcb [@todo description] |
||
152 | * @callback $opcb ( ) |
||
153 | * @return void |
||
154 | */ |
||
155 | public function unsubscribeReal($chan, $opcb = null) { |
||
158 | |||
159 | /** |
||
160 | * publish |
||
161 | * @param string $chan [@todo description] |
||
162 | * @param callable $cb [@todo description] |
||
163 | * @param callable $opcb [@todo description] |
||
164 | * @callback $cb ( ) |
||
165 | * @callback $opcb ( ) |
||
166 | * @return void |
||
167 | */ |
||
168 | public function publish($chan, $cb, $opcb = null) { |
||
171 | |||
172 | /** |
||
173 | * Called when the worker is ready to go |
||
174 | * @return void |
||
175 | */ |
||
176 | public function onReady() { |
||
184 | |||
185 | /** |
||
186 | * onFinish |
||
187 | * @return void |
||
188 | */ |
||
189 | public function onFinish() { |
||
195 | |||
196 | /** |
||
197 | * attachWss |
||
198 | * @param \PHPDaemon\Network\Pool $wss |
||
199 | * @return boolean |
||
200 | */ |
||
201 | public function attachWss($wss) { |
||
209 | |||
210 | /** |
||
211 | * wsHandler |
||
212 | * @param object $ws [@todo description] |
||
213 | * @param string $path [@todo description] |
||
214 | * @param object $client [@todo description] |
||
215 | * @param callable $state [@todo description] |
||
216 | * @callback $state ( object $route ) |
||
217 | * @return boolean |
||
218 | */ |
||
219 | public function wsHandler($ws, $path, $client, $state) { |
||
243 | |||
244 | /** |
||
245 | * detachWss |
||
246 | * @param object $wss [@todo description] |
||
247 | * @return boolean |
||
248 | */ |
||
249 | public function detachWss($wss) { |
||
257 | |||
258 | /** |
||
259 | * beginSession |
||
260 | * @param string $path [@todo description] |
||
261 | * @param string $sessId [@todo description] |
||
262 | * @param string $server [@todo description] |
||
263 | * @return object |
||
264 | */ |
||
265 | public function beginSession($path, $sessId, $server) { |
||
279 | |||
280 | /** |
||
281 | * getRouteOptions |
||
282 | * @param string $path [@todo description] |
||
283 | * @return array |
||
284 | */ |
||
285 | public function getRouteOptions($path) { |
||
301 | |||
302 | /** |
||
303 | * endSession |
||
304 | * @param Session $session |
||
305 | * @return void |
||
306 | */ |
||
307 | public function endSession($session) { |
||
310 | |||
311 | /** |
||
312 | * Creates Request. |
||
313 | * @param object $req Request. |
||
314 | * @param object $upstream Upstream application instance. |
||
315 | * @return object Request. |
||
316 | */ |
||
317 | public function beginRequest($req, $upstream) { |
||
378 | |||
379 | /** |
||
380 | * callMethod |
||
381 | * @param string $method [@todo description] |
||
382 | * @param object $req [@todo description] |
||
383 | * @param object $upstream [@todo description] |
||
384 | * @return object |
||
385 | */ |
||
386 | public function callMethod($method, $req, $upstream) { |
||
397 | } |
||
398 |