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 AwsS3Provider 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 AwsS3Provider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class AwsS3Provider extends Provider implements ProviderInterface |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * All the configurations needed by this class with the |
||
| 43 | * optional configurations default values. |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $default = [ |
||
| 48 | 'url' => null, |
||
| 49 | 'threshold' => 10, |
||
| 50 | 'compression' => [ |
||
| 51 | 'extensions' => [], |
||
| 52 | 'algorithm' => null, |
||
| 53 | 'level' => 9 |
||
| 54 | ], |
||
| 55 | 'mimetypes' => [ |
||
| 56 | ], |
||
| 57 | 'providers' => [ |
||
| 58 | 'aws' => [ |
||
| 59 | 's3' => [ |
||
| 60 | 'version' => null, |
||
| 61 | 'region' => null, |
||
| 62 | 'endpoint' => null, |
||
| 63 | 'buckets' => null, |
||
| 64 | 'upload_folder' => '', |
||
| 65 | 'http' => null, |
||
| 66 | 'acl' => 'public-read', |
||
| 67 | 'cloudfront' => [ |
||
| 68 | 'use' => false, |
||
| 69 | 'cdn_url' => null, |
||
| 70 | ], |
||
| 71 | ], |
||
| 72 | ], |
||
| 73 | ], |
||
| 74 | ]; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Required configurations (must exist in the config file). |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $rules = ['version', 'region', 'key', 'secret', 'buckets', 'url', 'mimetypes']; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * this array holds the parsed configuration to be used across the class. |
||
| 85 | * |
||
| 86 | * @var Array |
||
| 87 | */ |
||
| 88 | protected $supplier; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var Instance of Aws\S3\S3Client |
||
| 92 | */ |
||
| 93 | protected $s3_client; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var Instance of Guzzle\Batch\BatchBuilder |
||
| 97 | */ |
||
| 98 | protected $batch; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var \Publiux\laravelcdn\Contracts\CdnHelperInterface |
||
| 102 | */ |
||
| 103 | protected $cdn_helper; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var \Publiux\laravelcdn\Validators\Contracts\ConfigurationsInterface |
||
| 107 | */ |
||
| 108 | protected $configurations; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var \Publiux\laravelcdn\Validators\Contracts\ProviderValidatorInterface |
||
| 112 | */ |
||
| 113 | protected $provider_validator; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param \Symfony\Component\Console\Output\ConsoleOutput $console |
||
| 117 | * @param \Publiux\laravelcdn\Validators\Contracts\ProviderValidatorInterface $provider_validator |
||
| 118 | * @param \Publiux\laravelcdn\Contracts\CdnHelperInterface $cdn_helper |
||
| 119 | */ |
||
| 120 | public function __construct( |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Read the configuration and prepare an array with the relevant configurations |
||
| 132 | * for the (AWS S3) provider. and return itself. |
||
| 133 | * |
||
| 134 | * @param $configurations |
||
| 135 | * |
||
| 136 | * @return $this |
||
| 137 | */ |
||
| 138 | public function init($configurations) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Upload assets. |
||
| 170 | * |
||
| 171 | * @param $assets |
||
| 172 | * |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | public function upload($assets) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Create an S3 client instance |
||
| 241 | * (Note: it will read the credentials form the .env file). |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function connect() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param $s3_client |
||
| 267 | */ |
||
| 268 | public function setS3Client($s3_client) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param $assets |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | private function getFilesAlreadyOnBucket($assets) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | public function getBucket() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Empty bucket. |
||
| 333 | * |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | public function emptyBucket() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * This function will be called from the CdnFacade class when |
||
| 383 | * someone use this {{ Cdn::asset('') }} facade helper. |
||
| 384 | * |
||
| 385 | * @param $path |
||
| 386 | * |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | public function urlGenerator($path) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | public function getCloudFront() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | public function getCloudFrontUrl() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | public function getUrl() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @param $attr |
||
| 435 | * |
||
| 436 | * @return Mix | null |
||
| 437 | */ |
||
| 438 | public function __get($attr) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Does file needs compression |
||
| 445 | * |
||
| 446 | * @param SplFileInfo $file File info |
||
| 447 | * |
||
| 448 | * @return bool |
||
| 449 | */ |
||
| 450 | private function needCompress(SplFileInfo $file) { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Read file content and compress |
||
| 459 | * |
||
| 460 | * @param SplFileInfo $file File to read |
||
| 461 | * @param bool $needsCompress Need file to compress |
||
| 462 | * |
||
| 463 | * @return resource|string |
||
| 464 | */ |
||
| 465 | private function getFileContent(SplFileInfo $file, $needsCompress) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Get mimetype from config or from system |
||
| 491 | * |
||
| 492 | * @param SplFileInfo $file File info to get mimetype |
||
| 493 | * |
||
| 494 | * @return false|string |
||
| 495 | */ |
||
| 496 | protected function getMimetype(SplFileInfo $file) { |
||
| 503 | |||
| 504 | } |
||
| 505 |
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..