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 ThreemaGateway_Handler_Action_GatewayServer extends ThreemaGateway_Handler_Action_Abstract |
||
13 | { |
||
14 | /** |
||
15 | * Returns the Threema ID associated to a phone number. |
||
16 | * |
||
17 | * In case of an error this does not throw an exception, but just returns false. |
||
18 | * |
||
19 | * @param string $phone Phone number (the best way is in international E.164 |
||
20 | * format without `+`, e.g. 41791234567) |
||
21 | * @throws XenForo_Exception |
||
22 | * @return string|false |
||
23 | */ |
||
24 | public function lookupPhone($phone) |
||
49 | |||
50 | /** |
||
51 | * Returns the Threema ID associated to a mail address. |
||
52 | * |
||
53 | * In case of an error this does not throw an exception, but just returns false. |
||
54 | * |
||
55 | * @param string $mail E-mail |
||
56 | * @throws XenForo_Exception |
||
57 | * @return string|false |
||
58 | */ |
||
59 | View Code Duplication | public function lookupMail($mail) |
|
77 | |||
78 | /** |
||
79 | * Returns the capabilities of a Threema ID. |
||
80 | * |
||
81 | * In case of an error this does not throw an exception, but just returns false. |
||
82 | * |
||
83 | * @param string $threemaId |
||
84 | * @return Threema\MsgApi\Commands\Results\CapabilityResult |
||
85 | */ |
||
86 | View Code Duplication | public function getCapabilities($threemaId) |
|
104 | |||
105 | /** |
||
106 | * Returns the remaining credits of the Gateway account. |
||
107 | * |
||
108 | * @throws XenForo_Exception |
||
109 | * @return int |
||
110 | */ |
||
111 | public function getCredits() |
||
127 | |||
128 | /** |
||
129 | * Fetches the public key of an ID from the Threema server. |
||
130 | * |
||
131 | * @param string $threemaId The id whose public key should be fetched |
||
132 | * |
||
133 | * @throws XenForo_Exception |
||
134 | * @return string |
||
135 | */ |
||
136 | public function fetchPublicKey($threemaId) |
||
156 | } |
||
157 |
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.