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) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Checks, that given JSON node is not null. |
||
| 117 | * |
||
| 118 | * @Then the JSON node :node should not be null |
||
| 119 | */ |
||
| 120 | public function theJsonNodeShouldNotBeNull($node) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Checks, that given JSON node is true |
||
| 129 | * |
||
| 130 | * @Then the JSON node :node should be true |
||
| 131 | */ |
||
| 132 | public function theJsonNodeShouldBeTrue($node) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Checks, that given JSON node is false |
||
| 147 | * |
||
| 148 | * @Then the JSON node :node should be false |
||
| 149 | */ |
||
| 150 | public function theJsonNodeShouldBeFalse($node) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Checks, that given JSON node is equal to the given string |
||
| 165 | * |
||
| 166 | * @Then the JSON node :node should be equal to the string :text |
||
| 167 | */ |
||
| 168 | View Code Duplication | public function theJsonNodeShouldBeEqualToTheString($node, $text) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Checks, that given JSON node is equal to the given number |
||
| 183 | * |
||
| 184 | * @Then the JSON node :node should be equal to the number :number |
||
| 185 | */ |
||
| 186 | View Code Duplication | public function theJsonNodeShouldBeEqualToTheNumber($node, $number) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Checks, that given JSON node has N element(s) |
||
| 201 | * |
||
| 202 | * @Then the JSON node :node should have :count element(s) |
||
| 203 | */ |
||
| 204 | View Code Duplication | public function theJsonNodeShouldHaveElements($node, $count) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Checks, that given JSON node has less than N element(s) |
||
| 215 | * |
||
| 216 | * @Then the JSON node :node should have less than :count element(s) |
||
| 217 | */ |
||
| 218 | View Code Duplication | public function theJsonNodeShouldHaveLessThanElements($node, $count) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Checks, that given JSON node has more than N element(s) |
||
| 229 | * |
||
| 230 | * @Then the JSON node :node should have more than :count element(s) |
||
| 231 | */ |
||
| 232 | View Code Duplication | public function theJsonNodeShouldHaveMoreThanElements($node, $count) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Checks, that given JSON node contains given value |
||
| 243 | * |
||
| 244 | * @Then the JSON node :node should contain :text |
||
| 245 | */ |
||
| 246 | View Code Duplication | public function theJsonNodeShouldContain($node, $text) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Checks, that given JSON nodes contains values |
||
| 257 | * |
||
| 258 | * @Then the JSON nodes should contain: |
||
| 259 | */ |
||
| 260 | public function theJsonNodesShouldContain(TableNode $nodes) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Checks, that given JSON node does not contain given value |
||
| 269 | * |
||
| 270 | * @Then the JSON node :node should not contain :text |
||
| 271 | */ |
||
| 272 | View Code Duplication | public function theJsonNodeShouldNotContain($node, $text) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Checks, that given JSON nodes does not contain given value |
||
| 283 | * |
||
| 284 | * @Then the JSON nodes should not contain: |
||
| 285 | */ |
||
| 286 | public function theJsonNodesShouldNotContain(TableNode $nodes) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Checks, that given JSON node exist |
||
| 295 | * |
||
| 296 | * @Then the JSON node :name should exist |
||
| 297 | */ |
||
| 298 | public function theJsonNodeShouldExist($name) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Checks, that given JSON node does not exist |
||
| 312 | * |
||
| 313 | * @Then the JSON node :name should not exist |
||
| 314 | */ |
||
| 315 | public function theJsonNodeShouldNotExist($name) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @Then the JSON should be valid according to this schema: |
||
| 324 | */ |
||
| 325 | public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @Then the JSON should be invalid according to this schema: |
||
| 335 | */ |
||
| 336 | public function theJsonShouldBeInvalidAccordingToThisSchema(PyStringNode $schema) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @Then the JSON should be valid according to the schema :filename |
||
| 345 | */ |
||
| 346 | public function theJsonShouldBeValidAccordingToTheSchema($filename) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @Then the JSON should be invalid according to the schema :filename |
||
| 361 | */ |
||
| 362 | public function theJsonShouldBeInvalidAccordingToTheSchema($filename) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @Then the JSON should be equal to: |
||
| 373 | */ |
||
| 374 | public function theJsonShouldBeEqualTo(PyStringNode $content) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @Then print last JSON response |
||
| 393 | */ |
||
| 394 | public function printLastJsonResponse() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Checks, that response JSON matches with a swagger dump |
||
| 402 | * |
||
| 403 | * @Then the JSON should be valid according to swagger :dumpPath dump schema :schemaName |
||
| 404 | */ |
||
| 405 | public function theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName) |
||
| 421 | /** |
||
| 422 | * |
||
| 423 | * Checks, that response JSON not matches with a swagger dump |
||
| 424 | * |
||
| 425 | * @Then the JSON should not be valid according to swagger :dumpPath dump schema :schemaName |
||
| 426 | */ |
||
| 427 | public function theJsonShouldNotBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName) |
||
| 433 | |||
| 434 | |||
| 435 | |||
| 436 | protected function getJson() |
||
| 440 | |||
| 441 | private function checkSchemaFile($filename) |
||
| 449 | } |
||
| 450 |