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 |
||
| 19 | abstract class AmazonS3 implements Simulator |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * AWS key |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $key; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * AWS secret |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $secret; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * AWS S3 bucket |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $bucket; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * TTL for all items in this bucket. |
||
| 44 | * |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | protected $bucketTTL; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * AWS S3 region |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $region; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * AWS remote path / object key |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $path; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * AWS remote raw path / object key |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $pathRaw; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Unix timestamp of generating path from placeholder. |
||
| 72 | * |
||
| 73 | * @var int |
||
| 74 | */ |
||
| 75 | protected $time; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * AWS acl |
||
| 79 | * 'private' by default |
||
| 80 | * |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected $acl; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Use multi part config |
||
| 87 | * |
||
| 88 | * @var boolean |
||
| 89 | */ |
||
| 90 | protected $multiPartUpload; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Min multi part upload size |
||
| 94 | * |
||
| 95 | * @var int |
||
| 96 | */ |
||
| 97 | protected $minMultiPartUploadSize = 5242880; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Max stream upload size |
||
| 101 | * |
||
| 102 | * @var int |
||
| 103 | */ |
||
| 104 | protected $maxStreamUploadSize = 104857600; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Configure the sync. |
||
| 108 | * |
||
| 109 | * @see \phpbu\App\Backup\Sync::setup() |
||
| 110 | * @param array $config |
||
| 111 | * @throws \phpbu\App\Backup\Sync\Exception |
||
| 112 | */ |
||
| 113 | 9 | public function setup(array $config) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Make sure all mandatory keys are present in given config. |
||
| 136 | * |
||
| 137 | * @param array $config |
||
| 138 | * @throws \phpbu\App\Backup\Sync\Exception |
||
| 139 | */ |
||
| 140 | 9 | protected function validateConfig(array $config) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Execute the sync |
||
| 151 | * |
||
| 152 | * @see \phpbu\App\Backup\Sync::sync() |
||
| 153 | * @param \phpbu\App\Backup\Target $target |
||
| 154 | * @param \phpbu\App\Result $result |
||
| 155 | * @throws \phpbu\App\Backup\Sync\Exception |
||
| 156 | */ |
||
| 157 | abstract public function sync(Target $target, Result $result); |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Simulate the sync execution. |
||
| 161 | * |
||
| 162 | * @param \phpbu\App\Backup\Target $target |
||
| 163 | * @param \phpbu\App\Result $result |
||
| 164 | */ |
||
| 165 | 1 | public function simulate(Target $target, Result $result) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Should multi part upload be used. |
||
| 178 | * |
||
| 179 | * @param \phpbu\App\Backup\Target $target |
||
| 180 | * @return bool |
||
| 181 | * @throws \phpbu\App\Exception |
||
| 182 | */ |
||
| 183 | protected function useMultiPartUpload(Target $target) |
||
| 191 | } |
||
| 192 |