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 |
||
23 | class Factory |
||
24 | { |
||
25 | /** |
||
26 | * Map of available sources, checks, syncs and cleanups. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | private static $classMap = [ |
||
31 | // type |
||
32 | // alias => fqcn |
||
33 | 'adapter' => [ |
||
34 | 'array' => '\\phpbu\\App\\Adapter\\PHPArray', |
||
35 | 'dotenv' => '\\phpbu\\App\\Adapter\\Dotenv', |
||
36 | 'env' => '\\phpbu\\App\\Adapter\\Env', |
||
37 | ], |
||
38 | 'logger' => [ |
||
39 | 'json' => '\\phpbu\\App\\Log\\Json', |
||
40 | 'mail' => '\\phpbu\\App\\Log\\Mail', |
||
41 | 'webhook' => '\\phpbu\\App\\Log\\Webhook', |
||
42 | ], |
||
43 | 'source' => [ |
||
44 | 'arangodump' => '\\phpbu\\App\\Backup\\Source\\Arangodump', |
||
45 | 'elasticdump' => '\\phpbu\\App\\Backup\\Source\\Elasticdump', |
||
46 | 'mongodump' => '\\phpbu\\App\\Backup\\Source\\Mongodump', |
||
47 | 'mysqldump' => '\\phpbu\\App\\Backup\\Source\\Mysqldump', |
||
48 | 'pgdump' => '\\phpbu\\App\\Backup\\Source\\Pgdump', |
||
49 | 'redis' => '\\phpbu\\App\\Backup\\Source\\Redis', |
||
50 | 'rsync' => '\\phpbu\\App\\Backup\\Source\\Rsync', |
||
51 | 'tar' => '\\phpbu\\App\\Backup\\Source\\Tar', |
||
52 | 'xtrabackup' => '\\phpbu\\App\\Backup\\Source\\XtraBackup', |
||
53 | ], |
||
54 | 'check' => [ |
||
55 | 'sizemin' => '\\phpbu\\App\\Backup\\Check\\SizeMin', |
||
56 | 'sizediffpreviouspercent' => '\\phpbu\\App\\Backup\\Check\\SizeDiffPreviousPercent', |
||
57 | 'sizediffavgpercent' => '\\phpbu\\App\\Backup\\Check\\SizeDiffAvgPercent', |
||
58 | ], |
||
59 | 'crypter' => [ |
||
60 | 'mcrypt' => '\\phpbu\\App\\Backup\\Crypter\\Mcrypt', |
||
61 | 'openssl' => '\\phpbu\\App\\Backup\\Crypter\\OpenSSL', |
||
62 | ], |
||
63 | 'sync' => [ |
||
64 | 'amazons3' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v3', |
||
65 | 'amazons3-v3' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v3', |
||
66 | 'amazons3-v2' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v2', |
||
67 | 'dropbox' => '\\phpbu\\App\\Backup\\Sync\\Dropbox', |
||
68 | 'ftp' => '\\phpbu\\App\\Backup\\Sync\\Ftp', |
||
69 | 'googledrive' => '\\phpbu\\App\\Backup\\Sync\\GoogleDrive', |
||
70 | 'rsync' => '\\phpbu\\App\\Backup\\Sync\\Rsync', |
||
71 | 'sftp' => '\\phpbu\\App\\Backup\\Sync\\Sftp', |
||
72 | 'softlayer' => '\\phpbu\\App\\Backup\\Sync\\SoftLayer', |
||
73 | ], |
||
74 | 'cleaner' => [ |
||
75 | 'capacity' => '\\phpbu\\App\\Backup\\Cleaner\\Capacity', |
||
76 | 'outdated' => '\\phpbu\\App\\Backup\\Cleaner\\Outdated', |
||
77 | 'stepwise' => '\\phpbu\\App\\Backup\\Cleaner\\Stepwise', |
||
78 | 'quantity' => '\\phpbu\\App\\Backup\\Cleaner\\Quantity', |
||
79 | ], |
||
80 | ]; |
||
81 | |||
82 | /** |
||
83 | * Backup Factory. |
||
84 | * Creates 'Source', 'Check', 'Crypter', 'Sync' and 'Cleaner' Objects. |
||
85 | * |
||
86 | * @param string $type |
||
87 | * @param string $alias |
||
88 | * @throws \phpbu\App\Exception |
||
89 | * @return mixed |
||
90 | */ |
||
91 | 22 | View Code Duplication | protected function create($type, $alias) |
102 | |||
103 | /** |
||
104 | * Adapter Factory. |
||
105 | * |
||
106 | * @param string $alias |
||
107 | * @param array $conf |
||
108 | * @throws \phpbu\App\Exception |
||
109 | * @return \phpbu\App\Adapter |
||
110 | */ |
||
111 | 6 | public function createAdapter($alias, $conf = []) |
|
121 | |||
122 | /** |
||
123 | * Logger Factory. |
||
124 | * |
||
125 | * @param string $alias |
||
126 | * @param array $conf |
||
127 | * @throws \phpbu\App\Exception |
||
128 | * @return \phpbu\App\Listener |
||
129 | */ |
||
130 | 3 | public function createLogger($alias, $conf = []) |
|
143 | |||
144 | /** |
||
145 | * Create a backup target. |
||
146 | * |
||
147 | * @param \phpbu\App\Configuration\Backup\Target $conf |
||
148 | * @return \phpbu\App\Backup\Target |
||
149 | * @throws \phpbu\App\Exception |
||
150 | */ |
||
151 | 1 | public function createTarget(Configuration\Backup\Target $conf) |
|
162 | |||
163 | /** |
||
164 | * Source Factory. |
||
165 | * |
||
166 | * @param string $alias |
||
167 | * @param array $conf |
||
168 | * @throws \phpbu\App\Exception |
||
169 | * @return \phpbu\App\Backup\Source |
||
170 | */ |
||
171 | 2 | public function createSource($alias, $conf = []) |
|
181 | |||
182 | /** |
||
183 | * Check Factory. |
||
184 | * |
||
185 | * @param string $alias |
||
186 | * @throws \phpbu\App\Exception |
||
187 | * @return \phpbu\App\Backup\Check |
||
188 | */ |
||
189 | 4 | public function createCheck($alias) |
|
198 | |||
199 | /** |
||
200 | * Crypter Factory. |
||
201 | * |
||
202 | * @param string $alias |
||
203 | * @param array $conf |
||
204 | * @throws \phpbu\App\Exception |
||
205 | * @return \phpbu\App\Backup\Crypter |
||
206 | */ |
||
207 | 2 | public function createCrypter($alias, $conf = []) |
|
217 | |||
218 | /** |
||
219 | * Sync Factory. |
||
220 | * |
||
221 | * @param string $alias |
||
222 | * @param array $conf |
||
223 | * @throws \phpbu\App\Exception |
||
224 | * @return \phpbu\App\Backup\Sync |
||
225 | */ |
||
226 | 3 | public function createSync($alias, $conf = []) |
|
236 | |||
237 | /** |
||
238 | * Cleaner Factory. |
||
239 | * |
||
240 | * @param string $alias |
||
241 | * @param array $conf |
||
242 | * @throws \phpbu\App\Exception |
||
243 | * @return \phpbu\App\Backup\Cleaner |
||
244 | */ |
||
245 | 2 | public function createCleaner($alias, $conf = []) |
|
255 | |||
256 | /** |
||
257 | * Extend the backup factory. |
||
258 | * |
||
259 | * @param string $type Type to create 'adapter', 'source', 'check', 'sync' or 'cleaner' |
||
260 | * @param string $alias Name the class is registered at |
||
261 | * @param string $fqcn Full Qualified Class Name |
||
262 | * @param boolean $force Overwrite already registered class |
||
263 | * @throws \phpbu\App\Exception |
||
264 | */ |
||
265 | 23 | View Code Duplication | public static function register($type, $alias, $fqcn, $force = false) |
275 | |||
276 | /** |
||
277 | * Throws an exception if type is invalid. |
||
278 | * |
||
279 | * @param string $type |
||
280 | * @throws \phpbu\App\Exception |
||
281 | */ |
||
282 | 29 | private static function checkType($type) |
|
290 | } |
||
291 |