Complex classes like WebContext 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 WebContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class WebContext extends DefaultContext |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @Given /^I am on the (.+) (page|step)?$/ |
||
| 24 | * @When /^I go to the (.+) (page|step)?$/ |
||
| 25 | */ |
||
| 26 | public function iAmOnThePage($page) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @Then /^I should be on the (.+) (page|step)$/ |
||
| 33 | * @Then /^I should be redirected to the (.+) (page|step)$/ |
||
| 34 | * @Then /^I should still be on the (.+) (page|step)$/ |
||
| 35 | */ |
||
| 36 | public function iShouldBeOnThePage($page) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @Given /^I am on the page of ([^""]*) with ([^""]*) "([^""]*)"$/ |
||
| 48 | * @Given /^I go to the page of ([^""]*) with ([^""]*) "([^""]*)"$/ |
||
| 49 | */ |
||
| 50 | public function iAmOnTheResourcePage($type, $property, $value) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @Given /^I am on the page of ([^""(w)]*) "([^""]*)"$/ |
||
| 66 | * @Given /^I go to the page of ([^""(w)]*) "([^""]*)"$/ |
||
| 67 | */ |
||
| 68 | public function iAmOnTheResourcePageByName($type, $name) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @Then /^I should be on the page of ([^""]*) with ([^""]*) "([^""]*)"$/ |
||
| 90 | * @Then /^I should still be on the page of ([^""]*) with ([^""]*) "([^""]*)"$/ |
||
| 91 | */ |
||
| 92 | public function iShouldBeOnTheResourcePage($type, $property, $value) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @Then /^I should be on the page of ([^""(w)]*) "([^""]*)"$/ |
||
| 114 | * @Then /^I should still be on the page of ([^""(w)]*) "([^""]*)"$/ |
||
| 115 | */ |
||
| 116 | public function iShouldBeOnTheResourcePageByName($type, $name) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @Given /^I am (building|viewing|editing) ([^""]*) with ([^""]*) "([^""]*)"$/ |
||
| 144 | */ |
||
| 145 | public function iAmDoingSomethingWithResource($action, $type, $property, $value) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @Given /^I am (building|viewing|editing) ([^""(w)]*) "([^""]*)"$/ |
||
| 159 | */ |
||
| 160 | public function iAmDoingSomethingWithResourceByName($action, $type, $name) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @Then /^I should be (building|viewing|editing) ([^"]*) with ([^"]*) "([^""]*)"$/ |
||
| 186 | */ |
||
| 187 | public function iShouldBeDoingSomethingWithResource($action, $type, $property, $value) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @Then /^I should be (building|viewing|editing) ([^""(w)]*) "([^""]*)"$/ |
||
| 202 | */ |
||
| 203 | public function iShouldBeDoingSomethingWithResourceByName($action, $type, $name) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @Then /^(?:.* )?"([^"]*)" should appear on the page$/ |
||
| 224 | */ |
||
| 225 | public function textShouldAppearOnThePage($text) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @Then /^(?:.* )?"([^"]*)" should not appear on the page$/ |
||
| 232 | */ |
||
| 233 | public function textShouldNotAppearOnThePage($text) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @When /^I click "([^"]+)"$/ |
||
| 240 | */ |
||
| 241 | public function iClick($link) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @Given /^I should see an? "(?P<element>[^"]*)" element near "([^"]*)"$/ |
||
| 248 | */ |
||
| 249 | public function iShouldSeeAElementNear($element, $value) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @When /^I click "([^"]*)" near "([^"]*)"$/ |
||
| 257 | * @When /^I press "([^"]*)" near "([^"]*)"$/ |
||
| 258 | */ |
||
| 259 | public function iClickNear($button, $value) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @Then /^I should see "([^"]*)" field error$/ |
||
| 274 | */ |
||
| 275 | public function iShouldSeeFieldError($field) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @Given /^I should see (\d+) validation errors$/ |
||
| 284 | */ |
||
| 285 | public function iShouldSeeFieldsOnError($amount) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @Given /^I leave "([^"]*)" empty$/ |
||
| 292 | * @Given /^I leave "([^"]*)" field blank/ |
||
| 293 | */ |
||
| 294 | public function iLeaveFieldEmpty($field) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @Then /^I should see "([^"]*)" in "([^"]*)" field$/ |
||
| 301 | */ |
||
| 302 | public function iShouldSeeInField($value, $field) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * For example: I should see product with name "Wine X" in that list. |
||
| 309 | * |
||
| 310 | * @Then /^I should see (?:(?!enabled|disabled)[\w\s]+) with ((?:(?![\w\s]+ containing))[\w\s]+) "([^""]*)" in (?:that|the) list$/ |
||
| 311 | */ |
||
| 312 | public function iShouldSeeResourceWithValueInThatList($columnName, $value) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * For example: I should not see product with name "Wine X" in that list. |
||
| 324 | * |
||
| 325 | * @Then /^I should not see [\w\s]+ with ((?:(?![\w\s]+ containing))[\w\s]+) "([^""]*)" in (?:that|the) list$/ |
||
| 326 | */ |
||
| 327 | public function iShouldNotSeeResourceWithValueInThatList($columnName, $value) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * For example: I should see product with name containing "Wine X" in that list. |
||
| 339 | * |
||
| 340 | * @Then /^I should see (?:(?!enabled|disabled)[\w\s]+) with ([\w\s]+) containing "([^""]*)" in (?:that|the) list$/ |
||
| 341 | */ |
||
| 342 | public function iShouldSeeResourceWithValueContainingInThatList($columnName, $value) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * For example: I should not see product with name containing "Wine X" in that list. |
||
| 354 | * |
||
| 355 | * @Then /^I should not see [\w\s]+ with ([\w\s]+) containing "([^""]*)" in (?:that|the) list$/ |
||
| 356 | */ |
||
| 357 | public function iShouldNotSeeResourceWithValueContainingInThatList($columnName, $value) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * For example: I should see 10 products in that list. |
||
| 369 | * |
||
| 370 | * @Then /^I should see (\d+) ([^""]*) in (?:that|the) list$/ |
||
| 371 | */ |
||
| 372 | public function iShouldSeeThatMuchResourcesInTheList($amount, $type) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @Then /^I should be logged in$/ |
||
| 387 | */ |
||
| 388 | public function iShouldBeLoggedIn() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @Then /^I should not be logged in$/ |
||
| 397 | */ |
||
| 398 | public function iShouldNotBeLoggedIn() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @Given /^I click "([^"]*)" from the confirmation modal$/ |
||
| 407 | */ |
||
| 408 | public function iClickOnConfirmationModal($button) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @Given /^I add following attributes:$/ |
||
| 426 | */ |
||
| 427 | public function iAddFollowingAttributes(TableNode $attributes) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @Given /^I add "([^"]*)" attribute$/ |
||
| 439 | */ |
||
| 440 | public function iAddAttribute($attribute) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param array $attributes |
||
| 447 | */ |
||
| 448 | private function addAttributes(array $attributes) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @Given /^I choose "([^"]*)" attribute type$/ |
||
| 472 | */ |
||
| 473 | public function iChooseAttributeType($type) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @Given /^I wait (\d+) (seconds|second)$/ |
||
| 487 | */ |
||
| 488 | public function iWait($time) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @Then I should have my access denied |
||
| 495 | */ |
||
| 496 | public function iShouldHaveMyAccessDenied() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @Then /^I should see enabled [\w\s]+ with ([\w\s]+) "([^""]*)" in (?:that|the) list$/ |
||
| 503 | */ |
||
| 504 | public function iShouldSeeResourceInTheListAsEnabled($columnName, $value) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @Then /^I should see disabled [\w\s]+ with ([\w\s]+) "([^""]*)" in (?:that|the) list$/ |
||
| 516 | */ |
||
| 517 | public function iShouldSeeResourceInTheListAsDisabled($columnName, $value) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @Then /^I should see the following (?:row|rows):$/ |
||
| 529 | */ |
||
| 530 | public function iShouldSeeTheFollowingRow(TableNode $tableNode) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @Then /^I should not see the following (?:row|rows):$/ |
||
| 543 | */ |
||
| 544 | public function iShouldNotSeeTheFollowingRow(TableNode $tableNode) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @Then /^I should see ([\w\s]+) "([^""]*)" as available choice$/ |
||
| 557 | */ |
||
| 558 | public function iShouldSeeSelectWithOption($fieldName, $fieldOption) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @Then /^I should not see ([\w\s]+) "([^""]*)" as available choice$/ |
||
| 573 | */ |
||
| 574 | public function iShouldNotSeeSelectWithOption($fieldName, $fieldOption) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @Then /^I should still be on the (.+) page from ([^""]*) "([^""]*)"/ |
||
| 589 | */ |
||
| 590 | public function iShouldBeOnThePageWithGivenParent($page, $parentType, $parentName) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @Given /^I am (building|viewing|editing) ([^""]*) "([^""]*)" from ([^""]*) "([^""]*)"$/ |
||
| 603 | */ |
||
| 604 | public function iAmDoingSomethingWithResourceByNameFromGivenCategory($action, $type, $name, $categoryType, $categoryName) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * @Given /^I am on the zone creation page for type "([^"]*)"$/ |
||
| 619 | */ |
||
| 620 | public function iAmOnTheZoneCreationPageForType($type) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @Then /^I should be on the zone creation page for type "([^"]*)"$/ |
||
| 628 | * @Then /^I should still be on the zone creation page for type "([^"]*)"$/ |
||
| 629 | */ |
||
| 630 | public function iShouldBeOnTheZoneCreationPageForType($type) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * @Then /^I should see select "([^"]*)" with "([^"]*)" option selected$/ |
||
| 642 | */ |
||
| 643 | public function iShouldSeeSelectWithOptionSelected($fieldName, $fieldOption) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Assert that given code equals the current one. |
||
| 651 | * |
||
| 652 | * @param int $code |
||
| 653 | */ |
||
| 654 | protected function assertStatusCodeEquals($code) |
||
| 662 | |||
| 663 | /** |
||
| 664 | * @param string $name |
||
| 665 | */ |
||
| 666 | private function iAmOnTheCountryPageByName($name) |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @param string $action |
||
| 675 | * @param string $name |
||
| 676 | */ |
||
| 677 | private function iShouldBeDoingSomethingWithCountryByName($action, $name) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @param string $action |
||
| 686 | * @param string $name |
||
| 687 | */ |
||
| 688 | private function iAmDoingSomethingWithCountryByName($action, $name) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * @param string $name |
||
| 697 | */ |
||
| 698 | private function iShouldBeOnTheCountryPageByName($name) |
||
| 704 | |||
| 705 | /** |
||
| 706 | * @param NodeElement $modalContainer |
||
| 707 | */ |
||
| 708 | protected function waitForModalToAppear($modalContainer) |
||
| 714 | |||
| 715 | /** |
||
| 716 | * @param NodeElement $modalContainer |
||
| 717 | */ |
||
| 718 | protected function waitForModalToDisappear($modalContainer) |
||
| 724 | |||
| 725 | /** |
||
| 726 | * @Given /^I am on the product attribute creation page with type "([^"]*)"$/ |
||
| 727 | */ |
||
| 728 | public function iAmOnTheProductAttributeCreationPageWithType($type) |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @Given /^I should not be able to edit "([^"]*)" (field|select)$/ |
||
| 736 | */ |
||
| 737 | public function iShouldNotBeAbleToEditSelect($name, $fieldType) |
||
| 744 | } |
||
| 745 |