Complex classes like RpcServer 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 RpcServer, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Comodojo\RpcServer; |
||
38 | class RpcServer { |
||
39 | |||
40 | /** |
||
41 | * Capabilities collector |
||
42 | * |
||
43 | * @const string |
||
44 | */ |
||
45 | const XMLRPC = 'xml'; |
||
46 | |||
47 | /** |
||
48 | * Capabilities collector |
||
49 | * |
||
50 | * @const string |
||
51 | */ |
||
52 | const JSONRPC = 'json'; |
||
53 | |||
54 | /** |
||
55 | * Capabilities collector |
||
56 | * |
||
57 | * @var Capabilities |
||
58 | */ |
||
59 | private $capabilities; |
||
60 | |||
61 | /** |
||
62 | * RpcMethods collector |
||
63 | * |
||
64 | * @var Methods |
||
65 | */ |
||
66 | private $methods; |
||
67 | |||
68 | /** |
||
69 | * Standard Rpc Errors collector |
||
70 | * |
||
71 | * @var Errors |
||
72 | */ |
||
73 | private $errors; |
||
74 | |||
75 | /** |
||
76 | * The request payload, better the RAW export of 'php://input' |
||
77 | * |
||
78 | * @var string |
||
79 | */ |
||
80 | private $payload; |
||
81 | |||
82 | /** |
||
83 | * Encryption key, in case of encrypted transport |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | private $encrypt; |
||
88 | |||
89 | /** |
||
90 | * Current encoding |
||
91 | * |
||
92 | * @var string |
||
93 | */ |
||
94 | private $encoding = 'utf-8'; |
||
95 | |||
96 | /** |
||
97 | * Current protocol |
||
98 | * |
||
99 | * @var string |
||
100 | */ |
||
101 | private $protocol; |
||
102 | |||
103 | /** |
||
104 | * Supported RPC protocols |
||
105 | * |
||
106 | * @var array |
||
107 | */ |
||
108 | private $supported_protocols = ['xml', 'json']; |
||
109 | |||
110 | /** |
||
111 | * Internal marker (encryption) |
||
112 | * |
||
113 | * @var bool |
||
114 | */ |
||
115 | private $request_is_encrypted = false; |
||
116 | |||
117 | /** |
||
118 | * Current logger |
||
119 | * |
||
120 | * @var LoggerInterface |
||
121 | */ |
||
122 | private $logger; |
||
123 | |||
124 | /** |
||
125 | * Class constructor |
||
126 | * |
||
127 | * @param string $protocol |
||
128 | * |
||
129 | * @throws Exception |
||
130 | * @throws InvalidArgumentException |
||
131 | */ |
||
132 | 90 | public function __construct($protocol, LoggerInterface $logger = null) { |
|
167 | |||
168 | /** |
||
169 | * Set RPC protocol (json or xml) |
||
170 | * |
||
171 | * @param string $protocol |
||
172 | * |
||
173 | * @return self |
||
174 | * @throws InvalidArgumentException |
||
175 | */ |
||
176 | 90 | public function setProtocol($protocol) { |
|
185 | |||
186 | /** |
||
187 | * Get RPC protocol |
||
188 | * |
||
189 | * @return string |
||
190 | */ |
||
191 | 15 | public function getProtocol() { |
|
196 | |||
197 | /** |
||
198 | * Set request payload, raw format |
||
199 | * |
||
200 | * @return self |
||
201 | */ |
||
202 | 87 | public function setPayload($payload) { |
|
209 | |||
210 | /** |
||
211 | * Get request payload |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | 3 | public function getPayload() { |
|
220 | |||
221 | 3 | public function setEncoding($encoding) { |
|
228 | |||
229 | 3 | public function getEncoding() { |
|
234 | |||
235 | /** |
||
236 | * Set encryption key; this will enable the NOT-STANDARD payload encryption |
||
237 | * |
||
238 | * @param string $key The encryption key |
||
239 | * |
||
240 | * @return self |
||
241 | * @throws InvalidArgumentException |
||
242 | */ |
||
243 | 6 | public function setEncryption($key) { |
|
252 | |||
253 | /** |
||
254 | * Get the ecryption key or null if no encryption is selected |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | 3 | public function getEncryption() { |
|
263 | |||
264 | /** |
||
265 | * Get capabilities object |
||
266 | * |
||
267 | * @deprecated |
||
268 | * @see Parameters::getCapabilities() |
||
269 | * @return Capabilities |
||
270 | */ |
||
271 | 6 | public function capabilities() { |
|
276 | |||
277 | /** |
||
278 | * Get capabilities object |
||
279 | * |
||
280 | * @deprecated |
||
281 | * @see Parameters::getCapabilities() |
||
282 | * @return Capabilities |
||
283 | */ |
||
284 | 6 | public function getCapabilities() { |
|
289 | |||
290 | /** |
||
291 | * Get methods object |
||
292 | * |
||
293 | * @deprecated |
||
294 | * @see Parameters::getMethods() |
||
295 | * @return Methods |
||
296 | */ |
||
297 | 36 | public function methods() { |
|
302 | |||
303 | /** |
||
304 | * Get methods object |
||
305 | * |
||
306 | * @return Methods |
||
307 | */ |
||
308 | 36 | public function getMethods() { |
|
313 | |||
314 | /** |
||
315 | * Get errors object |
||
316 | * |
||
317 | * @deprecated |
||
318 | * @see Parameters::getErrors() |
||
319 | * @return Errors |
||
320 | */ |
||
321 | public function errors() { |
||
326 | |||
327 | /** |
||
328 | * Get errors object |
||
329 | * |
||
330 | * @return Errors |
||
331 | */ |
||
332 | public function getErrors() { |
||
337 | |||
338 | /** |
||
339 | * Serve request |
||
340 | * |
||
341 | * @return string |
||
342 | * @throws Exception |
||
343 | */ |
||
344 | 84 | public function serve() { |
|
377 | |||
378 | /** |
||
379 | * Uncan the provided payload |
||
380 | * |
||
381 | * @param string $payload |
||
382 | * |
||
383 | * @return mixed |
||
384 | * @throws RpcException |
||
385 | */ |
||
386 | 84 | private function uncan($payload) { |
|
443 | |||
444 | /** |
||
445 | * Can the RPC response |
||
446 | * |
||
447 | * @param mixed $response |
||
448 | * @param boolean $error |
||
449 | * |
||
450 | * @return string |
||
451 | * @throws RpcException |
||
452 | */ |
||
453 | 84 | private function can($response, $error) { |
|
514 | |||
515 | /** |
||
516 | * Inject introspection and reserved RPC methods |
||
517 | * |
||
518 | * @param Methods $methods |
||
519 | */ |
||
520 | 87 | private static function setIntrospectionMethods($methods) { |
|
552 | |||
553 | /** |
||
554 | * Inject supported capabilities |
||
555 | * |
||
556 | * @param Capabilities $capabilities |
||
557 | */ |
||
558 | 87 | private static function setCapabilities($capabilities) { |
|
576 | |||
577 | /** |
||
578 | * Inject standard and RPC errors |
||
579 | * |
||
580 | * @param Errors $errors |
||
581 | */ |
||
582 | 87 | private static function setErrors($errors) { |
|
607 | |||
608 | } |
||
609 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.