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:
| 1 | <?php |
||
| 28 | class Movie extends Provider { |
||
| 29 | public static $avconvBinary; |
||
| 30 | public static $ffmpegBinary; |
||
| 31 | public static $atomicParsleyBinary; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Keep track of movies without artwork to avoid retries in same request |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $noArtworkIndex = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritDoc} |
||
| 41 | */ |
||
| 42 | public function getMimeType() { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritDoc} |
||
| 48 | */ |
||
| 49 | public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { |
||
| 50 | // TODO: use proc_open() and stream the source file ? |
||
| 51 | |||
| 52 | $fileInfo = $fileview->getFileInfo($path); |
||
| 53 | $useFileDirectly = (!$fileInfo->isEncrypted() && !$fileInfo->isMounted()); |
||
| 54 | |||
| 55 | if ($useFileDirectly) { |
||
| 56 | $absPath = $fileview->getLocalFile($path); |
||
| 57 | View Code Duplication | } else { |
|
|
|
|||
| 58 | $absPath = \OC::$server->getTempManager()->getTemporaryFile(); |
||
| 59 | |||
| 60 | $handle = $fileview->fopen($path, 'rb'); |
||
| 61 | |||
| 62 | // we better use 5MB (1024 * 1024 * 5 = 5242880) instead of 1MB. |
||
| 63 | // in some cases 1MB was no enough to generate thumbnail |
||
| 64 | $firstmb = stream_get_contents($handle, 5242880); |
||
| 65 | file_put_contents($absPath, $firstmb); |
||
| 66 | } |
||
| 67 | |||
| 68 | $result = $this->generateThumbNail($maxX, $maxY, $absPath, 5); |
||
| 69 | if ($result === false) { |
||
| 70 | $result = $this->generateThumbNail($maxX, $maxY, $absPath, 1); |
||
| 71 | if ($result === false) { |
||
| 72 | $result = $this->generateThumbNail($maxX, $maxY, $absPath, 0); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | if (!$useFileDirectly) { |
||
| 77 | unlink($absPath); |
||
| 78 | } |
||
| 79 | |||
| 80 | return $result; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param $absPath |
||
| 85 | * @return bool|string |
||
| 86 | */ |
||
| 87 | private function extractMp4CoverArtwork($absPath) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param $absPath |
||
| 122 | * @param $second |
||
| 123 | * @return bool|string |
||
| 124 | */ |
||
| 125 | private function generateFromMovie($absPath, $second) { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param int $maxX |
||
| 152 | * @param int $maxY |
||
| 153 | * @param string $absPath |
||
| 154 | * @param int $second |
||
| 155 | * @return bool|\OCP\IImage |
||
| 156 | */ |
||
| 157 | private function generateThumbNail($maxX, $maxY, $absPath, $second) { |
||
| 177 | } |
||
| 178 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.