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 |
||
8 | class ApiKey extends BaseClass |
||
9 | { |
||
10 | /** The URI of the action */ |
||
11 | const URI = 'https://api.signere.no/api/ApiToken'; |
||
12 | |||
13 | /** |
||
14 | * Renews the primary API key. |
||
15 | * |
||
16 | * @param string $key |
||
17 | * @return object |
||
18 | */ |
||
19 | 1 | public function renewPrimary(string $key) |
|
36 | |||
37 | /** |
||
38 | * Renews the secondary API key. |
||
39 | * |
||
40 | * @param string $key |
||
41 | * @return object |
||
42 | */ |
||
43 | 1 | public function renewSecondary(string $key) |
|
60 | |||
61 | /** |
||
62 | * Generate a new primary key and return it. |
||
63 | * |
||
64 | * @param string $providerId |
||
65 | * @param int $otpCode |
||
66 | * @return object |
||
67 | */ |
||
68 | 1 | View Code Duplication | public function createPrimary(string $providerId, int $otpCode) |
|
|||
69 | { |
||
70 | // make the URL for this request |
||
71 | 1 | $url = sprintf( |
|
72 | 1 | '%s/OTP/RenewPrimaryKeyStep2/Provider/%s/OTPCode/%s', |
|
73 | 1 | $this->getBaseUrl(), |
|
74 | 1 | $providerId, |
|
75 | 1 | $otpCode |
|
76 | ); |
||
77 | |||
78 | // get the headers for this request |
||
79 | 1 | $headers = $this->headers->make('POST', $url, [], null); |
|
80 | |||
81 | // get the response |
||
82 | 1 | $response = $this->client->post($url, [ |
|
83 | 1 | 'headers' => $headers, |
|
84 | 'json' => [], |
||
85 | ]); |
||
86 | |||
87 | // return the response |
||
88 | 1 | return $response; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Sends an OTP code in an SMS to |
||
93 | * the given mobile number. |
||
94 | * |
||
95 | * @param array $body |
||
96 | * @return object |
||
97 | */ |
||
98 | 1 | public function recoverPrimary(array $body) |
|
129 | } |
||
130 |
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.