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 ExternalSign extends BaseClass |
||
9 | { |
||
10 | /** The URI of the action */ |
||
11 | const URI = 'https://api.signere.no/api/externalsign'; |
||
12 | |||
13 | /** |
||
14 | * Return the login information from the login. |
||
15 | * |
||
16 | * @param string $documentId |
||
17 | * @return object |
||
18 | */ |
||
19 | 1 | public function getUrlForSign(string $documentId) |
|
35 | |||
36 | /** |
||
37 | * Get the URLs to a viewerapplet showing |
||
38 | * documents in an iframe on website. |
||
39 | * |
||
40 | * @param string $documentId |
||
41 | * @param array $params |
||
42 | * @return object |
||
43 | */ |
||
44 | 1 | public function getUrlForApplet(string $documentId, array $params) |
|
45 | { |
||
46 | 1 | if (! isset($params['Domain']) || ! isset($params['Language'])) { |
|
47 | 1 | throw new BadMethodCallException('Params should contain "Domain" and "Language" keys'); |
|
48 | } |
||
49 | |||
50 | // make the URL for this request |
||
51 | 1 | $url = sprintf( |
|
52 | 1 | '%s/ViewerUrl/%s/%s/%s', |
|
53 | 1 | $this->getBaseUrl(), |
|
54 | 1 | $documentId, |
|
55 | 1 | $params['Domain'], |
|
56 | 1 | $params['Language'] |
|
57 | ); |
||
58 | |||
59 | // get the headers for this request |
||
60 | 1 | $headers = $this->headers->make('GET', $url, [], true); |
|
61 | |||
62 | // get the response |
||
63 | 1 | $response = $this->client->get($url, [ |
|
64 | 1 | 'headers' => $headers, |
|
65 | ]); |
||
66 | |||
67 | // return the response |
||
68 | 1 | return $response; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Get status of BankId mobile sign session. |
||
73 | * |
||
74 | * @param string $signeeRefId |
||
75 | * @return object |
||
76 | */ |
||
77 | 1 | public function getSessionStatus(string $signeeRefId) |
|
93 | |||
94 | /** |
||
95 | * Creates a externalsign request to integrate |
||
96 | * signing of documents in a website. |
||
97 | * |
||
98 | * @param array $body |
||
99 | * @return object |
||
100 | */ |
||
101 | 3 | View Code Duplication | public function createRequest(array $body) |
158 | |||
159 | /** |
||
160 | * Creates a app launch uri for the BankID app. |
||
161 | * |
||
162 | * @param array $body |
||
163 | * @return object |
||
164 | */ |
||
165 | 1 | View Code Duplication | public function createAppUrl(array $body) |
192 | |||
193 | /** |
||
194 | * Starts a BankID mobile sign session for the given document |
||
195 | * with given mobilenumber and date of birth. |
||
196 | * |
||
197 | * @param array $body |
||
198 | * @return object |
||
199 | */ |
||
200 | 1 | View Code Duplication | public function startMobile(array $body) |
227 | } |
||
228 |
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.