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 |
||
27 | class WebApiContext extends ApiClientContext implements ApiClientContextInterface |
||
28 | { |
||
29 | /** |
||
30 | * Adds Basic Authentication header to next request. |
||
31 | * |
||
32 | * @param string $username |
||
33 | * @param string $password |
||
34 | * |
||
35 | * @Given /^I am basic authenticating as "([^"]*)" with "([^"]*)" password$/ |
||
36 | */ |
||
37 | public function iAmBasicAuthenticatingAs($username, $password) |
||
44 | |||
45 | /** |
||
46 | * Adds Basic Authentication header to next request. |
||
47 | * |
||
48 | * @param string $username |
||
49 | * @param string $password |
||
50 | * |
||
51 | * @Given /^I am authenticating as "([^"]*)" with "([^"]*)" password$/ |
||
52 | * |
||
53 | * @deprecated |
||
54 | */ |
||
55 | public function iAmAuthenticatingAs($username, $password) |
||
59 | |||
60 | /** |
||
61 | * Sets a HTTP Header. |
||
62 | * |
||
63 | * @param string $name header name |
||
64 | * @param string $value header value |
||
65 | * |
||
66 | * @Given /^I set header "([^"]*)" with value "([^"]*)"$/ |
||
67 | */ |
||
68 | public function iSetHeaderWithValue($name, $value) |
||
72 | |||
73 | /** |
||
74 | * Sends HTTP request to specific relative URL. |
||
75 | * |
||
76 | * @param string $method request method |
||
77 | * @param string $url relative url |
||
78 | * |
||
79 | * @throws GuzzleException |
||
80 | * |
||
81 | * @When /^(?:I )?send a ([A-Z]+) request to "([^"]+)"$/ |
||
82 | */ |
||
83 | public function iSendARequest($method, $url) |
||
89 | |||
90 | /** |
||
91 | * Sends HTTP request to specific URL with field values from Table. |
||
92 | * |
||
93 | * @param string $method request method |
||
94 | * @param string $url relative url |
||
95 | * @param TableNode $values table of post values |
||
96 | * |
||
97 | * @throws GuzzleException |
||
98 | * |
||
99 | * @When /^(?:I )?send a ([A-Z]+) request to "([^"]+)" with values:$/ |
||
100 | */ |
||
101 | public function iSendARequestWithValues($method, $url, TableNode $values) |
||
113 | |||
114 | /** |
||
115 | * Sends HTTP request to specific URL with raw body from PyString. |
||
116 | * |
||
117 | * @param string $method request method |
||
118 | * @param string $url relative url |
||
119 | * @param PyStringNode $body request body |
||
120 | * |
||
121 | * @throws GuzzleException |
||
122 | * |
||
123 | * @When /^(?:I )?send a ([A-Z]+) request to "([^"]+)" with body:$/ |
||
124 | */ |
||
125 | public function iSendARequestWithBody($method, $url, PyStringNode $body) |
||
132 | |||
133 | /** |
||
134 | * Sends HTTP request to specific URL with form data from PyString. |
||
135 | * |
||
136 | * @param string $method request method |
||
137 | * @param string $url relative url |
||
138 | * @param PyStringNode $body request body |
||
139 | * |
||
140 | * @throws GuzzleException |
||
141 | * |
||
142 | * @When /^(?:I )?send a ([A-Z]+) request to "([^"]+)" with form data:$/ |
||
143 | */ |
||
144 | public function iSendARequestWithFormData($method, $url, PyStringNode $body) |
||
157 | |||
158 | /** |
||
159 | * Checks that response has specific status code. |
||
160 | * |
||
161 | * @param string $code status code |
||
162 | * |
||
163 | * @Then /^(?:the )?response code should be (\d+)$/ |
||
164 | */ |
||
165 | public function theResponseCodeShouldBe($code) |
||
172 | |||
173 | /** |
||
174 | * Checks that response body contains specific text. |
||
175 | * |
||
176 | * @param string $text |
||
177 | * |
||
178 | * @Then /^(?:the )?response should contain "([^"]*)"$/ |
||
179 | */ |
||
180 | View Code Duplication | public function theResponseShouldContain($text) |
|
187 | |||
188 | /** |
||
189 | * Checks that response body doesn't contains specific text. |
||
190 | * |
||
191 | * @param string $text |
||
192 | * |
||
193 | * @Then /^(?:the )?response should not contain "([^"]*)"$/ |
||
194 | */ |
||
195 | View Code Duplication | public function theResponseShouldNotContain($text) |
|
202 | |||
203 | /** |
||
204 | * Checks that response body contains JSON from PyString. |
||
205 | * |
||
206 | * Do not check that the response body /only/ contains the JSON from PyString, |
||
207 | * |
||
208 | * @param PyStringNode $jsonString |
||
209 | * |
||
210 | * @throws \RuntimeException |
||
211 | * |
||
212 | * @Then /^(?:the )?response should contain json:$/ |
||
213 | */ |
||
214 | public function theResponseShouldContainJson(PyStringNode $jsonString) |
||
231 | |||
232 | /** |
||
233 | * Check if the response header has a specific value. |
||
234 | * |
||
235 | * @param string $name |
||
236 | * @param string $expected |
||
237 | * |
||
238 | * @Then /^the response "([^"]*)" header should be "([^"]*)"$/ |
||
239 | */ |
||
240 | public function theResponseHeaderShouldBe($name, $expected) |
||
245 | |||
246 | /** |
||
247 | * Prints last response body. |
||
248 | * |
||
249 | * @Then print response |
||
250 | */ |
||
251 | public function printResponse() |
||
270 | } |
||
271 |
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.