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) |
| 65 | |||
| 66 | /** |
||
| 67 | * Send create ticket request. |
||
| 68 | * |
||
| 69 | * @param mixed $notifiable |
||
| 70 | */ |
||
| 71 | 2 | View Code Duplication | private function createNewTicket($notifiable) |
| 72 | { |
||
| 73 | 2 | $this->prepareCreateParameter($notifiable); |
|
| 74 | |||
| 75 | try { |
||
| 76 | 2 | $response = $this->client->tickets()->create($this->parameters); |
|
| 77 | |||
| 78 | 2 | event(new ZendeskTicketWasCreated($response)); |
|
| 79 | 2 | } catch (\Exception $e) { |
|
| 80 | throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage()); |
||
| 81 | } |
||
| 82 | 2 | } |
|
| 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 prepareCreateParameter($notifiable) |
|
| 112 | } |
||
| 113 |