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 |
||
22 | abstract class AmazonS3 implements Simulator |
||
23 | { |
||
24 | /** |
||
25 | * AWS key |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $key; |
||
30 | |||
31 | /** |
||
32 | * AWS secret |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $secret; |
||
37 | |||
38 | /** |
||
39 | * AWS S3 bucket |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $bucket; |
||
44 | |||
45 | /** |
||
46 | * AWS S3 region |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $region; |
||
51 | |||
52 | /** |
||
53 | * AWS remote path / object key |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $path; |
||
58 | |||
59 | /** |
||
60 | * AWS acl |
||
61 | * 'private' by default |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $acl; |
||
66 | |||
67 | /** |
||
68 | * Use multi part config |
||
69 | * |
||
70 | * @var boolean |
||
71 | */ |
||
72 | protected $multiPartUpload; |
||
73 | |||
74 | 6 | /** |
|
75 | * Min multi part upload size |
||
76 | 6 | * |
|
77 | * @var int |
||
78 | */ |
||
79 | 6 | protected $minMultiPartUploadSize = 5242880; |
|
80 | 1 | ||
81 | /** |
||
82 | 5 | * Max stream upload size |
|
83 | 1 | * |
|
84 | * @var int |
||
85 | 4 | */ |
|
86 | 1 | protected $maxStreamUploadSize = 104857600; |
|
87 | |||
88 | 3 | /** |
|
89 | 1 | * Configure the sync. |
|
90 | * |
||
91 | 2 | * @see \phpbu\App\Backup\Sync::setup() |
|
92 | 1 | * @param array $config |
|
93 | * @throws \phpbu\App\Backup\Sync\Exception |
||
94 | 1 | */ |
|
95 | 1 | public function setup(array $config) |
|
116 | |||
117 | /** |
||
118 | * Execute the sync |
||
119 | * |
||
120 | * @see \phpbu\App\Backup\Sync::sync() |
||
121 | * @param \phpbu\App\Backup\Target $target |
||
122 | * @param \phpbu\App\Result $result |
||
123 | * @throws \phpbu\App\Backup\Sync\Exception |
||
124 | */ |
||
125 | abstract public function sync(Target $target, Result $result); |
||
126 | |||
127 | /** |
||
128 | * Simulate the sync execution. |
||
129 | * |
||
130 | * @param \phpbu\App\Backup\Target $target |
||
131 | * @param \phpbu\App\Result $result |
||
132 | */ |
||
133 | public function simulate(Target $target, Result $result) |
||
143 | |||
144 | /** |
||
145 | * Should multi part upload be used. |
||
146 | * |
||
147 | * @param \phpbu\App\Backup\Target $target |
||
148 | * @return bool |
||
149 | */ |
||
150 | protected function useMultiPartUpload(Target $target) |
||
158 | } |
||
159 |
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.