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 |
||
14 | class JsonContext extends BaseContext |
||
15 | { |
||
16 | protected $inspector; |
||
17 | |||
18 | protected $httpCallResultPool; |
||
19 | |||
20 | public function __construct(HttpCallResultPool $httpCallResultPool, $evaluationMode = 'javascript') |
||
25 | |||
26 | /** |
||
27 | * Checks, that the response is correct JSON |
||
28 | * |
||
29 | * @Then the response should be in JSON |
||
30 | */ |
||
31 | public function theResponseShouldBeInJson() |
||
35 | |||
36 | /** |
||
37 | * Checks, that the response is not correct JSON |
||
38 | * |
||
39 | * @Then the response should not be in JSON |
||
40 | */ |
||
41 | public function theResponseShouldNotBeInJson() |
||
48 | |||
49 | /** |
||
50 | * Checks, that given JSON node is equal to given value |
||
51 | * |
||
52 | * @Then the JSON node :node should be equal to :text |
||
53 | */ |
||
54 | public function theJsonNodeShouldBeEqualTo($node, $text) |
||
66 | |||
67 | /** |
||
68 | * Checks, that given JSON nodes are equal to givens values |
||
69 | * |
||
70 | * @Then the JSON nodes should be equal to: |
||
71 | */ |
||
72 | public function theJsonNodesShouldBeEqualTo(TableNode $nodes) |
||
78 | |||
79 | /** |
||
80 | * Checks, that given JSON node matches given pattern |
||
81 | * |
||
82 | * @Then the JSON node :node should match :pattern |
||
83 | */ |
||
84 | public function theJsonNodeShouldMatch($node, $pattern) |
||
96 | |||
97 | /** |
||
98 | * Checks, that given JSON node is null |
||
99 | * |
||
100 | * @Then the JSON node :node should be null |
||
101 | */ |
||
102 | public function theJsonNodeShouldBeNull($node) |
||
110 | |||
111 | /** |
||
112 | * Checks, that given JSON node is not null. |
||
113 | * |
||
114 | * @Then the JSON node :node should not be null |
||
115 | */ |
||
116 | public function theJsonNodeShouldNotBeNull($node) |
||
122 | |||
123 | /** |
||
124 | * Checks, that given JSON node is true |
||
125 | * |
||
126 | * @Then the JSON node :node should be true |
||
127 | */ |
||
128 | public function theJsonNodeShouldBeTrue($node) |
||
140 | |||
141 | /** |
||
142 | * Checks, that given JSON node is false |
||
143 | * |
||
144 | * @Then the JSON node :node should be false |
||
145 | */ |
||
146 | public function theJsonNodeShouldBeFalse($node) |
||
158 | |||
159 | /** |
||
160 | * Checks, that given JSON node is equal to the given string |
||
161 | * |
||
162 | * @Then the JSON node :node should be equal to the string :text |
||
163 | */ |
||
164 | View Code Duplication | public function theJsonNodeShouldBeEqualToTheString($node, $text) |
|
176 | |||
177 | /** |
||
178 | * Checks, that given JSON node is equal to the given number |
||
179 | * |
||
180 | * @Then the JSON node :node should be equal to the number :number |
||
181 | */ |
||
182 | View Code Duplication | public function theJsonNodeShouldBeEqualToTheNumber($node, $number) |
|
194 | |||
195 | /** |
||
196 | * Checks, that given JSON node has N element(s) |
||
197 | * |
||
198 | * @Then the JSON node :node should have :count element(s) |
||
199 | */ |
||
200 | public function theJsonNodeShouldHaveElements($node, $count) |
||
208 | |||
209 | /** |
||
210 | * Checks, that given JSON node contains given value |
||
211 | * |
||
212 | * @Then the JSON node :node should contain :text |
||
213 | */ |
||
214 | View Code Duplication | public function theJsonNodeShouldContain($node, $text) |
|
222 | |||
223 | /** |
||
224 | * Checks, that given JSON nodes contains values |
||
225 | * |
||
226 | * @Then the JSON nodes should contain: |
||
227 | */ |
||
228 | public function theJsonNodesShouldContain(TableNode $nodes) |
||
234 | |||
235 | /** |
||
236 | * Checks, that given JSON node does not contain given value |
||
237 | * |
||
238 | * @Then the JSON node :node should not contain :text |
||
239 | */ |
||
240 | View Code Duplication | public function theJsonNodeShouldNotContain($node, $text) |
|
248 | |||
249 | /** |
||
250 | * Checks, that given JSON nodes does not contain given value |
||
251 | * |
||
252 | * @Then the JSON nodes should not contain: |
||
253 | */ |
||
254 | public function theJsonNodesShouldNotContain(TableNode $nodes) |
||
260 | |||
261 | /** |
||
262 | * Checks, that given JSON node exist |
||
263 | * |
||
264 | * @Then the JSON node :name should exist |
||
265 | */ |
||
266 | public function theJsonNodeShouldExist($name) |
||
277 | |||
278 | /** |
||
279 | * Checks, that given JSON node does not exist |
||
280 | * |
||
281 | * @Then the JSON node :name should not exist |
||
282 | */ |
||
283 | public function theJsonNodeShouldNotExist($name) |
||
289 | |||
290 | /** |
||
291 | * @Then the JSON should be valid according to this schema: |
||
292 | */ |
||
293 | public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema) |
||
300 | |||
301 | /** |
||
302 | * @Then the JSON should be invalid according to this schema: |
||
303 | */ |
||
304 | public function theJsonShouldBeInvalidAccordingToThisSchema(PyStringNode $schema) |
||
310 | |||
311 | /** |
||
312 | * @Then the JSON should be valid according to the schema :filename |
||
313 | */ |
||
314 | public function theJsonShouldBeValidAccordingToTheSchema($filename) |
||
326 | |||
327 | /** |
||
328 | * @Then the JSON should be invalid according to the schema :filename |
||
329 | */ |
||
330 | public function theJsonShouldBeInvalidAccordingToTheSchema($filename) |
||
338 | |||
339 | /** |
||
340 | * @Then the JSON should be equal to: |
||
341 | */ |
||
342 | public function theJsonShouldBeEqualTo(PyStringNode $content) |
||
358 | |||
359 | /** |
||
360 | * @Then print last JSON response |
||
361 | */ |
||
362 | public function printLastJsonResponse() |
||
367 | |||
368 | /** |
||
369 | * Checks, that response JSON matches with a swagger dump |
||
370 | * |
||
371 | * @Then the JSON should be valid according to swagger :dumpPath dump schema :schemaName |
||
372 | */ |
||
373 | public function theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName) |
||
389 | /** |
||
390 | * |
||
391 | * Checks, that response JSON not matches with a swagger dump |
||
392 | * |
||
393 | * @Then the JSON should not be valid according to swagger :dumpPath dump schema :schemaName |
||
394 | */ |
||
395 | public function theJsonShouldNotBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName) |
||
401 | |||
402 | |||
403 | |||
404 | protected function getJson() |
||
408 | |||
409 | private function checkSchemaFile($filename) |
||
417 | } |
||
418 |