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 |
||
| 29 | class UpdatePage extends BaseUpdatePage implements UpdatePageInterface |
||
| 30 | { |
||
| 31 | use ChecksCodeImmutability; |
||
| 32 | |||
| 33 | /** @var array */ |
||
| 34 | private $imageUrls = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | */ |
||
| 39 | public function chooseParent(TaxonInterface $taxon) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | public function describeItAs($description, $languageCode) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | */ |
||
| 55 | public function nameIt($name, $languageCode) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | public function specifySlug($slug, $languageCode) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | public function attachImage($path, $type = null) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * {@inheritdoc} |
||
| 95 | */ |
||
| 96 | public function isImageWithTypeDisplayed($type) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | */ |
||
| 115 | public function isSlugReadonly($languageCode = 'en_US') |
||
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | public function removeImageWithType($type) |
||
| 136 | |||
| 137 | public function removeFirstImage() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | public function enableSlugModification($languageCode = 'en_US') |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | public function countImages() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | public function changeImageWithType($type, $path) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | */ |
||
| 188 | public function modifyFirstImageType($type) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | public function getParent() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | public function getSlug($languageCode = 'en_US') |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | */ |
||
| 215 | public function getValidationMessageForImage() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * {@inheritdoc} |
||
| 229 | */ |
||
| 230 | public function getValidationMessageForImageAtPlace($place) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * {@inheritdoc} |
||
| 244 | */ |
||
| 245 | public function activateLanguageTab($locale) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritdoc} |
||
| 263 | */ |
||
| 264 | protected function getElement($name, array $parameters = []) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return NodeElement |
||
| 275 | */ |
||
| 276 | protected function getCodeElement() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * {@inheritdoc} |
||
| 283 | */ |
||
| 284 | protected function getDefinedElements() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @return NodeElement |
||
| 300 | */ |
||
| 301 | private function getLastImageElement() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @return NodeElement |
||
| 312 | */ |
||
| 313 | private function getFirstImageElement() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return NodeElement[] |
||
| 324 | */ |
||
| 325 | private function getImageElements() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $type |
||
| 334 | * |
||
| 335 | * @return NodeElement |
||
| 336 | */ |
||
| 337 | private function getImageElementByType($type) |
||
| 348 | |||
| 349 | private function provideImageUrlForType(string $type): ?string |
||
| 353 | |||
| 354 | private function saveImageUrlForType(string $type, string $imageUrl): void |
||
| 358 | } |
||
| 359 |
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: