Complex classes like ManagingShippingMethodsContext 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 ManagingShippingMethodsContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | final class ManagingShippingMethodsContext implements Context |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var IndexPageInterface |
||
| 31 | */ |
||
| 32 | private $indexPage; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var CreatePageInterface |
||
| 36 | */ |
||
| 37 | private $createPage; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var UpdatePageInterface |
||
| 41 | */ |
||
| 42 | private $updatePage; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var CurrentPageResolverInterface |
||
| 46 | */ |
||
| 47 | private $currentPageResolver; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var NotificationCheckerInterface |
||
| 51 | */ |
||
| 52 | private $notificationChecker; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param IndexPageInterface $indexPage |
||
| 56 | * @param CreatePageInterface $createPage |
||
| 57 | * @param UpdatePageInterface $updatePage |
||
| 58 | * @param CurrentPageResolverInterface $currentPageResolver |
||
| 59 | * @param NotificationCheckerInterface $notificationChecker |
||
| 60 | */ |
||
| 61 | public function __construct( |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @When I want to create a new shipping method |
||
| 77 | */ |
||
| 78 | public function iWantToCreateANewShippingMethod() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @When I specify its code as :code |
||
| 85 | * @When I do not specify its code |
||
| 86 | */ |
||
| 87 | public function iSpecifyItsCodeAs($code = null) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @When I specify its position as :position |
||
| 94 | */ |
||
| 95 | public function iSpecifyItsPositionAs($position = null) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @When I name it :name in :language |
||
| 102 | * @When I rename it to :name in :language |
||
| 103 | */ |
||
| 104 | public function iNameItIn($name, $language) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @When I describe it as :description in :language |
||
| 111 | */ |
||
| 112 | public function iDescribeItAsIn($description, $language) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @When I define it for the :zoneName zone |
||
| 119 | */ |
||
| 120 | public function iDefineItForTheZone($zoneName) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @When I specify its amount as :amount for :channel channel |
||
| 127 | */ |
||
| 128 | public function iSpecifyItsAmountForChannel($amount, ChannelInterface $channel) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @When I make it available in channel :channelName |
||
| 135 | */ |
||
| 136 | public function iMakeItAvailableInChannel($channelName) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @When I add it |
||
| 143 | * @When I try to add it |
||
| 144 | */ |
||
| 145 | public function iAddIt() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @When I choose :calculatorName calculator |
||
| 152 | * @When I do not specify amount for :calculatorName calculator |
||
| 153 | */ |
||
| 154 | public function iChooseCalculator($calculatorName) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @Then the shipping method :shipmentMethod should appear in the registry |
||
| 161 | * @Then the shipping method :shipmentMethod should be in the registry |
||
| 162 | */ |
||
| 163 | public function theShipmentMethodShouldAppearInTheRegistry($shipmentMethodName) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @Given /^(this shipping method) should still be in the registry$/ |
||
| 172 | */ |
||
| 173 | public function thisShippingMethodShouldStillBeInTheRegistry(ShippingMethodInterface $shippingMethod) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @Then the shipping method :shippingMethod should be available in channel :channelName |
||
| 180 | */ |
||
| 181 | public function theShippingMethodShouldBeAvailableInChannel( |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @Then I should be notified that shipping method with this code already exists |
||
| 192 | */ |
||
| 193 | public function iShouldBeNotifiedThatShippingMethodWithThisCodeAlreadyExists() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @Then there should still be only one shipping method with :element :code |
||
| 200 | */ |
||
| 201 | public function thereShouldStillBeOnlyOneShippingMethodWith($element, $code) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @Given I want to modify a shipping method :shippingMethod |
||
| 210 | * @Given /^I want to modify (this shipping method)$/ |
||
| 211 | */ |
||
| 212 | public function iWantToModifyAShippingMethod(ShippingMethodInterface $shippingMethod) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @Then the code field should be disabled |
||
| 219 | */ |
||
| 220 | public function theCodeFieldShouldBeDisabled() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @Then /^(this shipping method) name should be "([^"]+)"$/ |
||
| 227 | * @Then /^(this shipping method) should still be named "([^"]+)"$/ |
||
| 228 | */ |
||
| 229 | public function thisShippingMethodNameShouldBe(ShippingMethodInterface $shippingMethod, $shippingMethodName) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @When I save my changes |
||
| 241 | * @When I try to save my changes |
||
| 242 | */ |
||
| 243 | public function iSaveMyChanges() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @Then I should be notified that :element is required |
||
| 250 | */ |
||
| 251 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @Then I should be notified that code needs to contain only specific symbols |
||
| 258 | */ |
||
| 259 | public function iShouldBeNotifiedThatCodeShouldContain() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @When I archive the :name shipping method |
||
| 269 | */ |
||
| 270 | public function iArchiveTheShippingMethod($name) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @When I restore the :name shipping method |
||
| 278 | */ |
||
| 279 | public function iRestoreTheShippingMethod($name) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @Then I should be viewing non archival shipping methods |
||
| 287 | */ |
||
| 288 | public function iShouldBeViewingNonArchivalShippingMethods() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @Then I should see :count shipping methods on the list |
||
| 295 | */ |
||
| 296 | public function thereShouldBeNoShippingMethodsOnTheList($count) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @Then the only shipping method on the list should be :name |
||
| 303 | */ |
||
| 304 | public function theOnlyShippingMethodOnTheListShouldBe($name) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @Then shipping method with :element :name should not be added |
||
| 312 | */ |
||
| 313 | public function shippingMethodWithElementValueShouldNotBeAdded($element, $name) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @When I do not name it |
||
| 322 | */ |
||
| 323 | public function iDoNotNameIt() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @When I do not specify its zone |
||
| 330 | */ |
||
| 331 | public function iDoNotSpecifyItsZone() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @When I remove its zone |
||
| 338 | */ |
||
| 339 | public function iRemoveItsZone() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @Then I should be notified that :element has to be selected |
||
| 346 | */ |
||
| 347 | public function iShouldBeNotifiedThatElementHasToBeSelected($element) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @When I remove its name from :language translation |
||
| 354 | */ |
||
| 355 | public function iRemoveItsNameFromTranslation($language) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @Given I am browsing shipping methods |
||
| 362 | * @When I want to browse shipping methods |
||
| 363 | */ |
||
| 364 | public function iWantToBrowseShippingMethods() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @Given I am browsing archival shipping methods |
||
| 371 | */ |
||
| 372 | public function iAmBrowsingArchivalShippingMethods() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @Given I filter archival shipping methods |
||
| 381 | */ |
||
| 382 | public function iFilterArchivalShippingMethods() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @Then the first shipping method on the list should have :field :value |
||
| 390 | */ |
||
| 391 | public function theFirstShippingMethodOnTheListShouldHave($field, $value) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @Then the last shipping method on the list should have :field :value |
||
| 400 | */ |
||
| 401 | public function theLastShippingMethodOnTheListShouldHave($field, $value) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @When I switch the way shipping methods are sorted by :field |
||
| 410 | * @When I start sorting shipping methods by :field |
||
| 411 | * @Given the shipping methods are already sorted by :field |
||
| 412 | */ |
||
| 413 | public function iSortShippingMethodsBy($field) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @Then I should see :numberOfShippingMethods shipping methods in the list |
||
| 420 | */ |
||
| 421 | public function iShouldSeeShippingMethodsInTheList($numberOfShippingMethods) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @When I enable it |
||
| 428 | */ |
||
| 429 | public function iEnableIt() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @When I disable it |
||
| 436 | */ |
||
| 437 | public function iDisableIt() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @Then /^(this shipping method) should be disabled$/ |
||
| 444 | */ |
||
| 445 | public function thisShippingMethodShouldBeDisabled(ShippingMethodInterface $shippingMethod) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @Then /^(this shipping method) should be enabled$/ |
||
| 452 | */ |
||
| 453 | public function thisShippingMethodShouldBeEnabled(ShippingMethodInterface $shippingMethod) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @When I delete shipping method :shippingMethod |
||
| 460 | * @When I try to delete shipping method :shippingMethod |
||
| 461 | */ |
||
| 462 | public function iDeleteShippingMethod(ShippingMethodInterface $shippingMethod) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @Then /^(this shipping method) should no longer exist in the registry$/ |
||
| 470 | */ |
||
| 471 | public function thisShippingMethodShouldNoLongerExistInTheRegistry(ShippingMethodInterface $shippingMethod) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @Then I should be notified that it is in use |
||
| 478 | */ |
||
| 479 | public function iShouldBeNotifiedThatItIsInUse() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @Then I should be notified that amount for :channel channel should not be blank |
||
| 486 | */ |
||
| 487 | public function iShouldBeNotifiedThatAmountForChannelShouldNotBeBlank(ChannelInterface $channel) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param string $element |
||
| 500 | * @param string $expectedMessage |
||
| 501 | */ |
||
| 502 | private function assertFieldValidationMessage($element, $expectedMessage) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @param ShippingMethodInterface $shippingMethod |
||
| 512 | * @param bool $state |
||
| 513 | */ |
||
| 514 | private function assertShippingMethodState(ShippingMethodInterface $shippingMethod, $state) |
||
| 523 | } |
||
| 524 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.