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 |
||
| 5 | class MenuItem implements \JsonSerializable |
||
| 6 | { |
||
| 7 | const TYPE_POSTBACK = 'postback'; |
||
| 8 | const TYPE_WEB_URL = 'web_url'; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var string |
||
| 12 | */ |
||
| 13 | private $title; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | private $type; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Url or payload |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $data; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param string $type |
||
| 29 | * @param string $title |
||
| 30 | * @param string $data Url or payload value |
||
| 31 | */ |
||
| 32 | 14 | public function __construct($type, $title, $data) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * @return string |
||
| 48 | */ |
||
| 49 | 1 | public function getType() |
|
| 53 | |||
| 54 | /** |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | 1 | public function getTitle() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | 1 | public function getData() |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @inheritdoc |
||
| 72 | */ |
||
| 73 | 3 | View Code Duplication | public function jsonSerialize() |
| 88 | |||
| 89 | /** |
||
| 90 | * @param string $title |
||
| 91 | * |
||
| 92 | * @throws \InvalidArgumentException |
||
| 93 | */ |
||
| 94 | 14 | private function validateTitleSize($title) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param $payload |
||
| 103 | * |
||
| 104 | * @throws \InvalidArgumentException |
||
| 105 | */ |
||
| 106 | 5 | private function validatePayload($payload) |
|
| 114 | } |
||
| 115 |