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 | ||
| 12 | class ZendeskChannel | ||
| 13 | { | ||
| 14 | /** @var HttpClient */ | ||
| 15 | protected $client; | ||
| 16 | |||
| 17 | /** @var array */ | ||
| 18 | protected $parameters; | ||
| 19 | |||
| 20 | /** @param HttpClient $client */ | ||
| 21 | 3 | public function __construct(HttpClient $client) | |
| 25 | |||
| 26 | /** | ||
| 27 | * Send the given notification. | ||
| 28 | * | ||
| 29 | * @param mixed $notifiable | ||
| 30 | * @param \Illuminate\Notifications\Notification $notification | ||
| 31 | * | ||
| 32 | * @throws \NotificationChannels\Zendesk\Exceptions\InvalidConfiguration | ||
| 33 | * @throws \NotificationChannels\Zendesk\Exceptions\CouldNotSendNotification | ||
| 34 | */ | ||
| 35 | 3 | public function send($notifiable, Notification $notification) | |
| 47 | |||
| 48 | /** | ||
| 49 | * Send update ticket request. | ||
| 50 | * | ||
| 51 | * @param int $id | ||
| 52 | */ | ||
| 53 | 1 | View Code Duplication | private function updateTicket($id) | 
| 54 |     { | ||
| 55 | 1 | $this->prepareUpdateParameters(); | |
| 56 | |||
| 57 |         try { | ||
| 58 | 1 | $response = $this->client->tickets()->update($id, $this->parameters); | |
| 59 | |||
| 60 | event(new ZendeskTicketWasUpdated($response)); | ||
| 61 | 1 |         } catch (\Exception $e) { | |
| 62 | 1 | throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage()); | |
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | /** | ||
| 67 | * Send create ticket request. | ||
| 68 | * | ||
| 69 | * @param mixed $notifiable | ||
| 70 | */ | ||
| 71 | 2 | View Code Duplication | private function createNewTicket($notifiable) | 
| 83 | |||
| 84 | /** | ||
| 85 | * Prepare the parameters before update request send. | ||
| 86 | */ | ||
| 87 | 1 | public function prepareUpdateParameters() | |
| 91 | |||
| 92 | /** | ||
| 93 | * Prepare the parameters before create request send. | ||
| 94 | * | ||
| 95 | * @param mixed $notifiable | ||
| 96 | */ | ||
| 97 | 2 | private function prepareCreateParameters($notifiable) | |
| 112 | } | ||
| 113 |