Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Connection 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 Connection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Connection extends ClientConnection { |
||
16 | |||
17 | /** |
||
18 | * @TODO DESCR |
||
|
|||
19 | */ |
||
20 | const CONN_STATE_START = 0; |
||
21 | |||
22 | /** |
||
23 | * @TODO DESCR |
||
24 | */ |
||
25 | const CONN_STATE_GOT_INITIAL_PACKET = 0.1; |
||
26 | |||
27 | /** |
||
28 | * @TODO DESCR |
||
29 | */ |
||
30 | const CONN_STATE_AUTH = 1; |
||
31 | |||
32 | /** |
||
33 | * @TODO DESCR |
||
34 | */ |
||
35 | const CONN_STATE_LOGIN_PACKET_SENT = 1.1; |
||
36 | |||
37 | /** |
||
38 | * @TODO DESCR |
||
39 | */ |
||
40 | const CONN_STATE_CHALLENGE_PACKET_SENT = 1.2; |
||
41 | |||
42 | /** |
||
43 | * @TODO DESCR |
||
44 | */ |
||
45 | const CONN_STATE_LOGIN_PACKET_SENT_AFTER_CHALLENGE = 1.3; |
||
46 | |||
47 | /** |
||
48 | * @TODO DESCR |
||
49 | */ |
||
50 | const CONN_STATE_HANDSHAKED_OK = 2.1; |
||
51 | |||
52 | /** |
||
53 | * @TODO DESCR |
||
54 | */ |
||
55 | const CONN_STATE_HANDSHAKED_ERROR = 2.2; |
||
56 | |||
57 | /** |
||
58 | * @TODO DESCR |
||
59 | */ |
||
60 | const INPUT_STATE_START = 0; |
||
61 | |||
62 | /** |
||
63 | * @TODO DESCR |
||
64 | */ |
||
65 | const INPUT_STATE_END_OF_PACKET = 1; |
||
66 | |||
67 | /** |
||
68 | * @TODO DESCR |
||
69 | */ |
||
70 | const INPUT_STATE_PROCESSING = 2; |
||
71 | |||
72 | /** |
||
73 | * @var string EOL |
||
74 | */ |
||
75 | public $EOL = "\r\n"; |
||
76 | |||
77 | /** |
||
78 | * @var string The username to access the interface |
||
79 | */ |
||
80 | public $username; |
||
81 | |||
82 | /** |
||
83 | * @var string The password defined in manager interface of server |
||
84 | */ |
||
85 | public $secret; |
||
86 | |||
87 | /** |
||
88 | * @var float Connection's state |
||
89 | */ |
||
90 | public $state = self::CONN_STATE_START; |
||
91 | |||
92 | /** |
||
93 | * @var integer Input state |
||
94 | */ |
||
95 | public $instate = self::INPUT_STATE_START; |
||
96 | |||
97 | /** |
||
98 | * @var array Received packets |
||
99 | */ |
||
100 | public $packets = []; |
||
101 | |||
102 | /** |
||
103 | * @var integer For composite response on action |
||
104 | */ |
||
105 | public $cnt = 0; |
||
106 | |||
107 | /** |
||
108 | * @var array Stack of callbacks called when response received |
||
109 | */ |
||
110 | public $callbacks = []; |
||
111 | |||
112 | /** |
||
113 | * Assertions for callbacks. |
||
114 | * Assertion: if more events may follow as response this is a main part or full |
||
115 | * an action complete event indicating that all data has been sent |
||
116 | * @var array |
||
117 | */ |
||
118 | public $assertions = []; |
||
119 | |||
120 | /** |
||
121 | * @var callable Callback. Called when received response on challenge action |
||
122 | */ |
||
123 | public $onChallenge; |
||
124 | |||
125 | /** |
||
126 | * Execute the given callback when/if the connection is handshaked |
||
127 | * @param callable Callback |
||
128 | * @return void |
||
129 | */ |
||
130 | public function onConnected($cb) { |
||
131 | View Code Duplication | if ($this->state === self::CONN_STATE_HANDSHAKED_ERROR) { |
|
132 | $cb($this); |
||
133 | } |
||
134 | elseif ($this->state === self::CONN_STATE_HANDSHAKED_OK) { |
||
135 | $cb($this); |
||
136 | } |
||
137 | else { |
||
138 | if (!$this->onConnected) { |
||
139 | $this->onConnected = new StackCallbacks(); |
||
140 | } |
||
141 | |||
142 | $this->onConnected->push($cb); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Called when the connection is handshaked (at low-level), and peer is ready to recv. data |
||
148 | * @return void |
||
149 | */ |
||
150 | public function onReady() { |
||
164 | |||
165 | /** |
||
166 | * Called when the worker is going to shutdown |
||
167 | * @return boolean Ready to shutdown? |
||
168 | */ |
||
169 | public function gracefulShutdown() { |
||
180 | |||
181 | /** |
||
182 | * Called when session finishes |
||
183 | * @return void |
||
184 | */ |
||
185 | public function onFinish() { |
||
192 | |||
193 | /** |
||
194 | * Called when new data received |
||
195 | * @return void |
||
196 | */ |
||
197 | public function onRead() { |
||
302 | |||
303 | /** |
||
304 | * Send authentication packet |
||
305 | * @return void |
||
306 | */ |
||
307 | protected function auth() { |
||
328 | |||
329 | /** |
||
330 | * Action: Login |
||
331 | * Synopsis: Login Manager |
||
332 | * Privilege: <none> |
||
333 | * |
||
334 | * @return void |
||
335 | */ |
||
336 | protected function login() { |
||
346 | |||
347 | /** |
||
348 | * Action: Challenge |
||
349 | * Synopsis: Generate Challenge for MD5 Auth |
||
350 | * Privilege: <none> |
||
351 | * |
||
352 | * @param callable $cb |
||
353 | * @return void |
||
354 | */ |
||
355 | protected function challenge($cb) { |
||
363 | |||
364 | /** |
||
365 | * Action: SIPpeers |
||
366 | * Synopsis: List SIP peers (text format) |
||
367 | * Privilege: system,reporting,all |
||
368 | * Description: Lists SIP peers in text format with details on current status. |
||
369 | * Peerlist will follow as separate events, followed by a final event called |
||
370 | * PeerlistComplete. |
||
371 | * Variables: |
||
372 | * ActionID: <id> Action ID for this transaction. Will be returned. |
||
373 | * |
||
374 | * @param callable $cb Callback called when response received |
||
375 | * @callback $cb ( Connection $conn, array $packet ) |
||
376 | * @return void |
||
377 | */ |
||
378 | public function getSipPeers($cb) { |
||
381 | |||
382 | /** |
||
383 | * Action: IAXpeerlist |
||
384 | * Synopsis: List IAX Peers |
||
385 | * Privilege: system,reporting,all |
||
386 | * |
||
387 | * @param callable $cb Callback called when response received |
||
388 | * @callback $cb ( Connection $conn, array $packet ) |
||
389 | * @return void |
||
390 | */ |
||
391 | public function getIaxPeers($cb) { |
||
394 | |||
395 | /** |
||
396 | * Action: GetConfig |
||
397 | * Synopsis: Retrieve configuration |
||
398 | * Privilege: system,config,all |
||
399 | * Description: A 'GetConfig' action will dump the contents of a configuration |
||
400 | * file by category and contents or optionally by specified category only. |
||
401 | * Variables: (Names marked with * are required) |
||
402 | * *Filename: Configuration filename (e.g. foo.conf) |
||
403 | * Category: Category in configuration file |
||
404 | * |
||
405 | * @param string $filename Filename |
||
406 | * @param callable $cb Callback called when response received |
||
407 | * @callback $cb ( Connection $conn, array $packet ) |
||
408 | * @return void |
||
409 | */ |
||
410 | public function getConfig($filename, $cb) { |
||
413 | |||
414 | /** |
||
415 | * Action: GetConfigJSON |
||
416 | * Synopsis: Retrieve configuration |
||
417 | * Privilege: system,config,all |
||
418 | * Description: A 'GetConfigJSON' action will dump the contents of a configuration |
||
419 | * file by category and contents in JSON format. This only makes sense to be used |
||
420 | * using rawman over the HTTP interface. |
||
421 | * Variables: |
||
422 | * Filename: Configuration filename (e.g. foo.conf) |
||
423 | * |
||
424 | * @param string $filename Filename |
||
425 | * @param callable $cb Callback called when response received |
||
426 | * @callback $cb ( Connection $conn, array $packet ) |
||
427 | * @return void |
||
428 | */ |
||
429 | public function getConfigJSON($filename, $cb) { |
||
432 | |||
433 | /** |
||
434 | * Action: Setvar |
||
435 | * Synopsis: Set Channel Variable |
||
436 | * Privilege: call,all |
||
437 | * Description: Set a global or local channel variable. |
||
438 | * Variables: (Names marked with * are required) |
||
439 | * Channel: Channel to set variable for |
||
440 | * *Variable: Variable name |
||
441 | * *Value: Value |
||
442 | * |
||
443 | * @param string $channel |
||
444 | * @param string $variable |
||
445 | * @param string $value |
||
446 | * @param callable $cb |
||
447 | * @callback $cb ( Connection $conn, array $packet ) |
||
448 | * @return void |
||
449 | */ |
||
450 | public function setVar($channel, $variable, $value, $cb) { |
||
464 | |||
465 | /** |
||
466 | * Action: CoreShowChannels |
||
467 | * Synopsis: List currently active channels |
||
468 | * Privilege: system,reporting,all |
||
469 | * Description: List currently defined channels and some information |
||
470 | * about them. |
||
471 | * Variables: |
||
472 | * ActionID: Optional Action id for message matching. |
||
473 | * |
||
474 | * @param callable $cb |
||
475 | * @callback $cb ( Connection $conn, array $packet ) |
||
476 | * @return void |
||
477 | */ |
||
478 | public function coreShowChannels($cb) { |
||
483 | |||
484 | /** |
||
485 | * Action: Status |
||
486 | * Synopsis: Lists channel status |
||
487 | * Privilege: system,call,reporting,all |
||
488 | * Description: Lists channel status along with requested channel vars. |
||
489 | * Variables: (Names marked with * are required) |
||
490 | *Channel: Name of the channel to query for status |
||
491 | * Variables: Comma ',' separated list of variables to include |
||
492 | * ActionID: Optional ID for this transaction |
||
493 | * Will return the status information of each channel along with the |
||
494 | * value for the specified channel variables. |
||
495 | * |
||
496 | * @param callable $cb |
||
497 | * @param string $channel |
||
498 | * @callback $cb ( Connection $conn, array $packet ) |
||
499 | * @return void |
||
500 | */ |
||
501 | public function status($cb, $channel = null) { |
||
510 | |||
511 | /** |
||
512 | * Action: Redirect |
||
513 | * Synopsis: Redirect (transfer) a call |
||
514 | * Privilege: call,all |
||
515 | * Description: Redirect (transfer) a call. |
||
516 | * Variables: (Names marked with * are required) |
||
517 | * *Channel: Channel to redirect |
||
518 | * ExtraChannel: Second call leg to transfer (optional) |
||
519 | * *Exten: Extension to transfer to |
||
520 | * *Context: Context to transfer to |
||
521 | * *Priority: Priority to transfer to |
||
522 | * ActionID: Optional Action id for message matching. |
||
523 | * |
||
524 | * @param array $params |
||
525 | * @param callable $cb Callback called when response received |
||
526 | * @callback $cb ( Connection $conn, array $packet ) |
||
527 | * @return void |
||
528 | */ |
||
529 | public function redirect(array $params, $cb) { |
||
532 | |||
533 | /** |
||
534 | * Action: Originate |
||
535 | * Synopsis: Originate a call |
||
536 | * Privilege: call,all |
||
537 | * Description: first the Channel is rung. Then, when that answers, the Extension is dialled within the Context |
||
538 | * to initiate the other end of the call. |
||
539 | * Variables: (Names marked with * are required) |
||
540 | * *Channel: Channel on which to originate the call (The same as you specify in the Dial application command) |
||
541 | * *Context: Context to use on connect (must use Exten & Priority with it) |
||
542 | * *Exten: Extension to use on connect (must use Context & Priority with it) |
||
543 | * *Priority: Priority to use on connect (must use Context & Exten with it) |
||
544 | * Timeout: Timeout (in milliseconds) for the originating connection to happen(defaults to 30000 milliseconds) |
||
545 | * *CallerID: CallerID to use for the call |
||
546 | * Variable: Channels variables to set (max 32). Variables will be set for both channels (local and connected). |
||
547 | * Account: Account code for the call |
||
548 | * Application: Application to use on connect (use Data for parameters) |
||
549 | * Data : Data if Application parameter is used |
||
550 | * ActionID: Optional Action id for message matching. |
||
551 | * |
||
552 | * @param array $params |
||
553 | * @param callable $cb Callback called when response received |
||
554 | * @callback $cb ( Connection $conn, array $packet ) |
||
555 | * @return void |
||
556 | */ |
||
557 | public function originate(array $params, $cb) { |
||
562 | |||
563 | /** |
||
564 | * Action: ExtensionState |
||
565 | * Synopsis: Get an extension's state. |
||
566 | * Description: function can be used to retrieve the state from any hinted extension. |
||
567 | * Variables: (Names marked with * are required) |
||
568 | * *Exten: Extension to get state |
||
569 | * Context: Context for exten |
||
570 | * ActionID: Optional Action id for message matching. |
||
571 | * |
||
572 | * @param array $params |
||
573 | * @param callable $cb Callback called when response received |
||
574 | * @callback $cb ( Connection $conn, array $packet ) |
||
575 | * @return void |
||
576 | */ |
||
577 | public function extensionState(array $params, $cb) { |
||
580 | |||
581 | /** |
||
582 | * Action: Ping |
||
583 | * Description: A 'Ping' action will ellicit a 'Pong' response. Used to keep the |
||
584 | * manager connection open. |
||
585 | * Variables: NONE |
||
586 | * |
||
587 | * @param callable $cb Callback called when response received |
||
588 | * @callback $cb ( Connection $conn, array $packet ) |
||
589 | * @return void |
||
590 | */ |
||
591 | public function ping($cb) { |
||
594 | |||
595 | /** |
||
596 | * For almost any actions in Action: ListCommands |
||
597 | * Privilege: depends on $action |
||
598 | * |
||
599 | * @param string $action Action |
||
600 | * @param callable $cb Callback called when response received |
||
601 | * @param array $params Parameters |
||
602 | * @param array $assertion If more events may follow as response this is a main part or full an action complete event indicating that all data has been sent |
||
603 | * @callback $cb ( Connection $conn, array $packet ) |
||
604 | * @return void |
||
605 | */ |
||
606 | public function action($action, $cb, array $params = null, array $assertion = null) { |
||
611 | |||
612 | /** |
||
613 | * Action: Logoff |
||
614 | * Synopsis: Logoff Manager |
||
615 | * Privilege: <none> |
||
616 | * Description: Logoff this manager session |
||
617 | * Variables: NONE |
||
618 | * |
||
619 | * @param callable $cb Optional callback called when response received |
||
620 | * @callback $cb ( Connection $conn, array $packet ) |
||
621 | * @return void |
||
622 | */ |
||
623 | public function logoff($cb = null) { |
||
626 | |||
627 | /** |
||
628 | * Called when event occured |
||
629 | * @deprecated Replaced with ->bind('event', ...) |
||
630 | * @param callable $cb Callback |
||
631 | * @return void |
||
632 | */ |
||
633 | public function onEvent($cb) { |
||
636 | |||
637 | /** |
||
638 | * Sends arbitrary command |
||
639 | * @param string $packet A packet for sending by the connected client to Asterisk |
||
640 | * @param callable $cb Callback called when response received |
||
641 | * @param array $assertion If more events may follow as response this is a main part or full an action complete event indicating that all data has been sent |
||
642 | */ |
||
643 | protected function command($packet, $cb, $assertion = null) { |
||
667 | |||
668 | /** |
||
669 | * Generate AMI packet string from associative array provided |
||
670 | * @param array $params |
||
671 | * @return string |
||
672 | */ |
||
673 | protected function implodeParams(array $params) { |
||
682 | } |
||
683 |
This check looks
TODO
comments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.