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 |
||
| 14 | class FacebookMessage implements \JsonSerializable |
||
| 15 | { |
||
| 16 | /** @var string Recipient's ID. */ |
||
| 17 | public $recipient; |
||
| 18 | |||
| 19 | /** @var string Notification Text. */ |
||
| 20 | public $text; |
||
| 21 | |||
| 22 | /** @var string Notification Type */ |
||
| 23 | public $notificationType = 'REGULAR'; |
||
| 24 | |||
| 25 | /** @var array Call to Action Buttons */ |
||
| 26 | public $buttons = []; |
||
| 27 | |||
| 28 | /** @var array Generic Template Cards (items) */ |
||
| 29 | public $cards = []; |
||
| 30 | |||
| 31 | /** @var string Notification Type */ |
||
| 32 | public $notification_type = NotificationType::REGULAR; |
||
| 33 | |||
| 34 | /** @var string Attachment Type |
||
| 35 | * Defaults to File |
||
| 36 | */ |
||
| 37 | public $attachment_type = AttachmentType::FILE; |
||
| 38 | |||
| 39 | /** @var string Attachment URL */ |
||
| 40 | public $attachment_url; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected $has_attachment = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected $has_text = false; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string $text |
||
| 54 | * |
||
| 55 | * @return static |
||
| 56 | */ |
||
| 57 | public static function create($text = '') |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param string $text |
||
| 64 | */ |
||
| 65 | public function __construct($text = '') |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Recipient's PSID or Phone Number. |
||
| 73 | * |
||
| 74 | * The id must be an ID that was retrieved through the |
||
| 75 | * Messenger entry points or through the Messenger webhooks. |
||
| 76 | * |
||
| 77 | * @param $recipient ID of recipient or Phone number of the recipient |
||
| 78 | * with the format +1(212)555-2368 |
||
| 79 | * |
||
| 80 | * @return $this |
||
| 81 | */ |
||
| 82 | public function to($recipient) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Notification text. |
||
| 91 | * |
||
| 92 | * @param $text |
||
| 93 | * @throws CouldNotCreateMessage |
||
| 94 | * |
||
| 95 | * @return $this |
||
| 96 | */ |
||
| 97 | public function text($text) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Add Attachment |
||
| 109 | * |
||
| 110 | * @param $attachment_type |
||
| 111 | * @param $url |
||
| 112 | * @throws CouldNotCreateMessage |
||
| 113 | * |
||
| 114 | * @return $this |
||
| 115 | */ |
||
| 116 | public function attach($attachment_type, $url) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Push notification type. |
||
| 135 | * |
||
| 136 | * @param string $notificationType Possible values: REGULAR, SILENT_PUSH, NO_PUSH |
||
| 137 | * |
||
| 138 | * @return $this |
||
| 139 | */ |
||
| 140 | public function notificationType($notificationType = 'REGULAR') |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Add up to 3 call to action buttons. |
||
| 148 | * |
||
| 149 | * @param array $buttons |
||
|
|
|||
| 150 | * |
||
| 151 | * @return $this |
||
| 152 | * @throws CouldNotSendNotification |
||
| 153 | */ |
||
| 154 | public function cards(array $cards = []) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Add up to 3 call to action buttons. |
||
| 165 | * |
||
| 166 | * @param array $buttons |
||
| 167 | * |
||
| 168 | * @return $this |
||
| 169 | * @throws CouldNotSendNotification |
||
| 170 | */ |
||
| 171 | View Code Duplication | public function buttons(array $buttons = []) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Determine if user id is not given. |
||
| 182 | * |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public function toNotGiven() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Convert the object into something JSON serializable. |
||
| 192 | * |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | public function jsonSerialize() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Returns message payload for JSON conversion |
||
| 202 | * @throws CouldNotCreateMessage |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | public function toArray() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns message for simple text message |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | protected function textMessageToArray() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Returns message for attachment message |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | protected function attachmentMessageToArray() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Returns message for Generic Template message |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | protected function genericMessageToArray() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Returns message for Button Template message |
||
| 266 | * @return array |
||
| 267 | */ |
||
| 268 | protected function buttonMessageToArray() |
||
| 279 | } |
||
| 280 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.