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 |
||
18 | class FilesystemTest extends \PHPUnit_Framework_TestCase |
||
19 | { |
||
20 | /** |
||
21 | * @var Filesystem |
||
22 | */ |
||
23 | protected $fileSystem; |
||
24 | |||
25 | public function setUp() |
||
26 | { |
||
27 | $this->fileSystem = new Filesystem(); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @expectedException RuntimeException |
||
32 | */ |
||
33 | public function testRecursiveCopy() |
||
34 | { |
||
35 | $tmp = sys_get_temp_dir(); |
||
36 | $basePath = $tmp . "/n98_testdir"; |
||
37 | $folder1 = $basePath . "/folder1"; |
||
38 | $folder2 = $basePath . "/folder2"; |
||
39 | $file1 = $folder1 . "/file1.txt"; |
||
40 | $file2 = $folder2 . "/file2.txt"; |
||
41 | $dest = sys_get_temp_dir() . "/n98_copy_dest"; |
||
42 | |||
43 | @mkdir($folder1, 0777, true); |
||
|
|||
44 | @mkdir($folder2, 0777, true); |
||
45 | touch($file1); |
||
46 | touch($file2); |
||
47 | |||
48 | $this->fileSystem->recursiveCopy($basePath, $dest); |
||
49 | $this->assertFileExists($dest . "/folder1/file1.txt"); |
||
50 | $this->assertFileExists($dest . "/folder2/file2.txt"); |
||
51 | |||
52 | //cleanup |
||
53 | unlink($file1); |
||
54 | unlink($file2); |
||
55 | rmdir($folder1); |
||
56 | rmdir($folder2); |
||
57 | rmdir($basePath); |
||
58 | |||
59 | unlink($dest . "/folder1/file1.txt"); |
||
60 | unlink($dest . "/folder2/file2.txt"); |
||
61 | rmdir($dest . "/folder1"); |
||
62 | rmdir($dest . "/folder2"); |
||
63 | rmdir($dest); |
||
64 | |||
65 | $this->assertFileNotExists($dest . "/folder1/file1.txt"); |
||
66 | $this->assertFileNotExists($dest); |
||
67 | |||
68 | is_dir($tmp . '/a') || mkdir($tmp . '/a'); |
||
69 | touch($tmp . '/file1.txt'); |
||
70 | $this->fileSystem->recursiveCopy($tmp . '/a', $tmp . '/file1.txt'); |
||
71 | unlink($tmp . '/file1.txt'); |
||
72 | rmdir($tmp . '/a'); |
||
73 | } |
||
74 | |||
75 | public function testRecursiveCopyWithBlacklist() |
||
76 | { |
||
77 | $tmp = sys_get_temp_dir(); |
||
78 | $basePath = $tmp . "/n98_testdir"; |
||
79 | $folder1 = $basePath . "/folder1"; |
||
80 | $folder2 = $basePath . "/folder2"; |
||
81 | $file1 = $folder1 . "/file1.txt"; |
||
82 | $ignoreMe = $folder1 . "/ignore.me"; |
||
83 | $file2 = $folder2 . "/file2.txt"; |
||
84 | $dest = sys_get_temp_dir() . "/n98_copy_dest"; |
||
85 | |||
86 | @mkdir($folder1, 0777, true); |
||
87 | @mkdir($folder2, 0777, true); |
||
88 | touch($file1); |
||
89 | touch($file2); |
||
90 | |||
91 | $this->fileSystem->recursiveCopy($basePath, $dest, array('ignore.me')); |
||
92 | $this->assertFileExists($dest . "/folder1/file1.txt"); |
||
93 | $this->assertFileExists($dest . "/folder2/file2.txt"); |
||
94 | $this->assertFileNotExists($dest . "/folder1/ignore.me"); |
||
95 | |||
96 | //cleanup |
||
97 | unlink($file1); |
||
98 | unlink($file2); |
||
99 | rmdir($folder1); |
||
100 | rmdir($folder2); |
||
101 | rmdir($basePath); |
||
102 | |||
103 | unlink($dest . "/folder1/file1.txt"); |
||
104 | unlink($dest . "/folder2/file2.txt"); |
||
105 | rmdir($dest . "/folder1"); |
||
106 | rmdir($dest . "/folder2"); |
||
107 | rmdir($dest); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @requires function symlink |
||
112 | */ |
||
113 | public function testRecursiveDirectoryRemoveUnLinksSymLinks() |
||
114 | { |
||
115 | $tmp = sys_get_temp_dir(); |
||
116 | $basePath = $tmp . "/n98_testdir"; |
||
117 | $symLinked = $tmp . "/n98_linked"; |
||
118 | $symLinkedFile = $symLinked . "/symlinkme.txt"; |
||
119 | |||
120 | @mkdir($basePath, 0777, true); |
||
121 | @mkdir($symLinked, 0777, true); |
||
122 | |||
123 | touch($symLinkedFile); |
||
124 | |||
125 | $result = @symlink($symLinked, $basePath . "/symlink"); |
||
126 | $this->assertTrue($result); |
||
127 | |||
128 | $this->fileSystem->recursiveRemoveDirectory($basePath); |
||
129 | |||
130 | $this->assertFileExists($symLinkedFile); |
||
131 | $this->assertFileNotExists($basePath); |
||
132 | } |
||
133 | |||
134 | View Code Duplication | public function testRecursiveRemove() |
|
135 | { |
||
136 | $tmp = sys_get_temp_dir(); |
||
137 | $basePath = $tmp . "/n98_testdir"; |
||
138 | $folder1 = $basePath . "/folder1"; |
||
139 | $folder2 = $basePath . "/folder2"; |
||
140 | $file1 = $folder1 . "/file1.txt"; |
||
141 | $file2 = $folder2 . "/file2.txt"; |
||
142 | |||
143 | @mkdir($folder1, 0777, true); |
||
144 | @mkdir($folder2, 0777, true); |
||
145 | touch($file1); |
||
146 | touch($file2); |
||
147 | |||
148 | $this->fileSystem->recursiveRemoveDirectory($basePath); |
||
149 | $this->assertFileNotExists($basePath); |
||
150 | } |
||
151 | |||
152 | View Code Duplication | public function testRecursiveRemoveWithTrailingSlash() |
|
153 | { |
||
154 | $tmp = sys_get_temp_dir(); |
||
155 | $basePath = $tmp . "/n98_testdir"; |
||
156 | $folder1 = $basePath . "/folder1"; |
||
157 | $folder2 = $basePath . "/folder2"; |
||
158 | $file1 = $folder1 . "/file1.txt"; |
||
159 | $file2 = $folder2 . "/file2.txt"; |
||
160 | |||
161 | @mkdir($folder1, 0777, true); |
||
162 | @mkdir($folder2, 0777, true); |
||
163 | touch($file1); |
||
164 | touch($file2); |
||
165 | |||
166 | $this->fileSystem->recursiveRemoveDirectory($basePath . "/"); |
||
167 | $this->assertFileNotExists($basePath); |
||
168 | } |
||
169 | |||
170 | public function testFalseIsReturnedIfDirectoryNotExist() |
||
171 | { |
||
172 | $this->assertFalse($this->fileSystem->recursiveRemoveDirectory("not-a-folder")); |
||
173 | } |
||
174 | |||
175 | public function testFalseIsReturnedIfDirectoryNotReadable() |
||
176 | { |
||
177 | $tmp = sys_get_temp_dir(); |
||
178 | $basePath = $tmp . "/n98_testdir-never-existed"; |
||
179 | |||
180 | $this->assertFalse($this->fileSystem->recursiveRemoveDirectory($basePath)); |
||
181 | } |
||
182 | |||
183 | public function testParentIsNotRemovedIfEmptyIsTrue() |
||
184 | { |
||
185 | $tmp = sys_get_temp_dir(); |
||
186 | $basePath = $tmp . "/n98_testdir"; |
||
187 | $folder1 = $basePath . "/folder1"; |
||
188 | $folder2 = $basePath . "/folder2"; |
||
189 | $file1 = $folder1 . "/file1.txt"; |
||
190 | $file2 = $folder2 . "/file2.txt"; |
||
191 | |||
192 | @mkdir($folder1, 0777, true); |
||
193 | @mkdir($folder2, 0777, true); |
||
194 | touch($file1); |
||
195 | touch($file2); |
||
196 | |||
197 | $this->fileSystem->recursiveRemoveDirectory($basePath, true); |
||
198 | $this->assertFileExists($basePath); |
||
199 | $this->assertFileNotExists($folder1); |
||
200 | $this->assertFileNotExists($folder2); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param int $bytes |
||
205 | * @param int $decimalPlaces |
||
206 | * @param string $expected |
||
207 | * @dataProvider convertedBytesProvider |
||
208 | */ |
||
209 | public function testConvertBytesToHumanReadable($bytes, $decimalPlaces, $expected) |
||
210 | { |
||
211 | $res = Filesystem::humanFileSize($bytes, $decimalPlaces); |
||
212 | $this->assertSame($expected, $res); |
||
213 | |||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return array |
||
218 | */ |
||
219 | public static function convertedBytesProvider() |
||
229 | } |
||
230 |
If you suppress an error, we recommend checking for the error condition explicitly: