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 |
||
| 41 | class Generator { |
||
| 42 | |||
| 43 | /** @var IPreview */ |
||
| 44 | private $previewManager; |
||
| 45 | /** @var IConfig */ |
||
| 46 | private $config; |
||
| 47 | /** @var IAppData */ |
||
| 48 | private $appData; |
||
| 49 | /** @var GeneratorHelper */ |
||
| 50 | private $helper; |
||
| 51 | /** @var EventDispatcherInterface */ |
||
| 52 | private $eventDispatcher; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param IConfig $config |
||
| 56 | * @param IPreview $previewManager |
||
| 57 | * @param IAppData $appData |
||
| 58 | * @param GeneratorHelper $helper |
||
| 59 | * @param EventDispatcherInterface $eventDispatcher |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function __construct( |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Returns a preview of a file |
||
| 77 | * |
||
| 78 | * The cache is searched first and if nothing usable was found then a preview is |
||
| 79 | * generated by one of the providers |
||
| 80 | * |
||
| 81 | * @param File $file |
||
| 82 | * @param int $width |
||
| 83 | * @param int $height |
||
| 84 | * @param bool $crop |
||
| 85 | * @param string $mode |
||
| 86 | * @param string $mimeType |
||
| 87 | * @return ISimpleFile |
||
| 88 | * @throws NotFoundException |
||
| 89 | * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
||
| 90 | */ |
||
| 91 | public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param ISimpleFolder $previewFolder |
||
| 145 | * @param File $file |
||
| 146 | * @param string $mimeType |
||
| 147 | * @return ISimpleFile |
||
| 148 | * @throws NotFoundException |
||
| 149 | */ |
||
| 150 | private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType) { |
||
| 151 | $nodes = $previewFolder->getDirectoryListing(); |
||
| 152 | |||
| 153 | foreach ($nodes as $node) { |
||
| 154 | if (strpos($node->getName(), 'max')) { |
||
| 155 | return $node; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | $previewProviders = $this->previewManager->getProviders(); |
||
| 160 | foreach ($previewProviders as $supportedMimeType => $providers) { |
||
| 161 | if (!preg_match($supportedMimeType, $mimeType)) { |
||
| 162 | continue; |
||
| 163 | } |
||
| 164 | |||
| 165 | foreach ($providers as $provider) { |
||
| 166 | $provider = $this->helper->getProvider($provider); |
||
| 167 | if (!($provider instanceof IProvider)) { |
||
| 168 | continue; |
||
| 169 | } |
||
| 170 | |||
| 171 | $maxWidth = (int)$this->config->getSystemValue('preview_max_x', 4096); |
||
| 172 | $maxHeight = (int)$this->config->getSystemValue('preview_max_y', 4096); |
||
| 173 | |||
| 174 | $preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight); |
||
| 175 | |||
| 176 | if (!($preview instanceof IImage)) { |
||
| 177 | continue; |
||
| 178 | } |
||
| 179 | |||
| 180 | // Try to get the extention. |
||
| 181 | try { |
||
| 182 | $ext = $this->getExtention($preview->dataMimeType()); |
||
| 183 | } catch (\InvalidArgumentException $e) { |
||
| 184 | // Just continue to the next iteration if this preview doesn't have a valid mimetype |
||
| 185 | continue; |
||
| 186 | } |
||
| 187 | |||
| 188 | $path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext; |
||
| 189 | try { |
||
| 190 | $file = $previewFolder->newFile($path); |
||
| 191 | $file->putContent($preview->data()); |
||
| 192 | } catch (NotPermittedException $e) { |
||
| 193 | throw new NotFoundException(); |
||
| 194 | } |
||
| 195 | |||
| 196 | return $file; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | throw new NotFoundException(); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param ISimpleFile $file |
||
| 205 | * @return int[] |
||
| 206 | */ |
||
| 207 | private function getPreviewSize(ISimpleFile $file) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param int $width |
||
| 214 | * @param int $height |
||
| 215 | * @param bool $crop |
||
| 216 | * @param string $mimeType |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | private function generatePath($width, $height, $crop, $mimeType) { |
||
| 229 | |||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * @param int $width |
||
| 234 | * @param int $height |
||
| 235 | * @param bool $crop |
||
| 236 | * @param string $mode |
||
| 237 | * @param int $maxWidth |
||
| 238 | * @param int $maxHeight |
||
| 239 | * @return int[] |
||
| 240 | */ |
||
| 241 | private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param ISimpleFolder $previewFolder |
||
| 318 | * @param ISimpleFile $maxPreview |
||
| 319 | * @param int $width |
||
| 320 | * @param int $height |
||
| 321 | * @param bool $crop |
||
| 322 | * @param int $maxWidth |
||
| 323 | * @param int $maxHeight |
||
| 324 | * @return ISimpleFile |
||
| 325 | * @throws NotFoundException |
||
| 326 | * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
||
| 327 | */ |
||
| 328 | private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @param ISimpleFolder $previewFolder |
||
| 371 | * @param int $width |
||
| 372 | * @param int $height |
||
| 373 | * @param bool $crop |
||
| 374 | * @param string $mimeType |
||
| 375 | * @return ISimpleFile |
||
| 376 | * |
||
| 377 | * @throws NotFoundException |
||
| 378 | */ |
||
| 379 | private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get the specific preview folder for this file |
||
| 387 | * |
||
| 388 | * @param File $file |
||
| 389 | * @return ISimpleFolder |
||
| 390 | */ |
||
| 391 | private function getPreviewFolder(File $file) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param string $mimeType |
||
| 403 | * @return null|string |
||
| 404 | * @throws \InvalidArgumentException |
||
| 405 | */ |
||
| 406 | private function getExtention($mimeType) { |
||
| 418 | } |
||
| 419 |