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 GoogleDrive implements Simulator |
||
29 | { |
||
30 | use Cleanable; |
||
31 | |||
32 | /** |
||
33 | * Google drive service. |
||
34 | * |
||
35 | * @var \Google_Service_Drive |
||
36 | */ |
||
37 | private $service; |
||
38 | |||
39 | /** |
||
40 | * Google json secret file. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | private $secret; |
||
45 | |||
46 | /** |
||
47 | * Google json credentials file. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | private $access; |
||
52 | |||
53 | /** |
||
54 | * Google drive parent folder id. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | private $parent; |
||
59 | |||
60 | /** |
||
61 | * Upload chunk size. |
||
62 | * |
||
63 | * @var int |
||
64 | */ |
||
65 | private $chunkSize = 1 * 1024 * 1024; |
||
66 | |||
67 | /** |
||
68 | * (non-PHPDoc) |
||
69 | * |
||
70 | * @see \phpbu\App\Backup\Sync::setup() |
||
71 | * @param array $config |
||
72 | * @throws \phpbu\App\Exception |
||
73 | */ |
||
74 | 9 | public function setup(array $config) |
|
90 | |||
91 | /** |
||
92 | * Make sure both google authentication files exist and determine absolute path to them. |
||
93 | * |
||
94 | * @param array $config |
||
95 | * @throws \phpbu\App\Backup\Sync\Exception |
||
96 | */ |
||
97 | 7 | private function setupAuthFiles(array $config) |
|
110 | |||
111 | /** |
||
112 | * Execute the Sync |
||
113 | * |
||
114 | * @see \phpbu\App\Backup\Sync::sync() |
||
115 | * @param \phpbu\App\Backup\Target $target |
||
116 | * @param \phpbu\App\Result $result |
||
117 | * @throws \phpbu\App\Backup\Sync\Exception |
||
118 | */ |
||
119 | 3 | public function sync(Target $target, Result $result) |
|
149 | |||
150 | /** |
||
151 | * Simulate the sync execution. |
||
152 | * |
||
153 | * @param \phpbu\App\Backup\Target $target |
||
154 | * @param \phpbu\App\Result $result |
||
155 | */ |
||
156 | 1 | View Code Duplication | public function simulate(Target $target, Result $result) |
163 | |||
164 | /** |
||
165 | * Setup google api client and google drive service. |
||
166 | * |
||
167 | * @throws \Google_Exception |
||
168 | */ |
||
169 | protected function createDriveService() : GDriveService |
||
187 | |||
188 | /** |
||
189 | * Create google api file. |
||
190 | * |
||
191 | * @param \phpbu\App\Backup\Target $target |
||
192 | * @return \Google_Service_Drive_DriveFile |
||
193 | */ |
||
194 | 2 | protected function createFile(Target $target) : GFile |
|
202 | |||
203 | /** |
||
204 | * Create google api file deferred upload. |
||
205 | * |
||
206 | * @param \Google_Client $client |
||
207 | * @param \Psr\Http\Message\RequestInterface $request |
||
208 | * @param \phpbu\App\Backup\Target $target |
||
209 | * @return \Google_Http_MediaFileUpload |
||
210 | * @throws \phpbu\App\Exception |
||
211 | */ |
||
212 | protected function createUploadStream(GClient $client, RequestInterface $request, Target $target) : GStream |
||
213 | { |
||
214 | $media = new GStream( |
||
215 | $client, |
||
216 | $request, |
||
217 | 'application/octet-stream', |
||
218 | null, |
||
219 | true, |
||
220 | $this->chunkSize |
||
221 | ); |
||
222 | $media->setFileSize($target->getSize()); |
||
223 | |||
224 | return $media; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Return google credentials. |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | private function getAccessToken() : array |
||
236 | |||
237 | /** |
||
238 | * Update the access token in the google credentials file. |
||
239 | * |
||
240 | * @param array $accessToken |
||
241 | * @return void |
||
242 | */ |
||
243 | private function updateAccessToken(array $accessToken) |
||
247 | |||
248 | /** |
||
249 | * Creates collector for remote cleanup. |
||
250 | * |
||
251 | * @param \phpbu\App\Backup\Target $target |
||
252 | * @return \phpbu\App\Backup\Collector |
||
253 | * @throws \Google_Exception |
||
254 | */ |
||
255 | 1 | protected function createCollector(Target $target): Collector |
|
259 | } |
||
260 |