Complex classes like UpdateSimpleProductPage 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 UpdateSimpleProductPage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class UpdateSimpleProductPage extends BaseUpdatePage implements UpdateSimpleProductPageInterface |
||
| 30 | { |
||
| 31 | use ChecksCodeImmutability; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | public function nameItIn($name, $localeCode) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | public function specifyPrice($channelName, $price) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | */ |
||
| 55 | public function specifyOriginalPrice($channelName, $originalPrice) |
||
| 59 | |||
| 60 | public function addSelectedAttributes() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritdoc} |
||
| 74 | */ |
||
| 75 | public function removeAttribute($attributeName, $localeCode) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | public function getAttributeValue($attribute, $localeCode) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * {@inheritdoc} |
||
| 95 | */ |
||
| 96 | public function getNumberOfAttributes() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * {@inheritdoc} |
||
| 103 | */ |
||
| 104 | public function hasAttribute($attributeName) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | public function selectMainTaxon(TaxonInterface $taxon) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | public function isMainTaxonChosen($taxonName) |
||
| 132 | |||
| 133 | public function disableTracking() |
||
| 137 | |||
| 138 | public function enableTracking() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | public function isTracked() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * {@inheritdoc} |
||
| 153 | */ |
||
| 154 | public function enableSlugModification($locale) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritdoc} |
||
| 161 | */ |
||
| 162 | public function isImageWithTypeDisplayed($type) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | public function attachImage($path, $type = null) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | public function changeImageWithType($type, $path) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * {@inheritdoc} |
||
| 210 | */ |
||
| 211 | public function removeImageWithType($type) |
||
| 218 | |||
| 219 | public function removeFirstImage() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * {@inheritdoc} |
||
| 229 | */ |
||
| 230 | public function modifyFirstImageType($type) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritdoc} |
||
| 240 | */ |
||
| 241 | public function countImages() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * {@inheritdoc} |
||
| 250 | */ |
||
| 251 | public function isSlugReadOnlyIn($locale) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritdoc} |
||
| 258 | */ |
||
| 259 | public function associateProducts(ProductAssociationTypeInterface $productAssociationType, array $productsNames) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * {@inheritdoc} |
||
| 288 | */ |
||
| 289 | public function hasAssociatedProduct($productName, ProductAssociationTypeInterface $productAssociationType) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * {@inheritdoc} |
||
| 301 | */ |
||
| 302 | public function removeAssociatedProduct($productName, ProductAssociationTypeInterface $productAssociationType) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * {@inheritdoc} |
||
| 318 | */ |
||
| 319 | public function getPricingConfigurationForChannelAndCurrencyCalculator(ChannelInterface $channel, CurrencyInterface $currency) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * {@inheritdoc} |
||
| 330 | */ |
||
| 331 | public function getSlug($locale) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * {@inheritdoc} |
||
| 340 | */ |
||
| 341 | public function specifySlugIn($slug, $locale) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * {@inheritdoc} |
||
| 350 | */ |
||
| 351 | public function activateLanguageTab($locale) |
||
| 362 | |||
| 363 | public function getPriceForChannel($channelName) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * {@inheritdoc} |
||
| 370 | */ |
||
| 371 | public function getOriginalPriceForChannel($channelName) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * {@inheritdoc} |
||
| 378 | */ |
||
| 379 | protected function getCodeElement() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * {@inheritdoc} |
||
| 386 | */ |
||
| 387 | protected function getElement($name, array $parameters = []) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * {@inheritdoc} |
||
| 398 | */ |
||
| 399 | protected function getDefinedElements() |
||
| 423 | |||
| 424 | private function openTaxonBookmarks() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param string $tabName |
||
| 431 | */ |
||
| 432 | private function clickTabIfItsNotActive($tabName) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @param string $tabName |
||
| 442 | */ |
||
| 443 | private function clickTab($tabName) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param string $localeCode |
||
| 451 | */ |
||
| 452 | private function clickLocaleTabIfItsNotActive($localeCode) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param string $type |
||
| 462 | * |
||
| 463 | * @return NodeElement |
||
| 464 | */ |
||
| 465 | private function getImageElementByType($type) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return NodeElement[] |
||
| 479 | */ |
||
| 480 | private function getImageElements() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @return NodeElement |
||
| 489 | */ |
||
| 490 | private function getLastImageElement() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @return NodeElement |
||
| 501 | */ |
||
| 502 | private function getFirstImageElement() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @param string $locale |
||
| 513 | */ |
||
| 514 | private function waitForSlugGenerationIfNecessary($locale) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @param NodeElement $imageElement |
||
| 533 | * @param string $type |
||
| 534 | */ |
||
| 535 | private function setImageType(NodeElement $imageElement, $type) |
||
| 540 | } |
||
| 541 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: