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 | public function create(array $body) |
|
131 | { |
||
132 | // keys that are mandatory for this request |
||
133 | $needKeys = [ |
||
134 | 1 | 'Description', |
|
135 | 'FileContent', |
||
136 | 'FileMD5CheckSum', |
||
137 | 'FileName', |
||
138 | 'Language', |
||
139 | 'SigneeRefs', |
||
140 | 'SignJobId', |
||
141 | 'Title', |
||
142 | ]; |
||
143 | |||
144 | // keys that need to be present in each signeeref |
||
145 | $needSubKeys = [ |
||
146 | 1 | 'SigneeRefId', |
|
147 | 'FirstName', |
||
148 | 'LastName', |
||
149 | 'Email', |
||
150 | ]; |
||
151 | |||
152 | // if the body doesn't have needed fields, throw an exception |
||
153 | 1 | $this->validateHasKeys($body, $needKeys); |
|
154 | |||
155 | 1 | if (! is_array($body['SigneeRefs'])) { |
|
156 | throw new UnexpectedValueException('SigneeRefs key in input should be an array'); |
||
157 | } |
||
158 | |||
159 | 1 | foreach ($body['SigneeRefs'] as $ref) { |
|
160 | 1 | if (! is_array($ref)) { |
|
161 | throw new UnexpectedValueException('Each item in SigneeRefs should be an array'); |
||
162 | } |
||
163 | |||
164 | // the SignreeRefs should have some fields |
||
165 | 1 | $this->validateHasKeys($ref, $needSubKeys); |
|
166 | } |
||
167 | |||
168 | // make the URL for this request |
||
169 | 1 | $url = $this->getBaseUrl(); |
|
170 | |||
171 | // get the headers for this request |
||
172 | 1 | $headers = $this->headers->make('POST', $url, $body, true); |
|
173 | |||
174 | // get the response |
||
175 | 1 | $response = $this->client->post($url, [ |
|
176 | 1 | 'headers' => $headers, |
|
177 | 1 | 'json' => $body, |
|
178 | ]); |
||
179 | |||
180 | // return the response |
||
181 | 1 | return $response; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Creates a new document to sign, and returns |
||
186 | * a document response object. |
||
187 | * |
||
188 | * @param array $body |
||
189 | * @return object |
||
190 | */ |
||
191 | 1 | View Code Duplication | public function cancel(array $body) |
214 | |||
215 | /** |
||
216 | * Creates a new document to sign, and returns |
||
217 | * a document response object. |
||
218 | * |
||
219 | * @param array $body |
||
220 | * @return object |
||
221 | */ |
||
222 | 1 | View Code Duplication | public function changeDeadline(array $body) |
245 | } |
||
246 |
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.