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 |
||
14 | class RestApiContext implements Context, SnippetAcceptingContext |
||
15 | { |
||
16 | private $asserter; |
||
17 | |||
18 | private $restApiBrowser; |
||
19 | |||
20 | public function __construct(RestApiBrowser $restApiBrowser) |
||
25 | |||
26 | /** |
||
27 | * @param string $method request method |
||
28 | * @param string $url relative url |
||
29 | * |
||
30 | * @When /^(?:I )?send a ([A-Z]+) request to "([^"]+)"$/ |
||
31 | */ |
||
32 | public function iSendARequest($method, $url) |
||
36 | |||
37 | /** |
||
38 | * Sends HTTP request to specific URL with raw body from PyString. |
||
39 | * |
||
40 | * @param string $method request method |
||
41 | * @param string $url relative url |
||
42 | * @param PyStringNode $body |
||
43 | * |
||
44 | * @When /^(?:I )?send a ([A-Z]+) request to "([^"]+)" with body:$/ |
||
45 | */ |
||
46 | public function iSendARequestWithBody($method, $url, PyStringNode $body) |
||
50 | |||
51 | /** |
||
52 | * Sends HTTP request to specific URL with POST parameters. |
||
53 | * |
||
54 | * @When I send a :method request to :url with form data: |
||
55 | */ |
||
56 | public function iSendAPostRequestToWithFormData($method, $url, TableNode $formData) |
||
60 | |||
61 | /** |
||
62 | * @param string $code status code |
||
63 | * |
||
64 | * @Then /^(?:the )?response status code should be (\d+)$/ |
||
65 | */ |
||
66 | public function theResponseCodeShouldBe($code) |
||
72 | |||
73 | /** |
||
74 | * @return ResponseInterface |
||
75 | */ |
||
76 | private function getResponse() |
||
80 | |||
81 | /** |
||
82 | * @Given /^I set "([^"]*)" header equal to "([^"]*)"$/ |
||
83 | */ |
||
84 | public function iSetHeaderEqualTo($headerName, $headerValue) |
||
88 | |||
89 | /** |
||
90 | * @Given /^I add "([^"]*)" header equal to "([^"]*)"$/ |
||
91 | */ |
||
92 | public function iAddHeaderEqualTo($headerName, $headerValue) |
||
96 | |||
97 | /** |
||
98 | * Set login / password for next HTTP authentication |
||
99 | * |
||
100 | * @When /^I set basic authentication with "(?P<username>[^"]*)" and "(?P<password>[^"]*)"$/ |
||
101 | */ |
||
102 | public function iSetBasicAuthenticationWithAnd($username, $password) |
||
107 | |||
108 | /** |
||
109 | * @Then print request and response |
||
110 | */ |
||
111 | public function printRequestAndResponse() |
||
118 | |||
119 | /** |
||
120 | * @Then print request |
||
121 | */ |
||
122 | View Code Duplication | public function printRequest() |
|
133 | |||
134 | /** |
||
135 | * @return RequestInterface |
||
136 | */ |
||
137 | private function getRequest() |
||
141 | |||
142 | /** |
||
143 | * @param array $headers |
||
144 | * @return string |
||
145 | */ |
||
146 | private function getRawHeaders(array $headers) |
||
156 | |||
157 | /** |
||
158 | * @Then print response |
||
159 | */ |
||
160 | View Code Duplication | public function printResponse() |
|
173 | } |
||
174 |
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.