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) |
||
144 | |||
145 | /** |
||
146 | * Called when the connection is handshaked (at low-level), and peer is ready to recv. data |
||
147 | * @return void |
||
148 | */ |
||
149 | 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() |
||
181 | |||
182 | /** |
||
183 | * Called when session finishes |
||
184 | * @return void |
||
185 | */ |
||
186 | public function onFinish() |
||
194 | |||
195 | /** |
||
196 | * Called when new data received |
||
197 | * @return void |
||
198 | */ |
||
199 | public function onRead() |
||
313 | |||
314 | /** |
||
315 | * Send authentication packet |
||
316 | * @return void |
||
317 | */ |
||
318 | protected function auth() |
||
339 | |||
340 | /** |
||
341 | * Action: Login |
||
342 | * Synopsis: Login Manager |
||
343 | * Privilege: <none> |
||
344 | * |
||
345 | * @return void |
||
346 | */ |
||
347 | protected function login() |
||
358 | |||
359 | /** |
||
360 | * Action: Challenge |
||
361 | * Synopsis: Generate Challenge for MD5 Auth |
||
362 | * Privilege: <none> |
||
363 | * |
||
364 | * @param callable $cb |
||
365 | * @return void |
||
366 | */ |
||
367 | protected function challenge($cb) |
||
377 | |||
378 | /** |
||
379 | * Action: SIPpeers |
||
380 | * Synopsis: List SIP peers (text format) |
||
381 | * Privilege: system,reporting,all |
||
382 | * Description: Lists SIP peers in text format with details on current status. |
||
383 | * Peerlist will follow as separate events, followed by a final event called |
||
384 | * PeerlistComplete. |
||
385 | * Variables: |
||
386 | * ActionID: <id> Action ID for this transaction. Will be returned. |
||
387 | * |
||
388 | * @param callable $cb Callback called when response received |
||
389 | * @callback $cb ( Connection $conn, array $packet ) |
||
390 | * @return void |
||
391 | */ |
||
392 | public function getSipPeers($cb) |
||
396 | |||
397 | /** |
||
398 | * Action: IAXpeerlist |
||
399 | * Synopsis: List IAX Peers |
||
400 | * Privilege: system,reporting,all |
||
401 | * |
||
402 | * @param callable $cb Callback called when response received |
||
403 | * @callback $cb ( Connection $conn, array $packet ) |
||
404 | * @return void |
||
405 | */ |
||
406 | public function getIaxPeers($cb) |
||
410 | |||
411 | /** |
||
412 | * Action: GetConfig |
||
413 | * Synopsis: Retrieve configuration |
||
414 | * Privilege: system,config,all |
||
415 | * Description: A 'GetConfig' action will dump the contents of a configuration |
||
416 | * file by category and contents or optionally by specified category only. |
||
417 | * Variables: (Names marked with * are required) |
||
418 | * *Filename: Configuration filename (e.g. foo.conf) |
||
419 | * Category: Category in configuration file |
||
420 | * |
||
421 | * @param string $filename Filename |
||
422 | * @param callable $cb Callback called when response received |
||
423 | * @callback $cb ( Connection $conn, array $packet ) |
||
424 | * @return void |
||
425 | */ |
||
426 | public function getConfig($filename, $cb) |
||
430 | |||
431 | /** |
||
432 | * Action: GetConfigJSON |
||
433 | * Synopsis: Retrieve configuration |
||
434 | * Privilege: system,config,all |
||
435 | * Description: A 'GetConfigJSON' action will dump the contents of a configuration |
||
436 | * file by category and contents in JSON format. This only makes sense to be used |
||
437 | * using rawman over the HTTP interface. |
||
438 | * Variables: |
||
439 | * Filename: Configuration filename (e.g. foo.conf) |
||
440 | * |
||
441 | * @param string $filename Filename |
||
442 | * @param callable $cb Callback called when response received |
||
443 | * @callback $cb ( Connection $conn, array $packet ) |
||
444 | * @return void |
||
445 | */ |
||
446 | public function getConfigJSON($filename, $cb) |
||
450 | |||
451 | /** |
||
452 | * Action: Setvar |
||
453 | * Synopsis: Set Channel Variable |
||
454 | * Privilege: call,all |
||
455 | * Description: Set a global or local channel variable. |
||
456 | * Variables: (Names marked with * are required) |
||
457 | * Channel: Channel to set variable for |
||
458 | * *Variable: Variable name |
||
459 | * *Value: Value |
||
460 | * |
||
461 | * @param string $channel |
||
462 | * @param string $variable |
||
463 | * @param string $value |
||
464 | * @param callable $cb |
||
465 | * @callback $cb ( Connection $conn, array $packet ) |
||
466 | * @return void |
||
467 | */ |
||
468 | public function setVar($channel, $variable, $value, $cb) |
||
483 | |||
484 | /** |
||
485 | * Action: CoreShowChannels |
||
486 | * Synopsis: List currently active channels |
||
487 | * Privilege: system,reporting,all |
||
488 | * Description: List currently defined channels and some information |
||
489 | * about them. |
||
490 | * Variables: |
||
491 | * ActionID: Optional Action id for message matching. |
||
492 | * |
||
493 | * @param callable $cb |
||
494 | * @callback $cb ( Connection $conn, array $packet ) |
||
495 | * @return void |
||
496 | */ |
||
497 | public function coreShowChannels($cb) |
||
505 | |||
506 | /** |
||
507 | * Action: Status |
||
508 | * Synopsis: Lists channel status |
||
509 | * Privilege: system,call,reporting,all |
||
510 | * Description: Lists channel status along with requested channel vars. |
||
511 | * Variables: (Names marked with * are required) |
||
512 | *Channel: Name of the channel to query for status |
||
513 | * Variables: Comma ',' separated list of variables to include |
||
514 | * ActionID: Optional ID for this transaction |
||
515 | * Will return the status information of each channel along with the |
||
516 | * value for the specified channel variables. |
||
517 | * |
||
518 | * @param callable $cb |
||
519 | * @param string $channel |
||
520 | * @callback $cb ( Connection $conn, array $packet ) |
||
521 | * @return void |
||
522 | */ |
||
523 | public function status($cb, $channel = null) |
||
533 | |||
534 | /** |
||
535 | * Action: Redirect |
||
536 | * Synopsis: Redirect (transfer) a call |
||
537 | * Privilege: call,all |
||
538 | * Description: Redirect (transfer) a call. |
||
539 | * Variables: (Names marked with * are required) |
||
540 | * *Channel: Channel to redirect |
||
541 | * ExtraChannel: Second call leg to transfer (optional) |
||
542 | * *Exten: Extension to transfer to |
||
543 | * *Context: Context to transfer to |
||
544 | * *Priority: Priority to transfer to |
||
545 | * ActionID: Optional Action id for message matching. |
||
546 | * |
||
547 | * @param array $params |
||
548 | * @param callable $cb Callback called when response received |
||
549 | * @callback $cb ( Connection $conn, array $packet ) |
||
550 | * @return void |
||
551 | */ |
||
552 | public function redirect(array $params, $cb) |
||
556 | |||
557 | /** |
||
558 | * Action: Originate |
||
559 | * Synopsis: Originate a call |
||
560 | * Privilege: call,all |
||
561 | * Description: first the Channel is rung. Then, when that answers, the Extension is dialled within the Context |
||
562 | * to initiate the other end of the call. |
||
563 | * Variables: (Names marked with * are required) |
||
564 | * *Channel: Channel on which to originate the call (The same as you specify in the Dial application command) |
||
565 | * *Context: Context to use on connect (must use Exten & Priority with it) |
||
566 | * *Exten: Extension to use on connect (must use Context & Priority with it) |
||
567 | * *Priority: Priority to use on connect (must use Context & Exten with it) |
||
568 | * Timeout: Timeout (in milliseconds) for the originating connection to happen(defaults to 30000 milliseconds) |
||
569 | * *CallerID: CallerID to use for the call |
||
570 | * Variable: Channels variables to set (max 32). Variables will be set for both channels (local and connected). |
||
571 | * Account: Account code for the call |
||
572 | * Application: Application to use on connect (use Data for parameters) |
||
573 | * Data : Data if Application parameter is used |
||
574 | * ActionID: Optional Action id for message matching. |
||
575 | * |
||
576 | * @param array $params |
||
577 | * @param callable $cb Callback called when response received |
||
578 | * @callback $cb ( Connection $conn, array $packet ) |
||
579 | * @return void |
||
580 | */ |
||
581 | public function originate(array $params, $cb) |
||
587 | |||
588 | /** |
||
589 | * Action: ExtensionState |
||
590 | * Synopsis: Get an extension's state. |
||
591 | * Description: function can be used to retrieve the state from any hinted extension. |
||
592 | * Variables: (Names marked with * are required) |
||
593 | * *Exten: Extension to get state |
||
594 | * Context: Context for exten |
||
595 | * ActionID: Optional Action id for message matching. |
||
596 | * |
||
597 | * @param array $params |
||
598 | * @param callable $cb Callback called when response received |
||
599 | * @callback $cb ( Connection $conn, array $packet ) |
||
600 | * @return void |
||
601 | */ |
||
602 | public function extensionState(array $params, $cb) |
||
606 | |||
607 | /** |
||
608 | * Action: Ping |
||
609 | * Description: A 'Ping' action will ellicit a 'Pong' response. Used to keep the |
||
610 | * manager connection open. |
||
611 | * Variables: NONE |
||
612 | * |
||
613 | * @param callable $cb Callback called when response received |
||
614 | * @callback $cb ( Connection $conn, array $packet ) |
||
615 | * @return void |
||
616 | */ |
||
617 | public function ping($cb) |
||
621 | |||
622 | /** |
||
623 | * For almost any actions in Action: ListCommands |
||
624 | * Privilege: depends on $action |
||
625 | * |
||
626 | * @param string $action Action |
||
627 | * @param callable $cb Callback called when response received |
||
628 | * @param array $params Parameters |
||
629 | * @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 |
||
630 | * @callback $cb ( Connection $conn, array $packet ) |
||
631 | * @return void |
||
632 | */ |
||
633 | public function action($action, $cb, array $params = null, array $assertion = null) |
||
639 | |||
640 | /** |
||
641 | * Action: Logoff |
||
642 | * Synopsis: Logoff Manager |
||
643 | * Privilege: <none> |
||
644 | * Description: Logoff this manager session |
||
645 | * Variables: NONE |
||
646 | * |
||
647 | * @param callable $cb Optional callback called when response received |
||
648 | * @callback $cb ( Connection $conn, array $packet ) |
||
649 | * @return void |
||
650 | */ |
||
651 | public function logoff($cb = null) |
||
655 | |||
656 | /** |
||
657 | * Called when event occured |
||
658 | * @deprecated Replaced with ->bind('event', ...) |
||
659 | * @param callable $cb Callback |
||
660 | * @return void |
||
661 | */ |
||
662 | public function onEvent($cb) |
||
666 | |||
667 | /** |
||
668 | * Sends arbitrary command |
||
669 | * @param string $packet A packet for sending by the connected client to Asterisk |
||
670 | * @param callable $cb Callback called when response received |
||
671 | * @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 |
||
672 | */ |
||
673 | protected function command($packet, $cb, $assertion = null) |
||
698 | |||
699 | /** |
||
700 | * Generate AMI packet string from associative array provided |
||
701 | * @param array $params |
||
702 | * @return string |
||
703 | */ |
||
704 | protected function implodeParams(array $params) |
||
714 | } |
||
715 |
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.