Complex classes like Media 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 Media, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | abstract class Media implements MediaInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $name; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $description; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | protected $enabled = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $providerName; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | protected $providerStatus; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $providerReference; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $providerMetadata = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | protected $width; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var int |
||
| 63 | */ |
||
| 64 | protected $height; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var float |
||
| 68 | */ |
||
| 69 | protected $length; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $copyright; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $authorName; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $context; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | protected $cdnIsFlushable; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | protected $cdnFlushIdentifier; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var \DateTime |
||
| 98 | */ |
||
| 99 | protected $cdnFlushAt; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var int |
||
| 103 | */ |
||
| 104 | protected $cdnStatus; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var \DateTime |
||
| 108 | */ |
||
| 109 | protected $updatedAt; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var \DateTime |
||
| 113 | */ |
||
| 114 | protected $createdAt; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var mixed |
||
| 118 | */ |
||
| 119 | protected $binaryContent; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | protected $previousProviderReference; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var string |
||
| 128 | */ |
||
| 129 | protected $contentType; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var int |
||
| 133 | */ |
||
| 134 | protected $size; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var GalleryHasMediaInterface[] |
||
| 138 | */ |
||
| 139 | protected $galleryHasMedias; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var CategoryInterface |
||
| 143 | */ |
||
| 144 | protected $category; |
||
| 145 | |||
| 146 | public function prePersist() |
||
| 147 | { |
||
| 148 | $this->setCreatedAt(new \DateTime()); |
||
| 149 | $this->setUpdatedAt(new \DateTime()); |
||
| 150 | } |
||
| 151 | |||
| 152 | public function preUpdate() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @static |
||
| 159 | * |
||
| 160 | * @return string[] |
||
| 161 | */ |
||
| 162 | public static function getStatusList() |
||
| 163 | { |
||
| 164 | return array( |
||
| 165 | self::STATUS_OK => 'ok', |
||
| 166 | self::STATUS_SENDING => 'sending', |
||
| 167 | self::STATUS_PENDING => 'pending', |
||
| 168 | self::STATUS_ERROR => 'error', |
||
| 169 | self::STATUS_ENCODING => 'encoding', |
||
| 170 | ); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * {@inheritdoc} |
||
| 175 | */ |
||
| 176 | public function setBinaryContent($binaryContent) |
||
| 177 | { |
||
| 178 | $this->previousProviderReference = $this->providerReference; |
||
| 179 | $this->providerReference = null; |
||
| 180 | $this->binaryContent = $binaryContent; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritdoc} |
||
| 185 | */ |
||
| 186 | public function resetBinaryContent() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritdoc} |
||
| 193 | */ |
||
| 194 | public function getBinaryContent() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritdoc} |
||
| 201 | */ |
||
| 202 | public function getMetadataValue($name, $default = null) |
||
| 203 | { |
||
| 204 | $metadata = $this->getProviderMetadata(); |
||
| 205 | |||
| 206 | return isset($metadata[$name]) ? $metadata[$name] : $default; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | public function setMetadataValue($name, $value) |
||
| 213 | { |
||
| 214 | $metadata = $this->getProviderMetadata(); |
||
| 215 | $metadata[$name] = $value; |
||
| 216 | $this->setProviderMetadata($metadata); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * {@inheritdoc} |
||
| 221 | */ |
||
| 222 | public function unsetMetadataValue($name) |
||
| 223 | { |
||
| 224 | $metadata = $this->getProviderMetadata(); |
||
| 225 | unset($metadata[$name]); |
||
| 226 | $this->setProviderMetadata($metadata); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * {@inheritdoc} |
||
| 231 | */ |
||
| 232 | public function setName($name) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | */ |
||
| 240 | public function getName() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * {@inheritdoc} |
||
| 247 | */ |
||
| 248 | public function setDescription($description) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | public function getDescription() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritdoc} |
||
| 263 | */ |
||
| 264 | public function setEnabled($enabled) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritdoc} |
||
| 271 | */ |
||
| 272 | public function getEnabled() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * {@inheritdoc} |
||
| 279 | */ |
||
| 280 | public function setProviderName($providerName) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | public function getProviderName() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritdoc} |
||
| 295 | */ |
||
| 296 | public function setProviderStatus($providerStatus) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * {@inheritdoc} |
||
| 303 | */ |
||
| 304 | public function getProviderStatus() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritdoc} |
||
| 311 | */ |
||
| 312 | public function setProviderReference($providerReference) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * {@inheritdoc} |
||
| 319 | */ |
||
| 320 | public function getProviderReference() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * {@inheritdoc} |
||
| 327 | */ |
||
| 328 | public function setProviderMetadata(array $providerMetadata = array()) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * {@inheritdoc} |
||
| 335 | */ |
||
| 336 | public function getProviderMetadata() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * {@inheritdoc} |
||
| 343 | */ |
||
| 344 | public function setWidth($width) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * {@inheritdoc} |
||
| 351 | */ |
||
| 352 | public function getWidth() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * {@inheritdoc} |
||
| 359 | */ |
||
| 360 | public function setHeight($height) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * {@inheritdoc} |
||
| 367 | */ |
||
| 368 | public function getHeight() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * {@inheritdoc} |
||
| 375 | */ |
||
| 376 | public function setLength($length) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * {@inheritdoc} |
||
| 383 | */ |
||
| 384 | public function getLength() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * {@inheritdoc} |
||
| 391 | */ |
||
| 392 | public function setCopyright($copyright) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * {@inheritdoc} |
||
| 399 | */ |
||
| 400 | public function getCopyright() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * {@inheritdoc} |
||
| 407 | */ |
||
| 408 | public function setAuthorName($authorName) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * {@inheritdoc} |
||
| 415 | */ |
||
| 416 | public function getAuthorName() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * {@inheritdoc} |
||
| 423 | */ |
||
| 424 | public function setContext($context) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * {@inheritdoc} |
||
| 431 | */ |
||
| 432 | public function getContext() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * {@inheritdoc} |
||
| 439 | */ |
||
| 440 | public function setCdnIsFlushable($cdnIsFlushable) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * {@inheritdoc} |
||
| 447 | */ |
||
| 448 | public function getCdnIsFlushable() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * {@inheritdoc} |
||
| 455 | */ |
||
| 456 | public function setCdnFlushIdentifier($cdnFlushIdentifier) |
||
| 457 | { |
||
| 458 | $this->cdnFlushIdentifier = $cdnFlushIdentifier; |
||
|
|
|||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * {@inheritdoc} |
||
| 463 | */ |
||
| 464 | public function getCdnFlushIdentifier() |
||
| 465 | { |
||
| 466 | return $this->cdnFlushIdentifier; |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * {@inheritdoc} |
||
| 471 | */ |
||
| 472 | public function setCdnFlushAt(\DateTime $cdnFlushAt = null) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * {@inheritdoc} |
||
| 479 | */ |
||
| 480 | public function getCdnFlushAt() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * {@inheritdoc} |
||
| 487 | */ |
||
| 488 | public function setUpdatedAt(\DateTime $updatedAt = null) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * {@inheritdoc} |
||
| 495 | */ |
||
| 496 | public function getUpdatedAt() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * {@inheritdoc} |
||
| 503 | */ |
||
| 504 | public function setCreatedAt(\DateTime $createdAt = null) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * {@inheritdoc} |
||
| 511 | */ |
||
| 512 | public function getCreatedAt() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * {@inheritdoc} |
||
| 519 | */ |
||
| 520 | public function setContentType($contentType) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * {@inheritdoc} |
||
| 527 | */ |
||
| 528 | public function getContentType() |
||
| 532 | |||
| 533 | /** |
||
| 534 | * {@inheritdoc} |
||
| 535 | */ |
||
| 536 | public function getExtension() |
||
| 537 | { |
||
| 538 | // strips off query strings or hashes, which are common in URIs remote references |
||
| 539 | return preg_replace('{(\?|#).*}', '', pathinfo($this->getProviderReference(), PATHINFO_EXTENSION)); |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * {@inheritdoc} |
||
| 544 | */ |
||
| 545 | public function setSize($size) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * {@inheritdoc} |
||
| 552 | */ |
||
| 553 | public function getSize() |
||
| 557 | |||
| 558 | /** |
||
| 559 | * {@inheritdoc} |
||
| 560 | */ |
||
| 561 | public function setCdnStatus($cdnStatus) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * {@inheritdoc} |
||
| 568 | */ |
||
| 569 | public function getCdnStatus() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * {@inheritdoc} |
||
| 576 | */ |
||
| 577 | public function getBox() |
||
| 581 | |||
| 582 | /** |
||
| 583 | * {@inheritdoc} |
||
| 584 | */ |
||
| 585 | public function __toString() |
||
| 589 | |||
| 590 | /** |
||
| 591 | * {@inheritdoc} |
||
| 592 | */ |
||
| 593 | public function setGalleryHasMedias($galleryHasMedias) |
||
| 597 | |||
| 598 | /** |
||
| 599 | * {@inheritdoc} |
||
| 600 | */ |
||
| 601 | public function getGalleryHasMedias() |
||
| 605 | |||
| 606 | /** |
||
| 607 | * {@inheritdoc} |
||
| 608 | */ |
||
| 609 | public function getPreviousProviderReference() |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @param ExecutionContextInterface|LegacyExecutionContextInterface $context |
||
| 616 | */ |
||
| 617 | public function isStatusErroneous($context) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @return CategoryInterface |
||
| 637 | */ |
||
| 638 | public function getCategory() |
||
| 642 | |||
| 643 | /** |
||
| 644 | * @param CategoryInterface $category|null |
||
| 645 | */ |
||
| 646 | public function setCategory(CategoryInterface $category = null) |
||
| 650 | } |
||
| 651 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.