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 |
||
| 34 | class S3Upload extends BaseUpload implements UploadModelInterface |
||
| 35 | { |
||
| 36 | const DIR_LENGTH_FIRST = 2; |
||
| 37 | const DIR_LENGTH_SECOND = 4; |
||
| 38 | |||
| 39 | const BUCKET_DIR_SEPARATOR = '/'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Amazon web services S3 default bucket for upload files (not for delete). |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public $s3DefaultBucket; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Buckets for upload depending on the owner. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | public $s3Buckets = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Amazon web services SDK S3 client. |
||
| 57 | * |
||
| 58 | * @var S3ClientInterface|S3MultiRegionClient |
||
| 59 | */ |
||
| 60 | private $s3Client; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Binary contente of the original file. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private $originalContent; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Objects for delete (files in the S3 directory). |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private $objectsForDelete = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Bucket, in which the located files will be deleted. |
||
| 78 | * |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | private $bucketForDelete; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Bucket for upload new files. |
||
| 85 | * |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | private $bucketForUpload; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * S3 file options (bucket, prefix). |
||
| 92 | * |
||
| 93 | * @var ActiveRecord|S3FileOptions |
||
| 94 | */ |
||
| 95 | private $s3FileOptions; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Initialize. |
||
| 99 | */ |
||
| 100 | public function init() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Set s3 client. |
||
| 109 | * |
||
| 110 | * @param S3ClientInterface $s3Client |
||
| 111 | */ |
||
| 112 | public function setS3Client(S3ClientInterface $s3Client): void |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get s3 client. |
||
| 119 | * |
||
| 120 | * @return S3ClientInterface|null |
||
| 121 | */ |
||
| 122 | public function getS3Client() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get storage type - aws. |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | protected function getStorageType(): string |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Set some params for upload. |
||
| 139 | * It is needed to set the next parameters: |
||
| 140 | * $this->uploadDir |
||
| 141 | * $this->outFileName |
||
| 142 | * $this->bucketForUpload |
||
| 143 | * |
||
| 144 | * @throws InvalidConfigException |
||
| 145 | * |
||
| 146 | * @return void |
||
| 147 | */ |
||
| 148 | protected function setParamsForSend(): void |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Set some params for delete. |
||
| 173 | * It is needed to set the next parameters: |
||
| 174 | * $this->objectsForDelete |
||
| 175 | * $this->bucketForDelete |
||
| 176 | * |
||
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | protected function setParamsForDelete(): void |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Send file to remote storage. |
||
| 199 | * |
||
| 200 | * @throws InvalidConfigException |
||
| 201 | * |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | protected function sendFile(): bool |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Delete storage directory with original file and thumbs. |
||
| 227 | * |
||
| 228 | * @return void |
||
| 229 | */ |
||
| 230 | protected function deleteFiles(): void |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Create thumb. |
||
| 244 | * |
||
| 245 | * @param ThumbConfigInterface $thumbConfig |
||
| 246 | * |
||
| 247 | * @return mixed |
||
| 248 | */ |
||
| 249 | protected function createThumb(ThumbConfigInterface $thumbConfig) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Actions after main save. |
||
| 288 | * |
||
| 289 | * @return mixed |
||
| 290 | */ |
||
| 291 | protected function afterSave() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get binary contente of the original file. |
||
| 304 | * |
||
| 305 | * @throws InvalidValueException |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | private function getOriginalContent() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * S3 file options (bucket, prefix). |
||
| 324 | * |
||
| 325 | * @return ActiveRecord|S3FileOptions |
||
| 326 | */ |
||
| 327 | private function getS3FileOptions() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Set S3 options for uploaded file in amazon S3 storage. |
||
| 336 | * |
||
| 337 | * @param string $bucket |
||
| 338 | * @param string $prefix |
||
| 339 | * |
||
| 340 | * @return void |
||
| 341 | */ |
||
| 342 | private function setS3FileOptions(string $bucket, string $prefix): void |
||
| 355 | } |
||
| 356 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..