Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Generator 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 Generator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class Generator { |
||
| 40 | |||
| 41 | /** @var IPreview */ |
||
| 42 | private $previewManager; |
||
| 43 | /** @var IConfig */ |
||
| 44 | private $config; |
||
| 45 | /** @var IAppData */ |
||
| 46 | private $appData; |
||
| 47 | /** @var GeneratorHelper */ |
||
| 48 | private $helper; |
||
| 49 | /** @var EventDispatcherInterface */ |
||
| 50 | private $eventDispatcher; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param IConfig $config |
||
| 54 | * @param IPreview $previewManager |
||
| 55 | * @param IAppData $appData |
||
| 56 | * @param GeneratorHelper $helper |
||
| 57 | * @param EventDispatcherInterface $eventDispatcher |
||
| 58 | */ |
||
| 59 | View Code Duplication | public function __construct( |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Returns a preview of a file |
||
| 75 | * |
||
| 76 | * The cache is searched first and if nothing usable was found then a preview is |
||
| 77 | * generated by one of the providers |
||
| 78 | * |
||
| 79 | * @param File $file |
||
| 80 | * @param int $width |
||
| 81 | * @param int $height |
||
| 82 | * @param bool $crop |
||
| 83 | * @param string $mode |
||
| 84 | * @param string $mimeType |
||
| 85 | * @return ISimpleFile |
||
| 86 | * @throws NotFoundException |
||
| 87 | * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
||
| 88 | */ |
||
| 89 | public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
||
| 90 | $this->eventDispatcher->dispatch( |
||
| 91 | IPreview::EVENT, |
||
| 92 | new GenericEvent($file,[ |
||
| 93 | 'width' => $width, |
||
| 94 | 'height' => $height, |
||
| 95 | 'crop' => $crop, |
||
| 96 | 'mode' => $mode |
||
| 97 | ]) |
||
| 98 | ); |
||
| 99 | |||
| 100 | if ($mimeType === null) { |
||
| 101 | $mimeType = $file->getMimeType(); |
||
| 102 | } |
||
| 103 | if (!$this->previewManager->isMimeSupported($mimeType)) { |
||
| 104 | throw new NotFoundException(); |
||
| 105 | } |
||
| 106 | |||
| 107 | $previewFolder = $this->getPreviewFolder($file); |
||
| 108 | |||
| 109 | // Get the max preview and infer the max preview sizes from that |
||
| 110 | $maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType); |
||
| 111 | list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview); |
||
| 112 | |||
| 113 | // If both width and heigth are -1 we just want the max preview |
||
| 114 | if ($width === -1 && $height === -1) { |
||
| 115 | $width = $maxWidth; |
||
| 116 | $height = $maxHeight; |
||
| 117 | } |
||
| 118 | |||
| 119 | // Calculate the preview size |
||
| 120 | list($width, $height) = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight); |
||
| 121 | |||
| 122 | // No need to generate a preview that is just the max preview |
||
| 123 | if ($width === $maxWidth && $height === $maxHeight) { |
||
| 124 | return $maxPreview; |
||
| 125 | } |
||
| 126 | |||
| 127 | // Try to get a cached preview. Else generate (and store) one |
||
| 128 | try { |
||
| 129 | $file = $this->getCachedPreview($previewFolder, $width, $height, $crop); |
||
| 130 | } catch (NotFoundException $e) { |
||
| 131 | $file = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight); |
||
| 132 | } |
||
| 133 | |||
| 134 | return $file; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param ISimpleFolder $previewFolder |
||
| 139 | * @param File $file |
||
| 140 | * @param string $mimeType |
||
| 141 | * @return ISimpleFile |
||
| 142 | * @throws NotFoundException |
||
| 143 | */ |
||
| 144 | private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param ISimpleFile $file |
||
| 191 | * @return int[] |
||
| 192 | */ |
||
| 193 | private function getPreviewSize(ISimpleFile $file) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param int $width |
||
| 200 | * @param int $height |
||
| 201 | * @param bool $crop |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | private function generatePath($width, $height, $crop) { |
||
| 212 | |||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * @param int $width |
||
| 217 | * @param int $height |
||
| 218 | * @param bool $crop |
||
| 219 | * @param string $mode |
||
| 220 | * @param int $maxWidth |
||
| 221 | * @param int $maxHeight |
||
| 222 | * @return int[] |
||
| 223 | */ |
||
| 224 | private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param ISimpleFolder $previewFolder |
||
| 301 | * @param ISimpleFile $maxPreview |
||
| 302 | * @param int $width |
||
| 303 | * @param int $height |
||
| 304 | * @param bool $crop |
||
| 305 | * @param int $maxWidth |
||
| 306 | * @param int $maxHeight |
||
| 307 | * @return ISimpleFile |
||
| 308 | * @throws NotFoundException |
||
| 309 | * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
||
| 310 | */ |
||
| 311 | private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param ISimpleFolder $previewFolder |
||
| 354 | * @param int $width |
||
| 355 | * @param int $height |
||
| 356 | * @param bool $crop |
||
| 357 | * @return ISimpleFile |
||
| 358 | * |
||
| 359 | * @throws NotFoundException |
||
| 360 | */ |
||
| 361 | private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Get the specific preview folder for this file |
||
| 369 | * |
||
| 370 | * @param File $file |
||
| 371 | * @return ISimpleFolder |
||
| 372 | */ |
||
| 373 | private function getPreviewFolder(File $file) { |
||
| 382 | } |
||
| 383 |