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 |
||
13 | class JsonContext extends BaseContext |
||
14 | { |
||
15 | protected $inspector; |
||
16 | |||
17 | protected $httpCallResultPool; |
||
18 | |||
19 | public function __construct(HttpCallResultPool $httpCallResultPool, $evaluationMode = 'javascript') |
||
24 | |||
25 | /** |
||
26 | * Checks, that the response is correct JSON |
||
27 | * |
||
28 | * @Then the response should be in JSON |
||
29 | */ |
||
30 | public function theResponseShouldBeInJson() |
||
34 | |||
35 | /** |
||
36 | * Checks, that the response is not correct JSON |
||
37 | * |
||
38 | * @Then the response should not be in JSON |
||
39 | */ |
||
40 | public function theResponseShouldNotBeInJson() |
||
47 | |||
48 | /** |
||
49 | * Checks, that given JSON node is equal to given value |
||
50 | * |
||
51 | * @Then the JSON node :node should be equal to :text |
||
52 | */ |
||
53 | public function theJsonNodeShouldBeEqualTo($node, $text) |
||
65 | |||
66 | /** |
||
67 | * Checks, that given JSON nodes are equal to givens values |
||
68 | * |
||
69 | * @Then the JSON nodes should be equal to: |
||
70 | */ |
||
71 | public function theJsonNodesShoudBeEqualTo(TableNode $nodes) |
||
77 | |||
78 | /** |
||
79 | * Checks, that given JSON node is null |
||
80 | * |
||
81 | * @Then the JSON node :node should be null |
||
82 | */ |
||
83 | public function theJsonNodeShouldBeNull($node) |
||
95 | |||
96 | /** |
||
97 | * Checks, that given JSON node is not null. |
||
98 | * |
||
99 | * @Then the JSON node :node should not be null |
||
100 | */ |
||
101 | public function theJsonNodeShouldNotBeNull($name) |
||
107 | |||
108 | /** |
||
109 | * Checks, that given JSON node is true |
||
110 | * |
||
111 | * @Then the JSON node :node should be true |
||
112 | */ |
||
113 | public function theJsonNodeShouldBeTrue($node) |
||
125 | |||
126 | /** |
||
127 | * Checks, that given JSON node is false |
||
128 | * |
||
129 | * @Then the JSON node :node should be false |
||
130 | */ |
||
131 | public function theJsonNodeShouldBeFalse($node) |
||
143 | |||
144 | /** |
||
145 | * Checks, that given JSON node is equal to the given string |
||
146 | * |
||
147 | * @Then the JSON node :node should be equal to the string :text |
||
148 | */ |
||
149 | View Code Duplication | public function theJsonNodeShouldBeEqualToTheString($node, $text) |
|
161 | |||
162 | /** |
||
163 | * Checks, that given JSON node is equal to the given number |
||
164 | * |
||
165 | * @Then the JSON node :node should be equal to the number :number |
||
166 | */ |
||
167 | View Code Duplication | public function theJsonNodeShouldBeEqualToTheNumber($node, $number) |
|
179 | |||
180 | /** |
||
181 | * Checks, that given JSON node has N element(s) |
||
182 | * |
||
183 | * @Then the JSON node :node should have :count element(s) |
||
184 | */ |
||
185 | public function theJsonNodeShouldHaveElements($node, $count) |
||
193 | |||
194 | /** |
||
195 | * Checks, that given JSON node contains given value |
||
196 | * |
||
197 | * @Then the JSON node :node should contain :text |
||
198 | */ |
||
199 | View Code Duplication | public function theJsonNodeShouldContain($node, $text) |
|
207 | |||
208 | /** |
||
209 | * Checks, that given JSON nodes contains values |
||
210 | * |
||
211 | * @Then the JSON nodes should contain: |
||
212 | */ |
||
213 | public function theJsonNodesShoudContain(TableNode $nodes) |
||
219 | |||
220 | /** |
||
221 | * Checks, that given JSON node does not contain given value |
||
222 | * |
||
223 | * @Then the JSON node :node should not contain :text |
||
224 | */ |
||
225 | View Code Duplication | public function theJsonNodeShouldNotContain($node, $text) |
|
233 | |||
234 | /** |
||
235 | * Checks, that given JSON nodes does not contain given value |
||
236 | * |
||
237 | * @Then the JSON nodes should not contain: |
||
238 | */ |
||
239 | public function theJsonNodesShoudNotContain(TableNode $nodes) |
||
245 | |||
246 | /** |
||
247 | * Checks, that given JSON node exist |
||
248 | * |
||
249 | * @Given the JSON node :name should exist |
||
250 | */ |
||
251 | public function theJsonNodeShouldExist($name) |
||
263 | |||
264 | /** |
||
265 | * Checks, that given JSON node does not exist |
||
266 | * |
||
267 | * @Given the JSON node :name should not exist |
||
268 | */ |
||
269 | public function theJsonNodeShouldNotExist($name) |
||
275 | |||
276 | /** |
||
277 | * @Then the JSON should be valid according to this schema: |
||
278 | */ |
||
279 | public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema) |
||
286 | |||
287 | /** |
||
288 | * @Then the JSON should be valid according to the schema :filename |
||
289 | */ |
||
290 | public function theJsonShouldBeValidAccordingToTheSchema($filename) |
||
302 | |||
303 | /** |
||
304 | * @Then the JSON should be invalid according to the schema :filename |
||
305 | */ |
||
306 | public function theJsonShouldBeInvalidAccordingToTheSchema($filename) |
||
314 | |||
315 | /** |
||
316 | * @Then the JSON should be equal to: |
||
317 | */ |
||
318 | public function theJsonShouldBeEqualTo(PyStringNode $content) |
||
335 | |||
336 | /** |
||
337 | * @Then print last JSON response |
||
338 | */ |
||
339 | public function printLastJsonResponse() |
||
344 | |||
345 | protected function getJson() |
||
349 | |||
350 | private function checkSchemaFile($filename) |
||
358 | } |
||
359 |
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.