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 |
||
5 | class Document extends BaseClass |
||
6 | { |
||
7 | /** The URI of the action */ |
||
8 | const URI = 'https://api.signere.no/api/Document'; |
||
9 | |||
10 | /** |
||
11 | * Returns the url to sign the document for the given Signeeref |
||
12 | * or the first Signeeref if not SigneerefId is specified. |
||
13 | * |
||
14 | * @param string $documentId |
||
15 | * @param string|null $signeeRefId |
||
16 | * @return object |
||
17 | */ |
||
18 | 1 | public function getSignUrl(string $documentId, string $signeeRefId = null) |
|
19 | { |
||
20 | // make the URL for this request |
||
21 | 1 | $url = sprintf( |
|
22 | 1 | '%s/SignUrl?documentId=%s&signeeRefId=%s', |
|
23 | 1 | $this->getBaseUrl(), |
|
24 | 1 | $documentId, |
|
25 | 1 | $signeeRefId |
|
26 | ); |
||
27 | |||
28 | // get the headers for this request |
||
29 | 1 | $headers = $this->headers->make('GET', $url); |
|
30 | |||
31 | // get the response |
||
32 | 1 | $response = $this->client->get($url, [ |
|
33 | 1 | 'headers' => $headers, |
|
34 | ]); |
||
35 | |||
36 | // return the response |
||
37 | 1 | return $response; |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * Retrieves the document with the given ID. |
||
42 | * |
||
43 | * @param string $documentId |
||
44 | * @return object |
||
45 | */ |
||
46 | 1 | public function get(string $documentId) |
|
62 | |||
63 | /** |
||
64 | * Returns a temporary URL for viewing a signed |
||
65 | * document in the BankID applet. |
||
66 | * |
||
67 | * @param string $documentId |
||
68 | * @return object |
||
69 | */ |
||
70 | 1 | public function getTemporaryUrl(string $documentId) |
|
90 | |||
91 | /** |
||
92 | * Retrieves a list of documents based on the given parameters. |
||
93 | * |
||
94 | * @param string|null $jobId |
||
95 | * @param array $params |
||
96 | * @return object |
||
97 | */ |
||
98 | 1 | public function getList(string $jobId = null, array $params = []) |
|
122 | |||
123 | /** |
||
124 | * Creates a new document to sign, and returns |
||
125 | * a document response object. |
||
126 | * |
||
127 | * @param array $body |
||
128 | * @return object |
||
129 | */ |
||
130 | 1 | View Code Duplication | public function create(array $body) |
186 | |||
187 | /** |
||
188 | * Creates a new document to sign, and returns |
||
189 | * a document response object. |
||
190 | * |
||
191 | * @param array $body |
||
192 | * @return object |
||
193 | */ |
||
194 | 1 | public function cancel(array $body) |
|
221 | |||
222 | /** |
||
223 | * Creates a new document to sign, and returns |
||
224 | * a document response object. |
||
225 | * |
||
226 | * @param array $body |
||
227 | * @return object |
||
228 | */ |
||
229 | 1 | public function changeDeadline(array $body) |
|
256 | } |
||
257 |
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.