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 | 'env' => '\\phpbu\\App\\Adapter\\Env', |
||
35 | 'dotenv' => '\\phpbu\\App\\Adapter\\DotEnv', |
||
36 | ], |
||
37 | 'runner' => [ |
||
38 | 'bootstrap' => '\\phpbu\\App\\Runner\\Bootstrap', |
||
39 | 'check' => '\\phpbu\\App\\Runner\\Check', |
||
40 | 'cleaner' => '\\phpbu\\App\\Runner\\Cleaner', |
||
41 | 'crypter' => '\\phpbu\\App\\Runner\\Crypter', |
||
42 | 'source' => '\\phpbu\\App\\Runner\\Source', |
||
43 | 'sync' => '\\phpbu\\App\\Runner\\Sync', |
||
44 | ], |
||
45 | 'logger' => [ |
||
46 | 'json' => '\\phpbu\\App\\Log\\Json', |
||
47 | 'mail' => '\\phpbu\\App\\Log\\Mail', |
||
48 | ], |
||
49 | 'source' => [ |
||
50 | 'arangodump' => '\\phpbu\\App\\Backup\\Source\\Arangodump', |
||
51 | 'elasticdump' => '\\phpbu\\App\\Backup\\Source\\Elasticdump', |
||
52 | 'mongodump' => '\\phpbu\\App\\Backup\\Source\\Mongodump', |
||
53 | 'mysqldump' => '\\phpbu\\App\\Backup\\Source\\Mysqldump', |
||
54 | 'pgdump' => '\\phpbu\\App\\Backup\\Source\\Pgdump', |
||
55 | 'redis' => '\\phpbu\\App\\Backup\\Source\\Redis', |
||
56 | 'rsync' => '\\phpbu\\App\\Backup\\Source\\Rsync', |
||
57 | 'tar' => '\\phpbu\\App\\Backup\\Source\\Tar', |
||
58 | 'xtrabackup' => '\\phpbu\\App\\Backup\\Source\\XtraBackup', |
||
59 | ], |
||
60 | 'check' => [ |
||
61 | 'sizemin' => '\\phpbu\\App\\Backup\\Check\\SizeMin', |
||
62 | 'sizediffpreviouspercent' => '\\phpbu\\App\\Backup\\Check\\SizeDiffPreviousPercent', |
||
63 | 'sizediffavgpercent' => '\\phpbu\\App\\Backup\\Check\\SizeDiffAvgPercent', |
||
64 | ], |
||
65 | 'crypter' => [ |
||
66 | 'mcrypt' => '\\phpbu\\App\\Backup\\Crypter\\Mcrypt', |
||
67 | 'openssl' => '\\phpbu\\App\\Backup\\Crypter\\OpenSSL', |
||
68 | ], |
||
69 | 'sync' => [ |
||
70 | 'amazons3' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v3', |
||
71 | 'amazons3-v3' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v3', |
||
72 | 'amazons3-v2' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v2', |
||
73 | 'dropbox' => '\\phpbu\\App\\Backup\\Sync\\Dropbox', |
||
74 | 'ftp' => '\\phpbu\\App\\Backup\\Sync\\Ftp', |
||
75 | 'rsync' => '\\phpbu\\App\\Backup\\Sync\\Rsync', |
||
76 | 'sftp' => '\\phpbu\\App\\Backup\\Sync\\Sftp', |
||
77 | 'softlayer' => '\\phpbu\\App\\Backup\\Sync\\SoftLayer', |
||
78 | ], |
||
79 | 'cleaner' => [ |
||
80 | 'capacity' => '\\phpbu\\App\\Backup\\Cleaner\\Capacity', |
||
81 | 'outdated' => '\\phpbu\\App\\Backup\\Cleaner\\Outdated', |
||
82 | 'quantity' => '\\phpbu\\App\\Backup\\Cleaner\\Quantity', |
||
83 | ], |
||
84 | 17 | ]; |
|
85 | |||
86 | 17 | /** |
|
87 | 17 | * Backup Factory. |
|
88 | 17 | * Creates 'Source', 'Check', 'Crypter', 'Sync' and 'Cleaner' Objects. |
|
89 | 17 | * |
|
90 | 1 | * @param string $type |
|
91 | * @param string $alias |
||
92 | 16 | * @throws \phpbu\App\Exception |
|
93 | 16 | * @return mixed |
|
94 | */ |
||
95 | View Code Duplication | protected function create($type, $alias) |
|
106 | |||
107 | /** |
||
108 | * Runner Factory. |
||
109 | * |
||
110 | * @param string $alias |
||
111 | * @param bool $isSimulation |
||
112 | * @return mixed |
||
113 | * @throws \phpbu\App\Exception |
||
114 | */ |
||
115 | public function createRunner($alias, $isSimulation) |
||
124 | 1 | ||
125 | /** |
||
126 | 1 | * Adapter Factory. |
|
127 | 1 | * |
|
128 | * @param string $alias |
||
129 | * @param array $conf |
||
130 | * @throws \phpbu\App\Exception |
||
131 | * @return \phpbu\App\Adapter |
||
132 | */ |
||
133 | View Code Duplication | public function createAdapter($alias, $conf = []) |
|
143 | 1 | ||
144 | /** |
||
145 | 1 | * Logger Factory. |
|
146 | 1 | * |
|
147 | * @param string $alias |
||
148 | * @param array $conf |
||
149 | * @throws \phpbu\App\Exception |
||
150 | * @return \phpbu\App\Log\Logger |
||
151 | */ |
||
152 | public function createLogger($alias, $conf = []) |
||
165 | |||
166 | /** |
||
167 | * Source Factory. |
||
168 | * |
||
169 | * @param string $alias |
||
170 | * @param array $conf |
||
171 | * @throws \phpbu\App\Exception |
||
172 | * @return \phpbu\App\Backup\Source |
||
173 | */ |
||
174 | 2 | View Code Duplication | public function createSource($alias, $conf = []) |
184 | |||
185 | /** |
||
186 | * Check Factory. |
||
187 | * |
||
188 | * @param string $alias |
||
189 | * @throws \phpbu\App\Exception |
||
190 | * @return \phpbu\App\Backup\Check |
||
191 | */ |
||
192 | public function createCheck($alias) |
||
201 | 1 | ||
202 | /** |
||
203 | * Crypter Factory. |
||
204 | * |
||
205 | * @param string $alias |
||
206 | * @param array $conf |
||
207 | * @throws \phpbu\App\Exception |
||
208 | * @return \phpbu\App\Backup\Crypter |
||
209 | */ |
||
210 | View Code Duplication | public function createCrypter($alias, $conf = []) |
|
220 | 1 | ||
221 | /** |
||
222 | * Sync Factory. |
||
223 | * |
||
224 | * @param string $alias |
||
225 | * @param array $conf |
||
226 | * @throws \phpbu\App\Exception |
||
227 | * @return \phpbu\App\Backup\Sync |
||
228 | */ |
||
229 | View Code Duplication | public function createSync($alias, $conf = []) |
|
239 | |||
240 | 11 | /** |
|
241 | 11 | * Cleaner Factory. |
|
242 | * |
||
243 | * @param string $alias |
||
244 | * @param array $conf |
||
245 | * @throws \phpbu\App\Exception |
||
246 | * @return \phpbu\App\Backup\Cleaner |
||
247 | */ |
||
248 | View Code Duplication | public function createCleaner($alias, $conf = []) |
|
258 | |||
259 | /** |
||
260 | * Extend the backup factory. |
||
261 | * |
||
262 | * @param string $type Type to create 'adapter', 'source', 'check', 'sync' or 'cleaner' |
||
263 | * @param string $alias Name the class is registered at |
||
264 | * @param string $fqcn Full Qualified Class Name |
||
265 | * @param boolean $force Overwrite already registered class |
||
266 | * @throws \phpbu\App\Exception |
||
267 | */ |
||
268 | View Code Duplication | public static function register($type, $alias, $fqcn, $force = false) |
|
278 | |||
279 | /** |
||
280 | * Throws an exception if type is invalid. |
||
281 | * |
||
282 | * @param string $type |
||
283 | * @throws \phpbu\App\Exception |
||
284 | */ |
||
285 | private static function checkType($type) |
||
293 | } |
||
294 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.