Complex classes like UpdatePage 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 UpdatePage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class UpdatePage extends BaseUpdatePage implements UpdatePageInterface |
||
| 27 | { |
||
| 28 | use ChecksCodeImmutability; |
||
| 29 | |||
| 30 | /** @var array */ |
||
| 31 | private $imageUrls = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | public function chooseParent(TaxonInterface $taxon) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | public function describeItAs($description, $languageCode) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * {@inheritdoc} |
||
| 51 | */ |
||
| 52 | public function nameIt($name, $languageCode) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | public function specifySlug($slug, $languageCode) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | public function attachImage($path, $type = null) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | public function isImageWithTypeDisplayed($type) |
||
| 94 | { |
||
| 95 | $imageElement = $this->getImageElementByType($type); |
||
| 96 | |||
| 97 | $imageUrl = $imageElement ? $imageElement->find('css', 'img')->getAttribute('src') : $this->provideImageUrlForType($type); |
||
| 98 | if (null === $imageElement && null === $imageUrl) { |
||
| 99 | return false; |
||
| 100 | } |
||
| 101 | |||
| 102 | $this->getDriver()->visit($imageUrl); |
||
| 103 | $pageText = $this->getDocument()->getText(); |
||
| 104 | $this->getDriver()->back(); |
||
| 105 | |||
| 106 | return false === stripos($pageText, '404 Not Found'); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | public function isSlugReadonly($languageCode = 'en_US') |
||
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritdoc} |
||
| 122 | */ |
||
| 123 | public function removeImageWithType($type) |
||
| 133 | |||
| 134 | public function removeFirstImage() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | */ |
||
| 153 | public function enableSlugModification($languageCode = 'en_US') |
||
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | public function countImages() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritdoc} |
||
| 173 | */ |
||
| 174 | public function changeImageWithType($type, $path) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritdoc} |
||
| 184 | */ |
||
| 185 | public function modifyFirstImageType($type) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritdoc} |
||
| 195 | */ |
||
| 196 | public function getParent() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | */ |
||
| 204 | public function getSlug($languageCode = 'en_US') |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | public function getValidationMessageForImage() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | public function getValidationMessageForImageAtPlace($place) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | */ |
||
| 242 | public function activateLanguageTab($locale) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | */ |
||
| 261 | protected function getElement($name, array $parameters = []) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return NodeElement |
||
| 272 | */ |
||
| 273 | protected function getCodeElement() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * {@inheritdoc} |
||
| 280 | */ |
||
| 281 | protected function getDefinedElements() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return NodeElement |
||
| 297 | */ |
||
| 298 | private function getLastImageElement() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @return NodeElement |
||
| 309 | */ |
||
| 310 | private function getFirstImageElement() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return NodeElement[] |
||
| 321 | */ |
||
| 322 | private function getImageElements() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param string $type |
||
| 331 | * |
||
| 332 | * @return NodeElement |
||
| 333 | */ |
||
| 334 | private function getImageElementByType($type) |
||
| 345 | |||
| 346 | private function provideImageUrlForType(string $type): ?string |
||
| 350 | |||
| 351 | private function saveImageUrlForType(string $type, string $imageUrl): void |
||
| 359 | } |
||
| 360 |
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: