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 |
||
15 | class S3 |
||
16 | { |
||
17 | private $client; |
||
18 | |||
19 | /** |
||
20 | * __construct |
||
21 | * @author hagiwara |
||
22 | */ |
||
23 | public function __construct() |
||
52 | |||
53 | /** |
||
54 | * getClient |
||
55 | * clientを取得 |
||
56 | * @author hagiwara |
||
57 | */ |
||
58 | public function getClient() |
||
62 | |||
63 | /** |
||
64 | * upload |
||
65 | * S3へのファイルアップロード |
||
66 | * @author hagiwara |
||
67 | */ |
||
68 | public function upload($filepath, $filename) |
||
87 | |||
88 | /** |
||
89 | * upload |
||
90 | * S3からのファイルダウンロード |
||
91 | * @author hagiwara |
||
92 | */ |
||
93 | public function download($filename) |
||
104 | |||
105 | /** |
||
106 | * copy |
||
107 | * S3上でのファイルコピー |
||
108 | * @author hagiwara |
||
109 | */ |
||
110 | public function copy($oldFilename, $newFilename) |
||
124 | |||
125 | /** |
||
126 | * delete |
||
127 | * S3上でのファイル削除 |
||
128 | * @author hagiwara |
||
129 | */ |
||
130 | public function delete($filename) |
||
143 | |||
144 | /** |
||
145 | * deleteRecursive |
||
146 | * S3上でのファイル削除(再帰的) |
||
147 | * @author hagiwara |
||
148 | */ |
||
149 | public function deleteRecursive($dirname) |
||
166 | |||
167 | /** |
||
168 | * move |
||
169 | * S3上でのファイル移動(コピー&削除) |
||
170 | * @author hagiwara |
||
171 | */ |
||
172 | public function move($oldFilename, $newFilename) |
||
181 | |||
182 | /** |
||
183 | * fileExists |
||
184 | * S3上でのファイルの存在チェック(ディレクトリ存在チェックも兼) |
||
185 | * @author hagiwara |
||
186 | */ |
||
187 | public function fileExists($filename) |
||
191 | |||
192 | /** |
||
193 | * getFileList |
||
194 | * 特定ディレクトリ内のfileの一覧取得 |
||
195 | * @author hagiwara |
||
196 | */ |
||
197 | public function getFileList($dirname) |
||
204 | } |
||
205 |
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.