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 Form extends BaseClass |
||
8 | { |
||
9 | /** The URI of the action */ |
||
10 | const URI = 'https://api.signere.no/api/Form'; |
||
11 | |||
12 | /** |
||
13 | * Gets all forms to the authenticated documentprovider. |
||
14 | * |
||
15 | * @return object |
||
16 | */ |
||
17 | 1 | public function get() |
|
33 | |||
34 | /** |
||
35 | * Gets all signed forms to the authenticated documentprovider |
||
36 | * filtered by the input parameters provided. |
||
37 | * |
||
38 | * @param string|null $formId |
||
39 | * @param Carbon|null $from |
||
40 | * @param Carbon|null $to |
||
41 | * @return object |
||
42 | */ |
||
43 | 1 | public function getAllSigned(string $formId = null, Carbon $from = null, Carbon $to = null) |
|
44 | { |
||
45 | // make the URL for this request |
||
46 | 1 | $url = sprintf( |
|
47 | 1 | '%s/GetSignedForms?formId=%s&fromDate=%s&toDate=%s', |
|
48 | 1 | $this->getBaseUrl(), |
|
49 | 1 | $formId, |
|
50 | 1 | $from ? $from->toDateString() : null, |
|
51 | 1 | $to ? $to->toDateString() : null |
|
52 | ); |
||
53 | |||
54 | // get the headers for this request |
||
55 | 1 | $headers = $this->headers->make('GET', $url); |
|
56 | |||
57 | // get the response |
||
58 | 1 | $response = $this->client->get($url, [ |
|
59 | 1 | 'headers' => $headers, |
|
60 | ]); |
||
61 | |||
62 | // return the response |
||
63 | 1 | return $response; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Gets the attachements to the form. |
||
68 | * |
||
69 | * @param string $formId |
||
70 | * @param string $formSignId |
||
71 | * @param string $attachReference |
||
72 | * @return object |
||
73 | */ |
||
74 | 1 | public function getAttachments(string $formId, string $formSignId, string $attachReference) |
|
75 | { |
||
76 | // make the URL for this request |
||
77 | 1 | $url = sprintf( |
|
78 | 1 | '%s/GetFormAttachment?formId=%s&FormSignatureId=%s&AttatchmentReference=%s', |
|
79 | 1 | $this->getBaseUrl(), |
|
80 | 1 | $formId, |
|
81 | 1 | $formSignId, |
|
82 | 1 | $attachReference |
|
83 | ); |
||
84 | |||
85 | // get the headers for this request |
||
86 | 1 | $headers = $this->headers->make('GET', $url); |
|
87 | |||
88 | // get the response |
||
89 | 1 | $response = $this->client->get($url, [ |
|
90 | 1 | 'headers' => $headers, |
|
91 | ]); |
||
92 | |||
93 | // return the response |
||
94 | 1 | return $response; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Gets a signed form from DocumentID. |
||
99 | * |
||
100 | * @param string $documentId |
||
101 | * @return object |
||
102 | */ |
||
103 | 1 | public function getSignedByDocId(string $documentId) |
|
119 | |||
120 | /** |
||
121 | * Gets a signed form from formId and formSessionId. |
||
122 | * |
||
123 | * @param string $formId |
||
124 | * @param string $formSessionId |
||
125 | * @return object |
||
126 | */ |
||
127 | 1 | public function getSignedBySessionId(string $formId, string $formSessionId) |
|
128 | { |
||
129 | // make the URL for this request |
||
130 | 1 | $url = sprintf( |
|
131 | 1 | '%s/GetSignedFormByFormSessionId?formId=%s&formSessionId=%s', |
|
132 | 1 | $this->getBaseUrl(), |
|
133 | 1 | $formId, |
|
134 | 1 | $formSessionId |
|
135 | ); |
||
136 | |||
137 | // get the headers for this request |
||
138 | 1 | $headers = $this->headers->make('GET', $url); |
|
139 | |||
140 | // get the response |
||
141 | 1 | $response = $this->client->get($url, [ |
|
142 | 1 | 'headers' => $headers, |
|
143 | ]); |
||
144 | |||
145 | // return the response |
||
146 | 1 | return $response; |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * Enables the form. |
||
151 | * |
||
152 | * @param string $formId |
||
153 | * @return object |
||
154 | */ |
||
155 | 1 | View Code Duplication | public function enable(string $formId) |
172 | |||
173 | /** |
||
174 | * Disables the form. |
||
175 | * |
||
176 | * @param string $formId |
||
177 | * @return object |
||
178 | */ |
||
179 | 1 | View Code Duplication | public function disable(string $formId) |
196 | } |
||
197 |
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.