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:
Complex classes like JsonContext often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JsonContext, and based on these observations, apply Extract Interface, too.
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 theJsonNodesShouldBeEqualTo(TableNode $nodes) |
||
77 | |||
78 | public function theJsonNodesShoudBeEqualTo(TableNode $nodes) |
||
86 | |||
87 | /** |
||
88 | * Checks, that given JSON node is null |
||
89 | * |
||
90 | * @Then the JSON node :node should be null |
||
91 | */ |
||
92 | public function theJsonNodeShouldBeNull($node) |
||
104 | |||
105 | /** |
||
106 | * Checks, that given JSON node is not null. |
||
107 | * |
||
108 | * @Then the JSON node :node should not be null |
||
109 | */ |
||
110 | public function theJsonNodeShouldNotBeNull($name) |
||
116 | |||
117 | /** |
||
118 | * Checks, that given JSON node is true |
||
119 | * |
||
120 | * @Then the JSON node :node should be true |
||
121 | */ |
||
122 | public function theJsonNodeShouldBeTrue($node) |
||
134 | |||
135 | /** |
||
136 | * Checks, that given JSON node is false |
||
137 | * |
||
138 | * @Then the JSON node :node should be false |
||
139 | */ |
||
140 | public function theJsonNodeShouldBeFalse($node) |
||
152 | |||
153 | /** |
||
154 | * Checks, that given JSON node is equal to the given string |
||
155 | * |
||
156 | * @Then the JSON node :node should be equal to the string :text |
||
157 | */ |
||
158 | View Code Duplication | public function theJsonNodeShouldBeEqualToTheString($node, $text) |
|
170 | |||
171 | /** |
||
172 | * Checks, that given JSON node is equal to the given number |
||
173 | * |
||
174 | * @Then the JSON node :node should be equal to the number :number |
||
175 | */ |
||
176 | View Code Duplication | public function theJsonNodeShouldBeEqualToTheNumber($node, $number) |
|
188 | |||
189 | /** |
||
190 | * Checks, that given JSON node has N element(s) |
||
191 | * |
||
192 | * @Then the JSON node :node should have :count element(s) |
||
193 | */ |
||
194 | public function theJsonNodeShouldHaveElements($node, $count) |
||
202 | |||
203 | /** |
||
204 | * Checks, that given JSON node contains given value |
||
205 | * |
||
206 | * @Then the JSON node :node should contain :text |
||
207 | */ |
||
208 | View Code Duplication | public function theJsonNodeShouldContain($node, $text) |
|
216 | |||
217 | /** |
||
218 | * Checks, that given JSON nodes contains values |
||
219 | * |
||
220 | * @Then the JSON nodes should contain: |
||
221 | */ |
||
222 | public function theJsonNodesShouldContain(TableNode $nodes) |
||
228 | |||
229 | public function theJsonNodesShoudContain(TableNode $nodes) |
||
237 | |||
238 | /** |
||
239 | * Checks, that given JSON node does not contain given value |
||
240 | * |
||
241 | * @Then the JSON node :node should not contain :text |
||
242 | */ |
||
243 | View Code Duplication | public function theJsonNodeShouldNotContain($node, $text) |
|
251 | |||
252 | /** |
||
253 | * Checks, that given JSON nodes does not contain given value |
||
254 | * |
||
255 | * @Then the JSON nodes should not contain: |
||
256 | */ |
||
257 | public function theJsonNodesShouldNotContain(TableNode $nodes) |
||
263 | |||
264 | public function theJsonNodesShoudNotContain(TableNode $nodes) |
||
272 | |||
273 | /** |
||
274 | * Checks, that given JSON node exist |
||
275 | * |
||
276 | * @Given the JSON node :name should exist |
||
277 | */ |
||
278 | public function theJsonNodeShouldExist($name) |
||
290 | |||
291 | /** |
||
292 | * Checks, that given JSON node does not exist |
||
293 | * |
||
294 | * @Given the JSON node :name should not exist |
||
295 | */ |
||
296 | public function theJsonNodeShouldNotExist($name) |
||
302 | |||
303 | /** |
||
304 | * @Then the JSON should be valid according to this schema: |
||
305 | */ |
||
306 | public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema) |
||
313 | |||
314 | /** |
||
315 | * @Then the JSON should be valid according to the schema :filename |
||
316 | */ |
||
317 | public function theJsonShouldBeValidAccordingToTheSchema($filename) |
||
329 | |||
330 | /** |
||
331 | * @Then the JSON should be invalid according to the schema :filename |
||
332 | */ |
||
333 | public function theJsonShouldBeInvalidAccordingToTheSchema($filename) |
||
341 | |||
342 | /** |
||
343 | * @Then the JSON should be equal to: |
||
344 | */ |
||
345 | public function theJsonShouldBeEqualTo(PyStringNode $content) |
||
362 | |||
363 | /** |
||
364 | * @Then print last JSON response |
||
365 | */ |
||
366 | public function printLastJsonResponse() |
||
371 | |||
372 | protected function getJson() |
||
376 | |||
377 | private function checkSchemaFile($filename) |
||
385 | } |
||
386 |
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.