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:
| 1 | <?php |
||
| 16 | class BasicReturn extends Frame |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | private $replyCode; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $replyText; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $exchange; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $routingKey; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param int $channel |
||
| 40 | * @param int $replyCode |
||
| 41 | * @param string $replyText |
||
| 42 | * @param string $exchange |
||
| 43 | * @param string $routingKey |
||
| 44 | */ |
||
| 45 | public function __construct($channel, $replyCode, $replyText, $exchange, $routingKey) |
||
| 46 | { |
||
| 47 | $this->replyCode = $replyCode; |
||
| 48 | $this->replyText = $replyText; |
||
| 49 | $this->exchange = $exchange; |
||
| 50 | $this->routingKey = $routingKey; |
||
| 51 | |||
| 52 | parent::__construct($channel); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * ReplyCode. |
||
| 57 | * |
||
| 58 | * @return int |
||
| 59 | */ |
||
| 60 | public function getReplyCode() |
||
| 61 | { |
||
| 62 | return $this->replyCode; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * ReplyText. |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public function getReplyText() |
||
| 71 | { |
||
| 72 | return $this->replyText; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Exchange. |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getExchange() |
||
| 81 | { |
||
| 82 | return $this->exchange; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Message routing key. |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function getRoutingKey() |
||
| 91 | { |
||
| 92 | return $this->routingKey; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function encode() |
||
| 99 | { |
||
| 100 | $data = "\x00\x3C\x00\x32". |
||
| 101 | Value\ShortValue::encode($this->replyCode). |
||
| 102 | Value\ShortStringValue::encode($this->replyText). |
||
| 103 | Value\ShortStringValue::encode($this->exchange). |
||
| 104 | Value\ShortStringValue::encode($this->routingKey); |
||
| 105 | |||
| 106 | return "\x01".pack('nN', $this->channel, strlen($data)).$data."\xCE"; |
||
| 107 | } |
||
| 108 | } |
||
| 109 |