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 ConnectionStartOk extends Frame |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | private $clientProperties = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $mechanism; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $response; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $locale; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param int $channel |
||
| 40 | * @param array $clientProperties |
||
| 41 | * @param string $mechanism |
||
| 42 | * @param string $response |
||
| 43 | * @param string $locale |
||
| 44 | */ |
||
| 45 | public function __construct($channel, $clientProperties, $mechanism, $response, $locale) |
||
| 46 | { |
||
| 47 | $this->clientProperties = $clientProperties; |
||
| 48 | $this->mechanism = $mechanism; |
||
| 49 | $this->response = $response; |
||
| 50 | $this->locale = $locale; |
||
| 51 | |||
| 52 | parent::__construct($channel); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Client properties. |
||
| 57 | * |
||
| 58 | * @return array |
||
| 59 | */ |
||
| 60 | public function getClientProperties() |
||
| 61 | { |
||
| 62 | return $this->clientProperties; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Selected security mechanism. |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public function getMechanism() |
||
| 71 | { |
||
| 72 | return $this->mechanism; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Security response data. |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getResponse() |
||
| 81 | { |
||
| 82 | return $this->response; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Selected message locale. |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function getLocale() |
||
| 91 | { |
||
| 92 | return $this->locale; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function encode() |
||
| 99 | { |
||
| 100 | $data = "\x00\x0A\x00\x0B". |
||
| 101 | Value\TableValue::encode($this->clientProperties). |
||
| 102 | Value\ShortStringValue::encode($this->mechanism). |
||
| 103 | Value\LongStringValue::encode($this->response). |
||
| 104 | Value\ShortStringValue::encode($this->locale); |
||
| 105 | |||
| 106 | return "\x01".pack('nN', $this->channel, strlen($data)).$data."\xCE"; |
||
| 107 | } |
||
| 108 | } |
||
| 109 |