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) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | public function isSlugReadonly($languageCode = 'en_US') |
||
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | public function removeImageWithType($type) |
||
| 137 | |||
| 138 | public function removeFirstImage() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | */ |
||
| 157 | public function enableSlugModification($languageCode = 'en_US') |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | public function countImages() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritdoc} |
||
| 177 | */ |
||
| 178 | public function changeImageWithType($type, $path) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * {@inheritdoc} |
||
| 188 | */ |
||
| 189 | public function modifyFirstImageType($type) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | public function getParent() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritdoc} |
||
| 207 | */ |
||
| 208 | public function getSlug($languageCode = 'en_US') |
||
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritdoc} |
||
| 215 | */ |
||
| 216 | public function getValidationMessageForImage() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | */ |
||
| 231 | public function getValidationMessageForImageAtPlace($place) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * {@inheritdoc} |
||
| 245 | */ |
||
| 246 | public function activateLanguageTab($locale) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritdoc} |
||
| 264 | */ |
||
| 265 | protected function getElement($name, array $parameters = []) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return NodeElement |
||
| 276 | */ |
||
| 277 | protected function getCodeElement() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * {@inheritdoc} |
||
| 284 | */ |
||
| 285 | protected function getDefinedElements() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return NodeElement |
||
| 301 | */ |
||
| 302 | private function getLastImageElement() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return NodeElement |
||
| 313 | */ |
||
| 314 | private function getFirstImageElement() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return NodeElement[] |
||
| 325 | */ |
||
| 326 | private function getImageElements() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $type |
||
| 335 | * |
||
| 336 | * @return NodeElement |
||
| 337 | */ |
||
| 338 | private function getImageElementByType($type) |
||
| 349 | |||
| 350 | private function provideImageUrlForType(string $type): ?string |
||
| 354 | |||
| 355 | private function saveImageUrlForType(string $type, string $imageUrl): void |
||
| 363 | } |
||
| 364 |
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: