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  | 
            ||
| 28 | final class ManagingShippingMethodsContext implements Context  | 
            ||
| 29 | { | 
            ||
| 30 | public const SORT_TYPES = ['ascending' => 'asc', 'descending' => 'desc'];  | 
            ||
| 31 | |||
| 32 | /** @var ApiClientInterface */  | 
            ||
| 33 | private $client;  | 
            ||
| 34 | |||
| 35 | /** @var ApiClientInterface */  | 
            ||
| 36 | private $adminUsersClient;  | 
            ||
| 37 | |||
| 38 | /** @var ResponseCheckerInterface */  | 
            ||
| 39 | private $responseChecker;  | 
            ||
| 40 | |||
| 41 | /** @var IriConverterInterface */  | 
            ||
| 42 | private $iriConverter;  | 
            ||
| 43 | |||
| 44 | /** @var SharedStorageInterface */  | 
            ||
| 45 | private $sharedStorage;  | 
            ||
| 46 | |||
| 47 | public function __construct(  | 
            ||
| 60 | |||
| 61 | /**  | 
            ||
| 62 | * @Given I am browsing archival shipping methods  | 
            ||
| 63 | */  | 
            ||
| 64 | public function iAmBrowsingArchivalShippingMethods(): void  | 
            ||
| 70 | |||
| 71 | /**  | 
            ||
| 72 | * @When I change my locale to :localeCode  | 
            ||
| 73 | */  | 
            ||
| 74 | public function iSwitchTheLocaleToTheLocale(string $localeCode): void  | 
            ||
| 84 | |||
| 85 | /**  | 
            ||
| 86 | * @When I am browsing shipping methods  | 
            ||
| 87 | * @When I want to browse shipping methods  | 
            ||
| 88 | * @When I browse shipping methods  | 
            ||
| 89 | */  | 
            ||
| 90 | public function iBrowseShippingMethods(): void  | 
            ||
| 94 | |||
| 95 | /**  | 
            ||
| 96 | * @When I (try to )delete shipping method :shippingMethod  | 
            ||
| 97 | */  | 
            ||
| 98 | public function iDeleteShippingMethod(ShippingMethodInterface $shippingMethod): void  | 
            ||
| 102 | |||
| 103 | /**  | 
            ||
| 104 | * @When I want to create a new shipping method  | 
            ||
| 105 | */  | 
            ||
| 106 | public function iWantToCreateANewShippingMethod(): void  | 
            ||
| 110 | |||
| 111 | /**  | 
            ||
| 112 | * @When I add it  | 
            ||
| 113 | * @When I try to add it  | 
            ||
| 114 | */  | 
            ||
| 115 | public function iAddIt(): void  | 
            ||
| 119 | |||
| 120 | /**  | 
            ||
| 121 | * @When I specify its code as :code  | 
            ||
| 122 | * @When I do not specify its code  | 
            ||
| 123 | */  | 
            ||
| 124 | public function iSpecifyItsCodeAs(?string $code = ''): void  | 
            ||
| 128 | |||
| 129 | /**  | 
            ||
| 130 | * @When I specify its position as :position  | 
            ||
| 131 | */  | 
            ||
| 132 | public function iSpecifyItsPositionAs(int $position): void  | 
            ||
| 136 | |||
| 137 | /**  | 
            ||
| 138 | * @When I name it :name in :localeCode  | 
            ||
| 139 | * @When I rename it to :name in :localeCode  | 
            ||
| 140 | * @When I do not name it  | 
            ||
| 141 | * @When I remove its name from :localeCode translation  | 
            ||
| 142 | */  | 
            ||
| 143 | public function iNameItIn(?string $name = '', ?string $localeCode = 'en_US'): void  | 
            ||
| 147 | |||
| 148 | /**  | 
            ||
| 149 | * @When I describe it as :description in :localeCode  | 
            ||
| 150 | */  | 
            ||
| 151 | public function iDescribeItAsIn(string $description, string $localeCode): void  | 
            ||
| 158 | |||
| 159 | /**  | 
            ||
| 160 | * @When /^I define it for the (zone named "[^"]+")$/  | 
            ||
| 161 | * @When I do not specify its zone  | 
            ||
| 162 | */  | 
            ||
| 163 | public function iDefineItForTheZone(ZoneInterface $zone = null): void  | 
            ||
| 169 | |||
| 170 | /**  | 
            ||
| 171 | * @When I disable it  | 
            ||
| 172 | */  | 
            ||
| 173 | public function iDisableIt(): void  | 
            ||
| 177 | |||
| 178 | /**  | 
            ||
| 179 | * @When I enable it  | 
            ||
| 180 | */  | 
            ||
| 181 | public function iEnableIt(): void  | 
            ||
| 185 | |||
| 186 | /**  | 
            ||
| 187 | * @When I make it available in channel :channel  | 
            ||
| 188 | */  | 
            ||
| 189 | public function iMakeItAvailableInChannel(ChannelInterface $channel): void  | 
            ||
| 193 | |||
| 194 | /**  | 
            ||
| 195 | * @When I choose :shippingCalculator calculator  | 
            ||
| 196 | */  | 
            ||
| 197 | public function iChooseCalculator(string $shippingCalculator): void  | 
            ||
| 201 | |||
| 202 | /**  | 
            ||
| 203 | * @When I archive the :shippingMethod shipping method  | 
            ||
| 204 | */  | 
            ||
| 205 | public function iArchiveTheShippingMethod(ShippingMethodInterface $shippingMethod): void  | 
            ||
| 210 | |||
| 211 | /**  | 
            ||
| 212 | * @When I restore the :shippingMethod shipping method  | 
            ||
| 213 | */  | 
            ||
| 214 | public function iRestoreTheShippingMethod(ShippingMethodInterface $shippingMethod): void  | 
            ||
| 219 | |||
| 220 | /**  | 
            ||
| 221 | * @When I specify its amount as :amount for :channel channel  | 
            ||
| 222 | */  | 
            ||
| 223 | public function iSpecifyItsAmountAsForChannel(ChannelInterface $channel, int $amount): void  | 
            ||
| 227 | |||
| 228 | /**  | 
            ||
| 229 | * @When I want to modify a shipping method :shippingMethod  | 
            ||
| 230 | * @When /^I want to modify (this shipping method)$/  | 
            ||
| 231 | */  | 
            ||
| 232 | public function iWantToModifyShippingMethod(ShippingMethodInterface $shippingMethod): void  | 
            ||
| 236 | |||
| 237 | /**  | 
            ||
| 238 | * @When I save my changes  | 
            ||
| 239 | * @When I try to save my changes  | 
            ||
| 240 | */  | 
            ||
| 241 | public function iSaveMyChanges(): void  | 
            ||
| 245 | |||
| 246 | /**  | 
            ||
| 247 | * @When I sort the shipping methods :sortType by code  | 
            ||
| 248 | * @When I switch the way shipping methods are sorted :sortType by code  | 
            ||
| 249 | */  | 
            ||
| 250 | public function iSortShippingMethodsByCode(string $sortType = 'ascending'): void  | 
            ||
| 254 | |||
| 255 | /**  | 
            ||
| 256 | * @When I sort the shipping methods :sortType by name  | 
            ||
| 257 | * @When I switch the way shipping methods are sorted :sortType by name  | 
            ||
| 258 | * @Given the shipping methods are already sorted :sortType by name  | 
            ||
| 259 | */  | 
            ||
| 260 | public function iSortShippingMethodsByName(string $sortType = 'ascending'): void  | 
            ||
| 267 | |||
| 268 | /**  | 
            ||
| 269 | * @When I switch the way shipping methods are sorted by code  | 
            ||
| 270 | */  | 
            ||
| 271 | public function iSwitchTheWayShippingMethodsAreSortedByCode(): void  | 
            ||
| 275 | |||
| 276 | /**  | 
            ||
| 277 | * @When I switch the way shipping methods are sorted by name  | 
            ||
| 278 | */  | 
            ||
| 279 | public function iSwitchTheWayShippingMethodsAreSortedByName(): void  | 
            ||
| 283 | |||
| 284 | /**  | 
            ||
| 285 | * @When I filter archival shipping methods  | 
            ||
| 286 | */  | 
            ||
| 287 | public function iFilterArchivalShippingMethods(): void  | 
            ||
| 292 | |||
| 293 | /**  | 
            ||
| 294 | * @Then I should see :count shipping methods in the list  | 
            ||
| 295 | */  | 
            ||
| 296 | public function iShouldSeeShippingMethodsInTheList(int $count): void  | 
            ||
| 300 | |||
| 301 | /**  | 
            ||
| 302 | * @Then the shipping method :name should be in the registry  | 
            ||
| 303 | * @Then the shipping method :name should appear in the registry  | 
            ||
| 304 | */  | 
            ||
| 305 | public function theShippingMethodShouldAppearInTheRegistry(string $name): void  | 
            ||
| 312 | |||
| 313 | /**  | 
            ||
| 314 | * @Then /^(this shipping method) should still be in the registry$/  | 
            ||
| 315 | */  | 
            ||
| 316 | public function thisShippingMethodShouldAppearInTheRegistry(ShippingMethodInterface $shippingMethod): void  | 
            ||
| 325 | |||
| 326 | /**  | 
            ||
| 327 | * @Then I should be notified that it has been successfully deleted  | 
            ||
| 328 | */  | 
            ||
| 329 | public function iShouldBeNotifiedThatItHasBeenSuccessfullyDeleted(): void  | 
            ||
| 336 | |||
| 337 | /**  | 
            ||
| 338 | * @Then /^(this shipping method) should no longer exist in the registry$/  | 
            ||
| 339 | */  | 
            ||
| 340 | public function thisShippingMethodShouldNoLongerExistInTheRegistry(ShippingMethodInterface $shippingMethod): void  | 
            ||
| 349 | |||
| 350 | /**  | 
            ||
| 351 | * @Then I should be notified that it has been successfully created  | 
            ||
| 352 | */  | 
            ||
| 353 | public function iShouldBeNotifiedThatItHasBeenSuccessfullyCreated(): void  | 
            ||
| 360 | |||
| 361 | /**  | 
            ||
| 362 | * @Then the shipping method :shippingMethod should be available in channel :channel  | 
            ||
| 363 | */  | 
            ||
| 364 | public function theShippingMethodShouldBeAvailableInChannel(ShippingMethodInterface $shippingMethod, ChannelInterface $channel): void  | 
            ||
| 375 | |||
| 376 | /**  | 
            ||
| 377 | * @Then /^(this shipping method) name should be "([^"]+)"$/  | 
            ||
| 378 | * @Then /^(this shipping method) should still be named "([^"]+)"$/  | 
            ||
| 379 | */  | 
            ||
| 380 | public function thisShippingMethodNameShouldBe(ShippingMethodInterface $shippingMethod, string $name): void  | 
            ||
| 392 | |||
| 393 | /**  | 
            ||
| 394 | * @Then /^(this shipping method) should be disabled$/  | 
            ||
| 395 | */  | 
            ||
| 396 | public function thisShippingMethodShouldBeDisabled(ShippingMethodInterface $shippingMethod): void  | 
            ||
| 407 | |||
| 408 | /**  | 
            ||
| 409 | * @Then /^(this shipping method) should be enabled$/  | 
            ||
| 410 | */  | 
            ||
| 411 | public function thisShippingMethodShouldBeEnabled(ShippingMethodInterface $shippingMethod): void  | 
            ||
| 422 | |||
| 423 | /**  | 
            ||
| 424 | * @Then I should not be able to edit its code  | 
            ||
| 425 | */  | 
            ||
| 426 | public function iShouldNotBeAbleToEditItsCode(): void  | 
            ||
| 435 | |||
| 436 | /**  | 
            ||
| 437 | * @Then I should be notified that it has been successfully edited  | 
            ||
| 438 | */  | 
            ||
| 439 | public function iShouldBeNotifiedThatItHasBeenSuccessfullyEdited(): void  | 
            ||
| 446 | |||
| 447 | /**  | 
            ||
| 448 | * @Then I should be notified that shipping method with this code already exists  | 
            ||
| 449 | */  | 
            ||
| 450 | public function iShouldBeNotifiedThatShippingMethodWithThisCodeAlreadyExists(): void  | 
            ||
| 462 | |||
| 463 | /**  | 
            ||
| 464 | * @Then there should still be only one shipping method with code :value  | 
            ||
| 465 | */  | 
            ||
| 466 | public function thereShouldStillBeOnlyOneShippingMethodWith(string $value): void  | 
            ||
| 474 | |||
| 475 | /**  | 
            ||
| 476 | * @Then the only shipping method on the list should be :name  | 
            ||
| 477 | */  | 
            ||
| 478 | public function theOnlyShippingMethodOnTheListShouldBe(string $name): void  | 
            ||
| 486 | |||
| 487 | /**  | 
            ||
| 488 | * @Then I should see :amount shipping methods on the list  | 
            ||
| 489 | */  | 
            ||
| 490 | public function iShouldSeeShippingMethodOnTheList(int $amount): void  | 
            ||
| 497 | |||
| 498 | /**  | 
            ||
| 499 | * @Then I should be notified that it is in use  | 
            ||
| 500 | */  | 
            ||
| 501 | public function iShouldBeNotifiedThatItIsInUse(): void  | 
            ||
| 508 | |||
| 509 | /**  | 
            ||
| 510 | * @Then I should be notified that :element is required  | 
            ||
| 511 | */  | 
            ||
| 512 | public function iShouldBeNotifiedThatElementIsRequired(string $element): void  | 
            ||
| 519 | |||
| 520 | /**  | 
            ||
| 521 | * @Then I should be notified that zone has to be selected  | 
            ||
| 522 | */  | 
            ||
| 523 | public function iShouldBeNotifiedThatZoneHasToBeSelected(): void  | 
            ||
| 530 | |||
| 531 | /**  | 
            ||
| 532 | * @Then shipping method with :element :value should not be added  | 
            ||
| 533 | */  | 
            ||
| 534 | public function theShippingMethodWithElementValueShouldNotBeAdded(string $element, string $value): void  | 
            ||
| 541 | |||
| 542 | /**  | 
            ||
| 543 | * @Then the first shipping method on the list should have code :value  | 
            ||
| 544 | */  | 
            ||
| 545 | public function theFirstProductOnTheListShouldHave(string $value): void  | 
            ||
| 551 | |||
| 552 | /**  | 
            ||
| 553 | * @Then the first shipping method on the list should have name :value  | 
            ||
| 554 | */  | 
            ||
| 555 | public function theFirstShippingMethodOnTheListShouldHave(string $value): void  | 
            ||
| 561 | |||
| 562 | /**  | 
            ||
| 563 | * @Then the last shipping method on the list should have name :value  | 
            ||
| 564 | */  | 
            ||
| 565 | public function theLastShippingMethodOnTheListShouldHave(string $value): void  | 
            ||
| 571 | |||
| 572 | /**  | 
            ||
| 573 | * @Then I should be viewing non archival shipping methods  | 
            ||
| 574 | */  | 
            ||
| 575 | public function iShouldBeViewingNonArchivalShippingMethods(): void  | 
            ||
| 579 | |||
| 580 | private function getAdminLocaleCode(): string  | 
            ||
| 589 | }  | 
            ||
| 590 | 
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: