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 Message |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @var string |
||
| 9 | */ |
||
| 10 | private $id; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var int |
||
| 14 | */ |
||
| 15 | private $sequence; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var null|string |
||
| 19 | */ |
||
| 20 | private $text; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | private $attachments; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var null|string |
||
| 29 | */ |
||
| 30 | private $quickReply; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param string $id |
||
| 34 | * @param int $sequence |
||
| 35 | * @param null|string $text |
||
| 36 | * @param array $attachments |
||
| 37 | * @param null|string $quickReply |
||
| 38 | */ |
||
| 39 | 20 | public function __construct($id, $sequence, $text = null, array $attachments = [], $quickReply = null) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | 1 | public function getId() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @return int |
||
| 58 | */ |
||
| 59 | 1 | public function getSequence() |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @return null|string |
||
| 66 | */ |
||
| 67 | 2 | public function getText() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | 3 | public function getAttachments() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @return null|string |
||
| 82 | */ |
||
| 83 | 3 | public function getQuickReply() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @return bool |
||
| 90 | */ |
||
| 91 | 1 | public function hasText() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | 1 | public function hasAttachments() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | 2 | public function hasQuickReply() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | public function hasLocation() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return string|null |
||
| 122 | */ |
||
| 123 | public function getLatitude() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return string|null |
||
| 130 | */ |
||
| 131 | public function getLongitude() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param array $payload |
||
| 138 | * |
||
| 139 | * @return static |
||
| 140 | */ |
||
| 141 | 4 | View Code Duplication | public static function create(array $payload) |
| 149 | } |
||
| 150 |