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 |
||
20 | View Code Duplication | class FileTest extends \PHPUnit_Framework_TestCase |
|
|
|||
21 | { |
||
22 | use FileProvider; |
||
23 | |||
24 | /** |
||
25 | * 正常系 |
||
26 | * ファイル名を取得できること |
||
27 | * @test |
||
28 | * @dataProvider fileProvider |
||
29 | */ |
||
30 | public function okFileName($file) |
||
34 | |||
35 | /** |
||
36 | * 正常系 |
||
37 | * ファイル拡張子を取得できること |
||
38 | * @test |
||
39 | * @dataProvider fileProvider |
||
40 | */ |
||
41 | public function okFileExtension($file) |
||
45 | |||
46 | /** |
||
47 | * 正常系 |
||
48 | * ファイルパスを取得できること |
||
49 | * @test |
||
50 | * @dataProvider fileProvider |
||
51 | */ |
||
52 | public function okFilePath($file) |
||
56 | |||
57 | /** |
||
58 | * 正常系 |
||
59 | * 絶対ファイルパスを取得できること |
||
60 | * @test |
||
61 | * @dataProvider fileLinkProvider |
||
62 | */ |
||
63 | public function okFileAbsolutePath($file, $linkPath) |
||
70 | |||
71 | /** |
||
72 | * 正常系 |
||
73 | * ファイルに読み込み権限をチェックできること |
||
74 | * @test |
||
75 | * @dataProvider fileProvider |
||
76 | */ |
||
77 | public function okFileReadable($file) |
||
82 | |||
83 | /** |
||
84 | * 正常系 |
||
85 | * ファイルに書き込み権限をチェックできること |
||
86 | * @test |
||
87 | * @dataProvider fileProvider |
||
88 | */ |
||
89 | public function okFileWritable($file) |
||
94 | |||
95 | /** |
||
96 | * 正常系 |
||
97 | * ファイルに実行権限をチェックできること |
||
98 | * @test |
||
99 | * @dataProvider fileProvider |
||
100 | */ |
||
101 | public function okFileExecutable($file) |
||
106 | |||
107 | /** |
||
108 | * 正常系 |
||
109 | * ファイルであるかどうかチェックできること |
||
110 | * @test |
||
111 | * @dataProvider fileProvider |
||
112 | */ |
||
113 | public function okFile($file) |
||
118 | |||
119 | /** |
||
120 | * 正常系 |
||
121 | * ディレクトリであるかどうかチェックできること |
||
122 | * @test |
||
123 | * @dataProvider directoryProvider |
||
124 | */ |
||
125 | public function okDirectory($file) |
||
130 | |||
131 | /** |
||
132 | * 正常系 |
||
133 | * リンクであるかどうかチェックできること |
||
134 | * @test |
||
135 | * @dataProvider fileLinkProvider |
||
136 | */ |
||
137 | public function okLink($file, $linkPath) |
||
145 | |||
146 | /** |
||
147 | * 正常系 |
||
148 | * ファイルサイズを取得できること |
||
149 | * @test |
||
150 | * @dataProvider fileProvider |
||
151 | */ |
||
152 | public function okFileSize($file) |
||
156 | |||
157 | /** |
||
158 | * 正常系 |
||
159 | * ファイルを削除できること |
||
160 | * @test |
||
161 | * @dataProvider tmpFileProvider |
||
162 | */ |
||
163 | public function okFileDelete($file) |
||
169 | |||
170 | /** |
||
171 | * 正常系 |
||
172 | * ディレクトリを削除できること |
||
173 | * @test |
||
174 | * @dataProvider tmpDirectoryProvider |
||
175 | */ |
||
176 | public function okDirectoryDelete($file) |
||
181 | |||
182 | /** |
||
183 | * 正常系 |
||
184 | * ファイルサイズを取得できること |
||
185 | * @test |
||
186 | * @dataProvider tmpFileProvider |
||
187 | */ |
||
188 | public function okFileRename($file) |
||
197 | |||
198 | /** |
||
199 | * 正常系 |
||
200 | * ファイル最終更新日時を取得できること |
||
201 | * @test |
||
202 | * @dataProvider fileProvider |
||
203 | */ |
||
204 | public function okLastModified($file) |
||
208 | |||
209 | /** |
||
210 | * 異常系 |
||
211 | * ファイルのリネームに失敗すること |
||
212 | * @test |
||
213 | * @dataProvider renameFailureProvider |
||
214 | * @expectedException WebStream\Exception\Extend\IOException |
||
215 | */ |
||
216 | public function ngFileRename($file) |
||
221 | } |
||
222 |
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.