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 |
||
16 | class JsonContext extends BaseContext |
||
17 | { |
||
18 | protected $inspector; |
||
19 | |||
20 | protected $httpCallResultPool; |
||
21 | |||
22 | public function __construct(HttpCallResultPool $httpCallResultPool, $evaluationMode = 'javascript') |
||
27 | |||
28 | /** |
||
29 | * Checks, that the response is correct JSON |
||
30 | * |
||
31 | * @Then the response should be in JSON |
||
32 | */ |
||
33 | public function theResponseShouldBeInJson() |
||
37 | |||
38 | /** |
||
39 | * Checks, that the response is not correct JSON |
||
40 | * |
||
41 | * @Then the response should not be in JSON |
||
42 | */ |
||
43 | public function theResponseShouldNotBeInJson() |
||
50 | |||
51 | /** |
||
52 | * Checks, that given JSON node is equal to given value |
||
53 | * |
||
54 | * @Then the JSON node :node should be equal to :text |
||
55 | */ |
||
56 | View Code Duplication | public function theJsonNodeShouldBeEqualTo($node, $text) |
|
68 | |||
69 | /** |
||
70 | * Checks, that given JSON nodes are equal to givens values |
||
71 | * |
||
72 | * @Then the JSON nodes should be equal to: |
||
73 | */ |
||
74 | public function theJsonNodesShouldBeEqualTo(TableNode $nodes) |
||
80 | |||
81 | /** |
||
82 | * Checks, that given JSON node matches given pattern |
||
83 | * |
||
84 | * @Then the JSON node :node should match :pattern |
||
85 | */ |
||
86 | public function theJsonNodeShouldMatch($node, $pattern) |
||
104 | |||
105 | /** |
||
106 | * Checks, that given JSON node is null |
||
107 | * |
||
108 | * @Then the JSON node :node should be null |
||
109 | */ |
||
110 | public function theJsonNodeShouldBeNull($node) |
||
122 | |||
123 | /** |
||
124 | * Checks, that given JSON node is not null. |
||
125 | * |
||
126 | * @Then the JSON node :node should not be null |
||
127 | */ |
||
128 | public function theJsonNodeShouldNotBeNull($node) |
||
134 | |||
135 | /** |
||
136 | * Checks, that given JSON node is true |
||
137 | * |
||
138 | * @Then the JSON node :node should be true |
||
139 | */ |
||
140 | public function theJsonNodeShouldBeTrue($node) |
||
152 | |||
153 | /** |
||
154 | * Checks, that given JSON node is false |
||
155 | * |
||
156 | * @Then the JSON node :node should be false |
||
157 | */ |
||
158 | public function theJsonNodeShouldBeFalse($node) |
||
170 | |||
171 | /** |
||
172 | * Checks, that given JSON node is equal to the given string |
||
173 | * |
||
174 | * @Then the JSON node :node should be equal to the string :text |
||
175 | */ |
||
176 | View Code Duplication | public function theJsonNodeShouldBeEqualToTheString($node, $text) |
|
188 | |||
189 | /** |
||
190 | * Checks, that given JSON node is equal to the given number |
||
191 | * |
||
192 | * @Then the JSON node :node should be equal to the number :number |
||
193 | */ |
||
194 | View Code Duplication | public function theJsonNodeShouldBeEqualToTheNumber($node, $number) |
|
206 | |||
207 | /** |
||
208 | * Checks, that given JSON node has N element(s) |
||
209 | * |
||
210 | * @Then the JSON node :node should have :count element(s) |
||
211 | */ |
||
212 | public function theJsonNodeShouldHaveElements($node, $count) |
||
220 | |||
221 | /** |
||
222 | * Checks, that given JSON node contains given value |
||
223 | * |
||
224 | * @Then the JSON node :node should contain :text |
||
225 | */ |
||
226 | View Code Duplication | public function theJsonNodeShouldContain($node, $text) |
|
234 | |||
235 | /** |
||
236 | * Checks, that given JSON nodes contains values |
||
237 | * |
||
238 | * @Then the JSON nodes should contain: |
||
239 | */ |
||
240 | public function theJsonNodesShouldContain(TableNode $nodes) |
||
246 | |||
247 | /** |
||
248 | * Checks, that given JSON node does not contain given value |
||
249 | * |
||
250 | * @Then the JSON node :node should not contain :text |
||
251 | */ |
||
252 | View Code Duplication | public function theJsonNodeShouldNotContain($node, $text) |
|
260 | |||
261 | /** |
||
262 | * Checks, that given JSON nodes does not contain given value |
||
263 | * |
||
264 | * @Then the JSON nodes should not contain: |
||
265 | */ |
||
266 | public function theJsonNodesShouldNotContain(TableNode $nodes) |
||
272 | |||
273 | /** |
||
274 | * Checks, that given JSON node exist |
||
275 | * |
||
276 | * @Then the JSON node :name should exist |
||
277 | */ |
||
278 | public function theJsonNodeShouldExist($name) |
||
289 | |||
290 | /** |
||
291 | * Checks, that given JSON node does not exist |
||
292 | * |
||
293 | * @Then the JSON node :name should not exist |
||
294 | */ |
||
295 | public function theJsonNodeShouldNotExist($name) |
||
301 | |||
302 | /** |
||
303 | * @Then the JSON should be valid according to this schema: |
||
304 | */ |
||
305 | public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema) |
||
312 | |||
313 | /** |
||
314 | * @Then the JSON should be invalid according to this schema: |
||
315 | */ |
||
316 | public function theJsonShouldBeInvalidAccordingToThisSchema(PyStringNode $schema) |
||
322 | |||
323 | /** |
||
324 | * @Then the JSON should be valid according to the schema :filename |
||
325 | */ |
||
326 | public function theJsonShouldBeValidAccordingToTheSchema($filename) |
||
338 | |||
339 | /** |
||
340 | * @Then the JSON should be invalid according to the schema :filename |
||
341 | */ |
||
342 | public function theJsonShouldBeInvalidAccordingToTheSchema($filename) |
||
350 | |||
351 | /** |
||
352 | * @Then the JSON should be equal to: |
||
353 | */ |
||
354 | public function theJsonShouldBeEqualTo(PyStringNode $content) |
||
370 | |||
371 | /** |
||
372 | * @Then the JSON should match: |
||
373 | */ |
||
374 | public function theJsonShouldMatch(PyStringNode $content) |
||
388 | |||
389 | /** |
||
390 | * @Then print last JSON response |
||
391 | */ |
||
392 | public function printLastJsonResponse() |
||
397 | |||
398 | /** |
||
399 | * Checks, that response JSON matches with a swagger dump |
||
400 | * |
||
401 | * @Then the JSON should be valid according to swagger :dumpPath dump schema :schemaName |
||
402 | */ |
||
403 | public function theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName) |
||
419 | /** |
||
420 | * |
||
421 | * Checks, that response JSON not matches with a swagger dump |
||
422 | * |
||
423 | * @Then the JSON should not be valid according to swagger :dumpPath dump schema :schemaName |
||
424 | */ |
||
425 | public function theJsonShouldNotBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName) |
||
431 | |||
432 | |||
433 | |||
434 | protected function getJson() |
||
438 | |||
439 | private function checkSchemaFile($filename) |
||
447 | } |
||
448 |