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 |
||
7 | class DocumentProvider extends BaseClass |
||
8 | { |
||
9 | /** The URI of the action */ |
||
10 | const URI = 'https://api.signere.no/api/DocumentProvider'; |
||
11 | |||
12 | /** |
||
13 | * Retrieves a document provider account. |
||
14 | * |
||
15 | * @param string $providerId |
||
16 | * @return object |
||
17 | */ |
||
18 | 1 | public function getProviderAccount(string $providerId) |
|
34 | |||
35 | /** |
||
36 | * Gets the expires date for your BankID certificate. If you don't |
||
37 | * have your own BankID certificate it will return Bad request. |
||
38 | * |
||
39 | * @return object |
||
40 | */ |
||
41 | 1 | public function getCertExpiry() |
|
57 | |||
58 | /** |
||
59 | * Get the usage when using prepaid or demo account. |
||
60 | * |
||
61 | * @param string $providerId |
||
62 | * @param bool|bool $demo |
||
63 | * @return object |
||
64 | */ |
||
65 | 2 | View Code Duplication | public function getUsage(string $providerId, bool $demo = false) |
|
|||
66 | { |
||
67 | // make the URL for this request |
||
68 | 2 | $url = sprintf( |
|
69 | 2 | '%s/quota/%s?ProviderId=%s', |
|
70 | 2 | $this->getBaseUrl(), |
|
71 | 2 | $demo ? 'demo' : 'prepaid', |
|
72 | 2 | $providerId |
|
73 | ); |
||
74 | |||
75 | // get the headers for this request |
||
76 | 2 | $headers = $this->headers->make('GET', $url); |
|
77 | |||
78 | // get the response |
||
79 | 2 | $response = $this->client->get($url, [ |
|
80 | 2 | 'headers' => $headers, |
|
81 | ]); |
||
82 | |||
83 | // return the response |
||
84 | 2 | return $response; |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * Creates a new document provider. |
||
89 | * |
||
90 | * @param array $body |
||
91 | * @return object |
||
92 | */ |
||
93 | 1 | public function create(array $body) |
|
135 | |||
136 | /** |
||
137 | * Updates a new document provider. |
||
138 | * |
||
139 | * @param array $body |
||
140 | * @return object |
||
141 | */ |
||
142 | 1 | public function update(array $body) |
|
172 | } |
||
173 |
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.