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 |
||
| 20 | class PictureRepository |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var S3Client |
||
| 24 | */ |
||
| 25 | private $s3Client; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var Client |
||
| 29 | */ |
||
| 30 | private $mongoDbClient; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param S3Client $s3Client |
||
| 34 | * @param Client $mongoDbClient |
||
| 35 | */ |
||
| 36 | 7 | public function __construct(S3Client $s3Client, Client $mongoDbClient) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @param $limit |
||
| 44 | * @param bool $orderDesc |
||
| 45 | * @throws \Exception |
||
| 46 | * |
||
| 47 | * @return \Parse\ParseObject[]|\Generator |
||
| 48 | */ |
||
| 49 | public function findAllImages(int $limit, bool $orderDesc = false) : \Generator |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param ParseObject $picture |
||
| 89 | * |
||
| 90 | * @return \MongoDB\UpdateResult |
||
| 91 | * |
||
| 92 | * @throws \Exception |
||
| 93 | */ |
||
| 94 | 1 | View Code Duplication | public function renameImage(ParseObject $picture) |
| 102 | |||
| 103 | /** |
||
| 104 | * @param ParseObject $picture |
||
| 105 | * |
||
| 106 | * @return \MongoDB\UpdateResult |
||
| 107 | * |
||
| 108 | * @throws \Exception |
||
| 109 | */ |
||
| 110 | 1 | View Code Duplication | public function renameThumbnail(ParseObject $picture) |
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $originalFileName |
||
| 121 | * @param string $fieldName |
||
| 122 | * |
||
| 123 | * @return \MongoDB\UpdateResult |
||
| 124 | */ |
||
| 125 | 2 | private function renamePicture(string $originalFileName, string $fieldName) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @param ParseObject $picture |
||
| 142 | * |
||
| 143 | * @return array |
||
| 144 | * |
||
| 145 | * @throws \Exception |
||
| 146 | */ |
||
| 147 | 1 | public function uploadImage(ParseObject $picture) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param ParseObject $picture |
||
| 156 | * |
||
| 157 | * @return array |
||
| 158 | * |
||
| 159 | * @throws \Exception |
||
| 160 | */ |
||
| 161 | public function uploadThumbnail(ParseObject $picture) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $imageUrl |
||
| 170 | * |
||
| 171 | * @return array |
||
| 172 | * |
||
| 173 | * @throws \Exception |
||
| 174 | */ |
||
| 175 | 1 | private function uploadPicture(string $imageUrl) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param ParseObject $picture |
||
| 192 | * |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | 2 | public function deletePicture(ParseObject $picture) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * This will actually read from Parse server and insert data into a given MongoDB database. |
||
| 207 | * |
||
| 208 | * @return \MongoDB\InsertManyResult |
||
| 209 | * |
||
| 210 | * @throws \Exception |
||
| 211 | */ |
||
| 212 | public function migrateAllPictures() |
||
| 227 | |||
| 228 | //Below methods should probably be extracted to dedicated components |
||
| 229 | /** |
||
| 230 | * @param string $url |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | 5 | private function getFileNameFromUrl(string $url) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $url |
||
| 245 | * |
||
| 246 | * @return Stream |
||
| 247 | * |
||
| 248 | * @throws \ErrorException |
||
| 249 | */ |
||
| 250 | 1 | private function getFileStream(string $url) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param ParseObject $picture |
||
| 266 | * |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | private function buildDocumentFromParseObject(ParseObject $picture) |
||
| 275 | } |
||
| 276 |
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.