Complex classes like ManagingPaymentMethodsContext 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 ManagingPaymentMethodsContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | final class ManagingPaymentMethodsContext implements Context |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var CreatePageInterface |
||
| 30 | */ |
||
| 31 | private $createPage; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var IndexPageInterface |
||
| 35 | */ |
||
| 36 | private $indexPage; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var UpdatePageInterface |
||
| 40 | */ |
||
| 41 | private $updatePage; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var CurrentPageResolverInterface |
||
| 45 | */ |
||
| 46 | private $currentPageResolver; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var NotificationCheckerInterface |
||
| 50 | */ |
||
| 51 | private $notificationChecker; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | private $gatewayFactories; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param CreatePageInterface $createPage |
||
| 60 | * @param IndexPageInterface $indexPage |
||
| 61 | * @param UpdatePageInterface $updatePage |
||
| 62 | * @param CurrentPageResolverInterface $currentPageResolver |
||
| 63 | * @param NotificationCheckerInterface $notificationChecker |
||
| 64 | * @param array $gatewayFactories |
||
| 65 | */ |
||
| 66 | public function __construct( |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @Given I want to modify the :paymentMethod payment method |
||
| 84 | */ |
||
| 85 | public function iWantToModifyAPaymentMethod(PaymentMethodInterface $paymentMethod) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @When I name it :name in :language |
||
| 92 | * @When I rename it to :name in :language |
||
| 93 | * @When I remove its name from :language translation |
||
| 94 | */ |
||
| 95 | public function iNameItIn($name = null, $language) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @When I do not name it |
||
| 105 | */ |
||
| 106 | public function iDoNotNameIt() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @When I enable it |
||
| 113 | */ |
||
| 114 | public function iEnableIt() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @When I disable it |
||
| 121 | */ |
||
| 122 | public function iDisableIt() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @When I save my changes |
||
| 129 | * @When I try to save my changes |
||
| 130 | */ |
||
| 131 | public function iSaveMyChanges() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @When I delete the :paymentMethod payment method |
||
| 138 | * @When I try to delete the :paymentMethod payment method |
||
| 139 | */ |
||
| 140 | public function iDeletePaymentMethod(PaymentMethodInterface $paymentMethod) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @Then I should be notified that it is in use |
||
| 148 | */ |
||
| 149 | public function iShouldBeNotifiedThatItIsInUse() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @Then this payment method :element should be :value |
||
| 156 | */ |
||
| 157 | public function thisPaymentMethodElementShouldBe($element, $value) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @When I want to create a new offline payment method |
||
| 164 | * @When I want to create a new payment method with :factory gateway factory |
||
| 165 | */ |
||
| 166 | public function iWantToCreateANewPaymentMethod($factory = 'Offline') |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @When I specify its code as :code |
||
| 173 | * @When I do not specify its code |
||
| 174 | */ |
||
| 175 | public function iSpecifyItsCodeAs($code = null) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @When I describe it as :description in :language |
||
| 182 | */ |
||
| 183 | public function iDescribeItAsIn($description, $language) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @When make it available in channel :channel |
||
| 190 | */ |
||
| 191 | public function iMakeItAvailableInChannel($channel) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @Given I set its instruction as :instructions in :language |
||
| 198 | */ |
||
| 199 | public function iSetItsInstructionAsIn($instructions, $language) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @When I add it |
||
| 206 | * @When I try to add it |
||
| 207 | */ |
||
| 208 | public function iAddIt() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @When I check (also) the :paymentMethodName payment method |
||
| 215 | */ |
||
| 216 | public function iCheckThePaymentMethod(string $paymentMethodName): void |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @When I delete them |
||
| 223 | */ |
||
| 224 | public function iDeleteThem(): void |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @Then the payment method :paymentMethodName should appear in the registry |
||
| 231 | * @Then the payment method :paymentMethodName should be in the registry |
||
| 232 | * @Then I should see the payment method :paymentMethodName in the list |
||
| 233 | */ |
||
| 234 | public function thePaymentMethodShouldAppearInTheRegistry(string $paymentMethodName): void |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @Given /^(this payment method) should still be in the registry$/ |
||
| 243 | */ |
||
| 244 | public function thisPaymentMethodShouldStillBeInTheRegistry(PaymentMethodInterface $paymentMethod) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @Given I am browsing payment methods |
||
| 251 | * @When I browse payment methods |
||
| 252 | */ |
||
| 253 | public function iBrowsePaymentMethods() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @Then the first payment method on the list should have :field :value |
||
| 260 | */ |
||
| 261 | public function theFirstPaymentMethodOnTheListShouldHave($field, $value) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @Then the last payment method on the list should have :field :value |
||
| 268 | */ |
||
| 269 | public function theLastPaymentMethodOnTheListShouldHave($field, $value) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @When I switch the way payment methods are sorted by :field |
||
| 278 | * @When I start sorting payment methods by :field |
||
| 279 | * @Given the payment methods are already sorted by :field |
||
| 280 | */ |
||
| 281 | public function iSortPaymentMethodsBy($field) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @Then I should see a single payment method in the list |
||
| 288 | * @Then I should see :amount payment methods in the list |
||
| 289 | */ |
||
| 290 | public function iShouldSeePaymentMethodsInTheList(int $amount = 1): void |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @Then I should be notified that :element is required |
||
| 297 | */ |
||
| 298 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @Then I should be notified that I have to specify paypal :element |
||
| 305 | */ |
||
| 306 | public function iShouldBeNotifiedThatIHaveToSpecifyPaypal($element) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @Then I should be notified that gateway name should contain only letters and underscores |
||
| 316 | */ |
||
| 317 | public function iShouldBeNotifiedThatGatewayNameShouldContainOnlyLettersAndUnderscores() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @Then the payment method with :element :value should not be added |
||
| 327 | */ |
||
| 328 | public function thePaymentMethodWithElementValueShouldNotBeAdded($element, $value) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @Then /^(this payment method) should still be named "([^"]+)"$/ |
||
| 337 | */ |
||
| 338 | public function thisShippingMethodNameShouldBe(PaymentMethodInterface $paymentMethod, $paymentMethodName) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param string $element |
||
| 350 | * @param string $expectedMessage |
||
| 351 | */ |
||
| 352 | private function assertFieldValidationMessage($element, $expectedMessage) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @Then the code field should be disabled |
||
| 362 | */ |
||
| 363 | public function theCodeFieldShouldBeDisabled() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @Then the factory name field should be disabled |
||
| 370 | */ |
||
| 371 | public function theFactoryNameFieldShouldBeDisabled() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @Then this payment method should be enabled |
||
| 378 | */ |
||
| 379 | public function thisPaymentMethodShouldBeEnabled() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @Then this payment method should be disabled |
||
| 386 | */ |
||
| 387 | public function thisPaymentMethodShouldBeDisabled() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @Given the payment method :paymentMethod should have instructions :instructions in :language |
||
| 394 | */ |
||
| 395 | public function thePaymentMethodShouldHaveInstructionsIn( |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @Then the payment method :paymentMethod should be available in channel :channelName |
||
| 407 | */ |
||
| 408 | public function thePaymentMethodShouldBeAvailableInChannel( |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @Then /^(this payment method) should no longer exist in the registry$/ |
||
| 419 | */ |
||
| 420 | public function thisPaymentMethodShouldNoLongerExistInTheRegistry(PaymentMethodInterface $paymentMethod) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @Then I should be notified that payment method with this code already exists |
||
| 430 | */ |
||
| 431 | public function iShouldBeNotifiedThatPaymentMethodWithThisCodeAlreadyExists() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @Then there should still be only one payment method with :element :code |
||
| 438 | */ |
||
| 439 | public function thereShouldStillBeOnlyOnePaymentMethodWith($element, $code) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @When I configure it with test paypal credentials |
||
| 448 | */ |
||
| 449 | public function iConfigureItWithTestPaypalCredentials() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @When I configure it for username :username with :signature signature |
||
| 461 | */ |
||
| 462 | public function iConfigureItForUsernameWithSignature($username, $signature) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @When I do not specify configuration password |
||
| 473 | */ |
||
| 474 | public function iDoNotSpecifyConfigurationPassword() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @When I configure it with test stripe gateway data |
||
| 481 | */ |
||
| 482 | public function iConfigureItWithTestStripeGatewayData() |
||
| 487 | } |
||
| 488 |
If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway: