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 ConnectionStart extends Frame |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | private $versionMajor; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var int |
||
| 25 | */ |
||
| 26 | private $versionMinor; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $serverProperties = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $mechanisms; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $locales; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param int $channel |
||
| 45 | * @param int $versionMajor |
||
| 46 | * @param int $versionMinor |
||
| 47 | * @param array $serverProperties |
||
| 48 | * @param string $mechanisms |
||
| 49 | * @param string $locales |
||
| 50 | */ |
||
| 51 | public function __construct($channel, $versionMajor, $versionMinor, $serverProperties, $mechanisms, $locales) |
||
| 52 | { |
||
| 53 | $this->versionMajor = $versionMajor; |
||
| 54 | $this->versionMinor = $versionMinor; |
||
| 55 | $this->serverProperties = $serverProperties; |
||
| 56 | $this->mechanisms = $mechanisms; |
||
| 57 | $this->locales = $locales; |
||
| 58 | |||
| 59 | parent::__construct($channel); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Protocol major version. |
||
| 64 | * |
||
| 65 | * @return int |
||
| 66 | */ |
||
| 67 | public function getVersionMajor() |
||
| 68 | { |
||
| 69 | return $this->versionMajor; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Protocol minor version. |
||
| 74 | * |
||
| 75 | * @return int |
||
| 76 | */ |
||
| 77 | public function getVersionMinor() |
||
| 78 | { |
||
| 79 | return $this->versionMinor; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Server properties. |
||
| 84 | * |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | public function getServerProperties() |
||
| 88 | { |
||
| 89 | return $this->serverProperties; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Available security mechanisms. |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getMechanisms() |
||
| 98 | { |
||
| 99 | return $this->mechanisms; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Available message locales. |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function getLocales() |
||
| 108 | { |
||
| 109 | return $this->locales; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | public function encode() |
||
| 116 | { |
||
| 117 | $data = "\x00\x0A\x00\x0A". |
||
| 118 | Value\OctetValue::encode($this->versionMajor). |
||
| 119 | Value\OctetValue::encode($this->versionMinor). |
||
| 120 | Value\TableValue::encode($this->serverProperties). |
||
| 121 | Value\LongStringValue::encode($this->mechanisms). |
||
| 122 | Value\LongStringValue::encode($this->locales); |
||
| 123 | |||
| 124 | return "\x01".pack('nN', $this->channel, strlen($data)).$data."\xCE"; |
||
| 125 | } |
||
| 126 | } |
||
| 127 |