Complex classes like MetadataContext 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 MetadataContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class MetadataContext extends DefaultContext |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var PropertyAccessor |
||
| 37 | */ |
||
| 38 | private $propertyAccessor; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | public function __construct($applicationName = null) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @When I am customizing metadata |
||
| 52 | * @When I am customizing metadata with identifier :identifier |
||
| 53 | */ |
||
| 54 | public function iAmCustomizingMetadata($identifier = 'FooBar') |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @Then I should see metadata customization form |
||
| 61 | */ |
||
| 62 | public function iShouldSeeMetadataCustomizationForm() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @Then I should see the following metadata: |
||
| 72 | */ |
||
| 73 | public function iShouldSeeTheFollowingMetadata(TableNode $metadata) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @Then I should be customizing default metadata |
||
| 120 | */ |
||
| 121 | public function iShouldBeCustomizingDefaultMetadata() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @Then I should be customizing products metadata |
||
| 131 | */ |
||
| 132 | public function iShouldBeCustomizingProductsMetadata() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @Then I should be customizing specific product metadata |
||
| 142 | */ |
||
| 143 | public function iShouldBeCustomizingSpecificProductMetadata() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @Then I should be customizing specific product variant metadata |
||
| 153 | */ |
||
| 154 | public function iShouldBeCustomizingSpecificProductVariantMetadata() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @Then I should see Twitter's application card form |
||
| 164 | */ |
||
| 165 | public function iShouldSeeTwitterApplicationCardForm() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @Then I should not see Twitter's application card form |
||
| 173 | */ |
||
| 174 | public function iShouldNotSeeTwitterApplicationCardForm() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @Given there is the following metadata :metadataName: |
||
| 182 | */ |
||
| 183 | public function thereIsTheFollowingMetadata($metadataName, TableNode $table) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @Given product :productName has the following page metadata: |
||
| 212 | */ |
||
| 213 | public function productHasTheFollowingPageMetadata($productName, TableNode $table) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @Then I should see :title as page title |
||
| 223 | */ |
||
| 224 | public function iShouldSeeAsPageTitle($title) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @Then the page keywords should contain :keyword |
||
| 231 | */ |
||
| 232 | public function thePageKeywordsShouldContain($keyword) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @Then there should be Twitter summary card metadata on this page |
||
| 239 | */ |
||
| 240 | public function thereShouldBeTwitterSummaryCardMetadataOnThisPage() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @Then Twitter site should be :site |
||
| 247 | */ |
||
| 248 | public function twitterSiteShouldBe($site) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @Then Twitter image should be :image |
||
| 255 | */ |
||
| 256 | public function twitterImageShouldBe($image) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @When /I deselect "([^"]+)"/ |
||
| 263 | */ |
||
| 264 | public function iDeselectSelectField($fieldName) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param ElementInterface $element |
||
| 271 | * @param string $regexp |
||
| 272 | * |
||
| 273 | * @throws \Exception If assertion failed |
||
| 274 | */ |
||
| 275 | private function assertItIsMetadataCustomizationPage(ElementInterface $element, $regexp) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param ElementInterface $element |
||
| 285 | * @param string $regexp |
||
| 286 | * |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | private function isItMetadataCustomizationPage(ElementInterface $element, $regexp) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param ElementInterface $element |
||
| 310 | * @param array $fields |
||
| 311 | * |
||
| 312 | * @throws \Exception If assertion failed |
||
| 313 | */ |
||
| 314 | private function assertThereIsFormWithFields(ElementInterface $element, array $fields) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param ElementInterface $element |
||
| 323 | * @param string[] $fields |
||
| 324 | * |
||
| 325 | * @return ElementInterface|null |
||
| 326 | */ |
||
| 327 | private function getFormWithFields(ElementInterface $element, array $fields) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param string $value |
||
| 350 | * |
||
| 351 | * @return CardInterface |
||
| 352 | */ |
||
| 353 | protected function createTwitterCardFromString($value) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param PageMetadataInterface $pageMetadata |
||
| 378 | * @param string $key |
||
| 379 | * @param string $value |
||
| 380 | * |
||
| 381 | * @return boolean True if created new metadata object |
||
| 382 | */ |
||
| 383 | protected function createNewMetadataObjectIfNeeded(PageMetadataInterface $pageMetadata, $key, $value) |
||
| 393 | } |
||
| 394 |