Complex classes like ManagingProductAttributesContext 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 ManagingProductAttributesContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | final class ManagingProductAttributesContext implements Context |
||
30 | { |
||
31 | /** |
||
32 | * @var CreatePageInterface |
||
33 | */ |
||
34 | private $createPage; |
||
35 | |||
36 | /** |
||
37 | * @var IndexPageInterface |
||
38 | */ |
||
39 | private $indexPage; |
||
40 | |||
41 | /** |
||
42 | * @var UpdatePageInterface |
||
43 | */ |
||
44 | private $updatePage; |
||
45 | |||
46 | /** |
||
47 | * @var CurrentPageResolverInterface |
||
48 | */ |
||
49 | private $currentPageResolver; |
||
50 | |||
51 | /** |
||
52 | * @var SharedSecurityServiceInterface |
||
53 | */ |
||
54 | private $sharedSecurityService; |
||
55 | |||
56 | /** |
||
57 | * @param CreatePageInterface $createPage |
||
58 | * @param IndexPageInterface $indexPage |
||
59 | * @param UpdatePageInterface $updatePage |
||
60 | * @param CurrentPageResolverInterface $currentPageResolver |
||
61 | * @param SharedSecurityServiceInterface $sharedSecurityService |
||
62 | */ |
||
63 | public function __construct( |
||
76 | |||
77 | /** |
||
78 | * @When I want to create a new :type product attribute |
||
79 | */ |
||
80 | public function iWantToCreateANewTextProductAttribute($type) |
||
84 | |||
85 | /** |
||
86 | * @When I specify its code as :code |
||
87 | * @When I do not specify its code |
||
88 | */ |
||
89 | public function iSpecifyItsCodeAs($code = null) |
||
93 | |||
94 | /** |
||
95 | * @When I name it :name in :language |
||
96 | */ |
||
97 | public function iSpecifyItsNameAs($name, $language) |
||
101 | |||
102 | /** |
||
103 | * @When I add it |
||
104 | * @When I try to add it |
||
105 | */ |
||
106 | public function iAddIt() |
||
110 | |||
111 | /** |
||
112 | * @When I( also) add value :value |
||
113 | */ |
||
114 | public function iAddValue(string $value): void |
||
121 | |||
122 | /** |
||
123 | * @When I delete value :value |
||
124 | */ |
||
125 | public function iDeleteValue(string $value): void |
||
129 | |||
130 | /** |
||
131 | * @When I change its value :oldValue to :newValue |
||
132 | */ |
||
133 | public function iChangeItsValueTo(string $oldValue, string $newValue): void |
||
137 | |||
138 | /** |
||
139 | * @Then I should see the product attribute :name in the list |
||
140 | */ |
||
141 | public function iShouldSeeTheProductAttributeInTheList($name) |
||
147 | |||
148 | /** |
||
149 | * @Then the :type attribute :name should appear in the store |
||
150 | */ |
||
151 | public function theAttributeShouldAppearInTheStore($type, $name) |
||
160 | |||
161 | /** |
||
162 | * @When /^I want to edit (this product attribute)$/ |
||
163 | */ |
||
164 | public function iWantToEditThisAttribute(ProductAttributeInterface $productAttribute) |
||
168 | |||
169 | /** |
||
170 | * @When I change its name to :name in :language |
||
171 | */ |
||
172 | public function iChangeItNameToIn($name, $language) |
||
176 | |||
177 | /** |
||
178 | * @When I save my changes |
||
179 | * @When I try to save my changes |
||
180 | */ |
||
181 | public function iSaveMyChanges() |
||
185 | |||
186 | /** |
||
187 | * @Then the code field should be disabled |
||
188 | */ |
||
189 | public function theCodeFieldShouldBeDisabled() |
||
193 | |||
194 | /** |
||
195 | * @Then the type field should be disabled |
||
196 | */ |
||
197 | public function theTypeFieldShouldBeDisabled() |
||
203 | |||
204 | /** |
||
205 | * @Then I should be notified that product attribute with this code already exists |
||
206 | */ |
||
207 | public function iShouldBeNotifiedThatProductAttributeWithThisCodeAlreadyExists() |
||
211 | |||
212 | /** |
||
213 | * @Then there should still be only one product attribute with code :code |
||
214 | */ |
||
215 | public function thereShouldStillBeOnlyOneProductAttributeWithCode($code) |
||
221 | |||
222 | /** |
||
223 | * @When I do not name it |
||
224 | */ |
||
225 | public function iDoNotNameIt() |
||
229 | |||
230 | /** |
||
231 | * @Then I should be notified that :element is required |
||
232 | */ |
||
233 | public function iShouldBeNotifiedThatIsRequired($element) |
||
237 | |||
238 | /** |
||
239 | * @Given the attribute with :elementName :elementValue should not appear in the store |
||
240 | */ |
||
241 | public function theAttributeWithCodeShouldNotAppearInTheStore($elementName, $elementValue) |
||
247 | |||
248 | /** |
||
249 | * @When I remove its name from :language translation |
||
250 | */ |
||
251 | public function iRemoveItsNameFromTranslation($language) |
||
255 | |||
256 | /** |
||
257 | * @When I want to see all product attributes in store |
||
258 | */ |
||
259 | public function iWantToSeeAllProductAttributesInStore() |
||
263 | |||
264 | /** |
||
265 | * @When /^(the administrator) changes (this product attribute)'s value "([^"]*)" to "([^"]*)"$/ |
||
266 | */ |
||
267 | public function theAdministratorChangesThisProductAttributesValueTo( |
||
282 | |||
283 | /** |
||
284 | * @When I specify its min length as :min |
||
285 | * @When I specify its min entries value as :min |
||
286 | */ |
||
287 | public function iSpecifyItsMinValueAs(int $min): void |
||
291 | |||
292 | /** |
||
293 | * @When I specify its max length as :max |
||
294 | * @When I specify its max entries value as :max |
||
295 | */ |
||
296 | public function iSpecifyItsMaxLengthAs(int $max): void |
||
300 | |||
301 | /** |
||
302 | * @When I check multiple option |
||
303 | */ |
||
304 | public function iCheckMultipleOption(): void |
||
308 | |||
309 | /** |
||
310 | * @When I do not check multiple option |
||
311 | */ |
||
312 | public function iDoNotCheckMultipleOption(): void |
||
316 | |||
317 | /** |
||
318 | * @When /^(the administrator) deletes the value "([^"]+)" from (this product attribute)$/ |
||
319 | */ |
||
320 | public function theAdministratorDeletesTheValueFromThisProductAttribute( |
||
334 | |||
335 | /** |
||
336 | * @Then /^I should see (\d+) product attributes in the list$/ |
||
337 | */ |
||
338 | public function iShouldSeeCustomersInTheList($amountOfProductAttributes) |
||
342 | |||
343 | /** |
||
344 | * @When /^I delete (this product attribute)$/ |
||
345 | */ |
||
346 | public function iDeleteThisProductAttribute(ProductAttributeInterface $productAttribute) |
||
351 | |||
352 | /** |
||
353 | * @Then /^(this product attribute) should no longer exist in the registry$/ |
||
354 | */ |
||
355 | public function thisProductAttributeShouldNoLongerExistInTheRegistry(ProductAttributeInterface $productAttribute) |
||
359 | |||
360 | /** |
||
361 | * @Then the first product attribute on the list should have name :name |
||
362 | */ |
||
363 | public function theFirstProductAttributeOnTheListShouldHave($name) |
||
369 | |||
370 | /** |
||
371 | * @Then the last product attribute on the list should have name :name |
||
372 | */ |
||
373 | public function theLastProductAttributeOnTheListShouldHave($name) |
||
379 | |||
380 | /** |
||
381 | * @Then /^(this product attribute) should have value "([^"]*)"/ |
||
382 | */ |
||
383 | public function theSelectAttributeShouldHaveValue(ProductAttributeInterface $productAttribute, string $value): void |
||
389 | |||
390 | /** |
||
391 | * @Then I should be notified that max length must be greater or equal to the min length |
||
392 | */ |
||
393 | public function iShouldBeNotifiedThatMaxLengthMustBeGreaterOrEqualToTheMinLength(): void |
||
399 | |||
400 | /** |
||
401 | * @Then I should be notified that max entries value must be greater or equal to the min entries value |
||
402 | */ |
||
403 | public function iShouldBeNotifiedThatMaxEntriesValueMustBeGreaterOrEqualToTheMinEntriesValue(): void |
||
409 | |||
410 | /** |
||
411 | * @Then I should be notified that min entries value must be lower or equal to the number of added choices |
||
412 | */ |
||
413 | public function iShouldBeNotifiedThatMinEntriesValueMustBeLowerOrEqualToTheNumberOfAddedChoices(): void |
||
419 | |||
420 | /** |
||
421 | * @Then I should be notified that multiple must be true if min or max entries values are specified |
||
422 | */ |
||
423 | public function iShouldBeNotifiedThatMultipleMustBeTrueIfMinOrMaxEntriesValuesAreSpecified(): void |
||
429 | |||
430 | /** |
||
431 | * @Then /^(this product attribute) should not have value "([^"]*)"/ |
||
432 | */ |
||
433 | public function theSelectAttributeShouldNotHaveValue(ProductAttributeInterface $productAttribute, string $value): void |
||
439 | |||
440 | /** |
||
441 | * @param string $element |
||
442 | * @param string $expectedMessage |
||
443 | * |
||
444 | * @throws \InvalidArgumentException |
||
445 | */ |
||
446 | private function assertFieldValidationMessage(string $element, string $expectedMessage): void |
||
453 | |||
454 | /** |
||
455 | * @param string $expectedMessage |
||
456 | * |
||
457 | * @throws \InvalidArgumentException |
||
458 | */ |
||
459 | private function assertValidationMessage(string $expectedMessage): void |
||
466 | } |
||
467 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: