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 |
||
32 | class Connection extends ClientConnection implements CommandInterface |
||
33 | { |
||
34 | |||
35 | /** |
||
36 | * The AMQP protocol header is sent by the client before any frame-based |
||
37 | * communication takes place. It is the only data transferred that is not |
||
38 | * a frame. |
||
39 | */ |
||
40 | const PROTOCOL_HEADER = "AMQP\x00\x00\x09\x01"; |
||
41 | |||
42 | /** |
||
43 | * The maximum number of channels. |
||
44 | * |
||
45 | * AMQP channel ID is 2 bytes, but zero is reserved for connection-level |
||
46 | * communication. |
||
47 | */ |
||
48 | const MAXIMUM_CHANNELS = 0xffff - 1; |
||
49 | |||
50 | /** |
||
51 | * The maximum frame size the client supports. |
||
52 | * |
||
53 | * Note: RabbitMQ's default is 0x20000 (128 KB), our limit is higher to |
||
54 | * allow for some server-side configurability. |
||
55 | */ |
||
56 | const MAXIMUM_FRAME_SIZE = 0x80000; // 512 KB |
||
57 | |||
58 | /** |
||
59 | * The broker sends channelMax of zero in the tune frame if it does not |
||
60 | * impose a channel limit. |
||
61 | */ |
||
62 | const UNLIMITED_CHANNELS = 0; |
||
63 | |||
64 | /** |
||
65 | * The broker sends frameMax of zero in the tune frame if it does not impose |
||
66 | * a frame size limit. |
||
67 | */ |
||
68 | const UNLIMITED_FRAME_SIZE = 0; |
||
69 | |||
70 | /** |
||
71 | * The broker sends a heartbeat of zero in the tune frame if it does not use |
||
72 | * heartbeats. |
||
73 | */ |
||
74 | const HEARTBEAT_DISABLED = 0; |
||
75 | |||
76 | /** |
||
77 | * Event raised when protocol handshake ready |
||
78 | */ |
||
79 | const EVENT_ON_HANDSHAKE = 'event.amqp.connection.handshake'; |
||
80 | |||
81 | /** |
||
82 | * Event raised when connection close frame incoming |
||
83 | */ |
||
84 | |||
85 | const EVENT_ON_CONNECTION_CLOSE = 'event.amqp.connection.close'; |
||
86 | |||
87 | /** |
||
88 | * @var FrameParser |
||
89 | */ |
||
90 | protected $parser; |
||
91 | |||
92 | /** |
||
93 | * @var FrameSerializer |
||
94 | */ |
||
95 | protected $serializer; |
||
96 | |||
97 | /** |
||
98 | * @var ConnectionOptions |
||
99 | */ |
||
100 | protected $connectionOptions; |
||
101 | |||
102 | /** |
||
103 | * @var bool |
||
104 | */ |
||
105 | protected $isHandshaked = false; |
||
106 | |||
107 | /** |
||
108 | * The broker's supported features. |
||
109 | * @var Features |
||
110 | */ |
||
111 | private $features; |
||
112 | |||
113 | /** |
||
114 | * @var int |
||
115 | */ |
||
116 | private $maximumChannelCount; |
||
117 | |||
118 | /** |
||
119 | * @var int |
||
120 | */ |
||
121 | private $maximumFrameSize; |
||
122 | |||
123 | /** |
||
124 | * @var array |
||
125 | */ |
||
126 | private $channels = []; |
||
127 | |||
128 | /** |
||
129 | * @var int |
||
130 | */ |
||
131 | private $nextChannelId = 1; |
||
132 | |||
133 | /** |
||
134 | * @var bool |
||
135 | */ |
||
136 | private $debug = false; |
||
137 | |||
138 | /** |
||
139 | * |
||
140 | */ |
||
141 | protected function init() |
||
156 | |||
157 | /** |
||
158 | * |
||
159 | */ |
||
160 | public function onReady() |
||
170 | |||
171 | /** |
||
172 | * |
||
173 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
174 | * @throws \PHPDaemon\Clients\AMQP\Driver\Exception\AMQPConnectionException |
||
175 | * @throws \InvalidArgumentException |
||
176 | */ |
||
177 | protected function onRead() |
||
359 | |||
360 | /** |
||
361 | * @param int $id |
||
362 | * @param callable $callback |
||
363 | * @throws \InvalidArgumentException |
||
364 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
365 | * @throws \PHPDaemon\Clients\AMQP\Driver\Exception\AMQPConnectionException |
||
366 | */ |
||
367 | public function getChannel(callable $callback, $id = 1) |
||
378 | |||
379 | /** |
||
380 | * @param callable|null $callback |
||
381 | * @throws \InvalidArgumentException |
||
382 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
383 | * @throws \PHPDaemon\Clients\AMQP\Driver\Exception\AMQPConnectionException |
||
384 | */ |
||
385 | public function openChannel(callable $callback = null) |
||
389 | |||
390 | /** |
||
391 | * @return int |
||
392 | * @throws \PHPDaemon\Clients\AMQP\Driver\Exception\AMQPConnectionException |
||
393 | */ |
||
394 | public function findChannelId() |
||
424 | |||
425 | /** |
||
426 | * @param OutgoingFrame $frame |
||
427 | * @param callable|null $callback |
||
428 | * @return bool |
||
429 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
430 | * @throws \InvalidArgumentException |
||
431 | */ |
||
432 | public function command(OutgoingFrame $frame, callable $callback = null) |
||
440 | |||
441 | |||
442 | public function addChannel($id, Channel $channel) |
||
448 | |||
449 | /** |
||
450 | * @return Features |
||
451 | */ |
||
452 | public function getFeatures() |
||
456 | |||
457 | /** |
||
458 | * @return ConnectionOptions |
||
459 | */ |
||
460 | public function getConnectionOptions() |
||
464 | |||
465 | /** |
||
466 | * @return int |
||
467 | */ |
||
468 | public function getMaximumChannelCount() |
||
472 | |||
473 | /** |
||
474 | * @return int |
||
475 | */ |
||
476 | public function getMaximumFrameSize() |
||
480 | |||
481 | /** |
||
482 | * @return bool |
||
483 | */ |
||
484 | public function isHandshaked() |
||
488 | } |
||
489 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.