Complex classes like BaseProvider 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 BaseProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | abstract class BaseProvider implements MediaProviderInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $formats = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string[] |
||
| 33 | */ |
||
| 34 | protected $templates = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var ResizerInterface |
||
| 38 | */ |
||
| 39 | protected $resizer; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Filesystem |
||
| 43 | */ |
||
| 44 | protected $filesystem; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var GeneratorInterface |
||
| 48 | */ |
||
| 49 | protected $pathGenerator; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var CDNInterface |
||
| 53 | */ |
||
| 54 | protected $cdn; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var ThumbnailInterface |
||
| 58 | */ |
||
| 59 | protected $thumbnail; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $name; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var MediaInterface[] |
||
| 68 | */ |
||
| 69 | private $clones = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param string $name |
||
| 73 | */ |
||
| 74 | public function __construct($name, Filesystem $filesystem, CDNInterface $cdn, GeneratorInterface $pathGenerator, ThumbnailInterface $thumbnail) |
||
| 82 | |||
| 83 | final public function transform(MediaInterface $media): void |
||
| 92 | |||
| 93 | public function flushCdn(MediaInterface $media): void |
||
| 111 | |||
| 112 | public function addFormat($name, $format): void |
||
| 116 | |||
| 117 | public function getFormat($name) |
||
| 121 | |||
| 122 | public function requireThumbnails() |
||
| 126 | |||
| 127 | public function generateThumbnails(MediaInterface $media): void |
||
| 131 | |||
| 132 | public function removeThumbnails(MediaInterface $media, $formats = null): void |
||
| 136 | |||
| 137 | public function getFormatName(MediaInterface $media, $format) |
||
| 154 | |||
| 155 | public function getProviderMetadata() |
||
| 159 | |||
| 160 | public function preRemove(MediaInterface $media): void |
||
| 169 | |||
| 170 | public function postRemove(MediaInterface $media): void |
||
| 185 | |||
| 186 | public function generatePath(MediaInterface $media) |
||
| 190 | |||
| 191 | public function getFormats() |
||
| 195 | |||
| 196 | public function setName($name): void |
||
| 200 | |||
| 201 | public function getName() |
||
| 205 | |||
| 206 | public function setTemplates(array $templates): void |
||
| 210 | |||
| 211 | public function getTemplates() |
||
| 215 | |||
| 216 | public function getTemplate($name) |
||
| 220 | |||
| 221 | public function getResizer() |
||
| 225 | |||
| 226 | public function getFilesystem() |
||
| 230 | |||
| 231 | public function getCdn() |
||
| 235 | |||
| 236 | public function getCdnPath($relativePath, $isFlushable) |
||
| 240 | |||
| 241 | public function setResizer(ResizerInterface $resizer): void |
||
| 245 | |||
| 246 | public function prePersist(MediaInterface $media): void |
||
| 251 | |||
| 252 | public function preUpdate(MediaInterface $media): void |
||
| 256 | |||
| 257 | public function validate(ErrorElement $errorElement, MediaInterface $media): void |
||
| 260 | |||
| 261 | abstract protected function doTransform(MediaInterface $media); |
||
| 262 | } |
||
| 263 |