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 |
||
30 | class OutputStreamWriterTest extends \PHPUnit_Framework_TestCase |
||
31 | { |
||
32 | use OutputStreamWriterProvider; |
||
33 | |||
34 | /** |
||
35 | * 正常系 |
||
36 | * ファイルに書き込みができること |
||
37 | * @test |
||
38 | * @dataProvider writeProvider |
||
39 | */ |
||
40 | View Code Duplication | public function okFileWriteFromFilePath($filePath) |
|
53 | |||
54 | /** |
||
55 | * 正常系 |
||
56 | * ファイルに書き込みができること |
||
57 | * ファイルオブジェクトを指定 |
||
58 | * @test |
||
59 | * @dataProvider writeProvider |
||
60 | */ |
||
61 | View Code Duplication | public function okFileWriteFromFileObject($filePath) |
|
74 | |||
75 | /** |
||
76 | * 正常系 |
||
77 | * Offset指定でファイルに書き込みができること |
||
78 | * @test |
||
79 | * @dataProvider writeProvider |
||
80 | */ |
||
81 | View Code Duplication | public function okFileWriteOffset($filePath) |
|
94 | |||
95 | /** |
||
96 | * 正常系 |
||
97 | * Length指定でファイルに書き込みができること |
||
98 | * @test |
||
99 | * @dataProvider writeProvider |
||
100 | */ |
||
101 | View Code Duplication | public function okFileWriteLength($filePath) |
|
114 | |||
115 | /** |
||
116 | * 正常系 |
||
117 | * Offset,Length指定でファイルに書き込みができること |
||
118 | * @test |
||
119 | * @dataProvider writeProvider |
||
120 | */ |
||
121 | View Code Duplication | public function okFileWriteOffsetLength($filePath) |
|
134 | |||
135 | /** |
||
136 | * 正常系 |
||
137 | * ファイルに追記できること |
||
138 | * @test |
||
139 | * @dataProvider writeProvider |
||
140 | */ |
||
141 | public function okFileWriteCharAppend($filePath) |
||
159 | |||
160 | /** |
||
161 | * 正常系 |
||
162 | * コンソールに書き込みができること |
||
163 | * @test |
||
164 | */ |
||
165 | View Code Duplication | public function okConsoleWrite() |
|
176 | |||
177 | /** |
||
178 | * 正常系 |
||
179 | * offset指定でコンソールに書き込めること |
||
180 | * @test |
||
181 | */ |
||
182 | View Code Duplication | public function okConsoleWriteOffset() |
|
193 | |||
194 | /** |
||
195 | * 正常系 |
||
196 | * Length指定でコンソールに書き込みができること |
||
197 | * @test |
||
198 | */ |
||
199 | View Code Duplication | public function okConsoleWriteLength() |
|
210 | |||
211 | /** |
||
212 | * 正常系 |
||
213 | * Offset,Length指定でコンソールに書き込みができること |
||
214 | * @test |
||
215 | */ |
||
216 | View Code Duplication | public function okConsoleWriteOffsetLength() |
|
227 | |||
228 | /** |
||
229 | * 異常系 |
||
230 | * ファイルオブジェクト、ファイルパス以外を指定した場合、例外が発生すること |
||
231 | * @test |
||
232 | * @expectedException WebStream\Exception\Extend\InvalidArgumentException |
||
233 | */ |
||
234 | public function ngInvalidFileType() |
||
239 | |||
240 | /** |
||
241 | * 異常系 |
||
242 | * ファイルがロックされている状態でストリームオブジェクトを作成した場合、例外が発生すること |
||
243 | * @test |
||
244 | * @dataProvider writeProvider |
||
245 | * @expectedException WebStream\Exception\Extend\IOException |
||
246 | */ |
||
247 | public function ngAlreadyFileLocked($filePath) |
||
264 | |||
265 | /** |
||
266 | * @test |
||
267 | */ |
||
268 | public function dummy() |
||
272 | |||
273 | /** |
||
274 | * 異常系 |
||
275 | * flush済みの状態でflushすると例外が発生すること |
||
276 | * @test |
||
277 | * @dataProvider writeProvider |
||
278 | * @expectedException WebStream\Exception\Extend\IOException |
||
279 | */ |
||
280 | public function ngInvalidFlush($filePath) |
||
292 | } |
||
293 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.