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 |
||
| 32 | class MetadataContext extends DefaultContext |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var PropertyAccessor |
||
| 36 | */ |
||
| 37 | private $propertyAccessor; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | public function __construct($applicationName = null) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @When I am customizing metadata |
||
| 51 | * @When I am customizing metadata with identifier :identifier |
||
| 52 | */ |
||
| 53 | public function iAmCustomizingMetadata($identifier = 'FooBar') |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @Then I should see metadata customization form |
||
| 60 | */ |
||
| 61 | public function iShouldSeeMetadataCustomizationForm() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @Then I should see the following metadata: |
||
| 71 | */ |
||
| 72 | public function iShouldSeeTheFollowingMetadata(TableNode $metadata) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @Then I should be customizing default metadata |
||
| 119 | */ |
||
| 120 | public function iShouldBeCustomizingDefaultMetadata() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @Then I should be customizing products metadata |
||
| 130 | */ |
||
| 131 | public function iShouldBeCustomizingProductsMetadata() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @Then I should be customizing specific product metadata |
||
| 141 | */ |
||
| 142 | public function iShouldBeCustomizingSpecificProductMetadata() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @Then I should be customizing specific product variant metadata |
||
| 152 | */ |
||
| 153 | public function iShouldBeCustomizingSpecificProductVariantMetadata() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @Then I should see Twitter's application card form |
||
| 163 | */ |
||
| 164 | public function iShouldSeeTwitterApplicationCardForm() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @Then I should not see Twitter's application card form |
||
| 172 | */ |
||
| 173 | public function iShouldNotSeeTwitterApplicationCardForm() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @Given there is the following metadata :metadataName: |
||
| 181 | */ |
||
| 182 | public function thereIsTheFollowingMetadata($metadataName, TableNode $table) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @Given product :productName has the following page metadata: |
||
| 211 | */ |
||
| 212 | public function productHasTheFollowingPageMetadata($productName, TableNode $table) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @Then I should see :title as page title |
||
| 222 | */ |
||
| 223 | public function iShouldSeeAsPageTitle($title) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @Then the page keywords should contain :keyword |
||
| 230 | */ |
||
| 231 | public function thePageKeywordsShouldContain($keyword) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @Then there should be Twitter summary card metadata on this page |
||
| 238 | */ |
||
| 239 | public function thereShouldBeTwitterSummaryCardMetadataOnThisPage() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @Then Twitter site should be :site |
||
| 246 | */ |
||
| 247 | public function twitterSiteShouldBe($site) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @Then Twitter image should be :image |
||
| 254 | */ |
||
| 255 | public function twitterImageShouldBe($image) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @When /I deselect "([^"]+)"/ |
||
| 262 | */ |
||
| 263 | public function iDeselectSelectField($fieldName) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param ElementInterface $element |
||
| 270 | * @param string $regexp |
||
| 271 | * |
||
| 272 | * @throws \Exception If assertion failed |
||
| 273 | */ |
||
| 274 | private function assertItIsMetadataCustomizationPage(ElementInterface $element, $regexp) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param ElementInterface $element |
||
| 283 | * @param string $regexp |
||
| 284 | * |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | private function isItMetadataCustomizationPage(ElementInterface $element, $regexp) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param ElementInterface $element |
||
| 308 | * @param array $fields |
||
| 309 | * |
||
| 310 | * @throws \Exception If assertion failed |
||
| 311 | */ |
||
| 312 | private function assertThereIsFormWithFields(ElementInterface $element, array $fields) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param ElementInterface $element |
||
| 321 | * @param string[] $fields |
||
| 322 | * |
||
| 323 | * @return ElementInterface|null |
||
| 324 | */ |
||
| 325 | private function getFormWithFields(ElementInterface $element, array $fields) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param string $value |
||
| 348 | * |
||
| 349 | * @return CardInterface |
||
| 350 | */ |
||
| 351 | protected function createTwitterCardFromString($value) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param PageMetadataInterface $pageMetadata |
||
| 376 | * @param string $key |
||
| 377 | * @param string $value |
||
| 378 | * |
||
| 379 | * @return bool True if created new metadata object |
||
| 380 | */ |
||
| 381 | protected function createNewMetadataObjectIfNeeded(PageMetadataInterface $pageMetadata, $key, $value) |
||
| 391 | } |
||
| 392 |