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 |
||
16 | class Send extends AbstractApi implements SendInterface |
||
17 | { |
||
18 | use ValidatorTrait; |
||
19 | |||
20 | /** |
||
21 | * @param string|array $recipient |
||
22 | * @param mixed $message |
||
23 | * |
||
24 | * @throws \Exception |
||
25 | */ |
||
26 | 11 | View Code Duplication | public function message($recipient, $message, array $options = []): SendResponse |
37 | |||
38 | /** |
||
39 | * @param string|array $recipient |
||
40 | * |
||
41 | * @throws \Kerox\Messenger\Exception\MessengerException |
||
42 | */ |
||
43 | 2 | View Code Duplication | public function action($recipient, string $action, array $options = []): SendResponse |
54 | |||
55 | /** |
||
56 | * @throws \Exception |
||
57 | */ |
||
58 | 1 | View Code Duplication | public function attachment(AbstractAttachment $attachment): SendResponse |
67 | |||
68 | /** |
||
69 | * @param mixed $recipient |
||
70 | * |
||
71 | * @throws \Kerox\Messenger\Exception\InvalidRecipientException |
||
72 | */ |
||
73 | 13 | private function isValidRecipient($recipient): void |
|
79 | |||
80 | /** |
||
81 | * @param mixed $message |
||
82 | * |
||
83 | * @throws \Kerox\Messenger\Exception\MessengerException |
||
84 | */ |
||
85 | 11 | private function isValidOptions(array $options, $message = null): void |
|
102 | |||
103 | /** |
||
104 | * @throws \Kerox\Messenger\Exception\MessengerException |
||
105 | */ |
||
106 | 2 | private function isValidMessagingType(string $messagingType): void |
|
113 | |||
114 | 11 | private function getAllowedOptionsKeys(): array |
|
123 | |||
124 | 2 | public function getAllowedMessagingType(): array |
|
133 | } |
||
134 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.