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 | public function theJsonNodesShoudBeEqualTo(TableNode $nodes) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Checks, that given JSON node is null |
||
| 90 | * |
||
| 91 | * @Then the JSON node :node should be null |
||
| 92 | */ |
||
| 93 | public function theJsonNodeShouldBeNull($node) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Checks, that given JSON node is not null. |
||
| 108 | * |
||
| 109 | * @Then the JSON node :node should not be null |
||
| 110 | */ |
||
| 111 | public function theJsonNodeShouldNotBeNull($name) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Checks, that given JSON node is true |
||
| 120 | * |
||
| 121 | * @Then the JSON node :node should be true |
||
| 122 | */ |
||
| 123 | public function theJsonNodeShouldBeTrue($node) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Checks, that given JSON node is false |
||
| 138 | * |
||
| 139 | * @Then the JSON node :node should be false |
||
| 140 | */ |
||
| 141 | public function theJsonNodeShouldBeFalse($node) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Checks, that given JSON node is equal to the given string |
||
| 156 | * |
||
| 157 | * @Then the JSON node :node should be equal to the string :text |
||
| 158 | */ |
||
| 159 | View Code Duplication | public function theJsonNodeShouldBeEqualToTheString($node, $text) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Checks, that given JSON node is equal to the given number |
||
| 174 | * |
||
| 175 | * @Then the JSON node :node should be equal to the number :number |
||
| 176 | */ |
||
| 177 | View Code Duplication | public function theJsonNodeShouldBeEqualToTheNumber($node, $number) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Checks, that given JSON node has N element(s) |
||
| 192 | * |
||
| 193 | * @Then the JSON node :node should have :count element(s) |
||
| 194 | */ |
||
| 195 | public function theJsonNodeShouldHaveElements($node, $count) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Checks, that given JSON node contains given value |
||
| 206 | * |
||
| 207 | * @Then the JSON node :node should contain :text |
||
| 208 | */ |
||
| 209 | View Code Duplication | public function theJsonNodeShouldContain($node, $text) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Checks, that given JSON nodes contains values |
||
| 220 | * |
||
| 221 | * @Then the JSON nodes should contain: |
||
| 222 | */ |
||
| 223 | public function theJsonNodesShouldContain(TableNode $nodes) |
||
| 229 | |||
| 230 | public function theJsonNodesShoudContain(TableNode $nodes) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Checks, that given JSON node does not contain given value |
||
| 241 | * |
||
| 242 | * @Then the JSON node :node should not contain :text |
||
| 243 | */ |
||
| 244 | View Code Duplication | public function theJsonNodeShouldNotContain($node, $text) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Checks, that given JSON nodes does not contain given value |
||
| 255 | * |
||
| 256 | * @Then the JSON nodes should not contain: |
||
| 257 | */ |
||
| 258 | public function theJsonNodesShouldNotContain(TableNode $nodes) |
||
| 264 | |||
| 265 | public function theJsonNodesShoudNotContain(TableNode $nodes) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Checks, that given JSON node exist |
||
| 276 | * |
||
| 277 | * @Given the JSON node :name should exist |
||
| 278 | */ |
||
| 279 | public function theJsonNodeShouldExist($name) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Checks, that given JSON node does not exist |
||
| 294 | * |
||
| 295 | * @Given the JSON node :name should not exist |
||
| 296 | */ |
||
| 297 | public function theJsonNodeShouldNotExist($name) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @Then the JSON should be valid according to this schema: |
||
| 306 | */ |
||
| 307 | public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @Then the JSON should be invalid according to this schema: |
||
| 317 | */ |
||
| 318 | public function theJsonShouldBeInvalidAccordingToThisSchema(PyStringNode $schema) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @Then the JSON should be valid according to the schema :filename |
||
| 327 | */ |
||
| 328 | public function theJsonShouldBeValidAccordingToTheSchema($filename) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @Then the JSON should be invalid according to the schema :filename |
||
| 343 | */ |
||
| 344 | public function theJsonShouldBeInvalidAccordingToTheSchema($filename) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @Then the JSON should be equal to: |
||
| 355 | */ |
||
| 356 | public function theJsonShouldBeEqualTo(PyStringNode $content) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @Then print last JSON response |
||
| 376 | */ |
||
| 377 | public function printLastJsonResponse() |
||
| 382 | |||
| 383 | protected function getJson() |
||
| 387 | |||
| 388 | private function checkSchemaFile($filename) |
||
| 396 | } |
||
| 397 |