Skip to content

Commit 9a4bf1e

Browse files
committed
minor symfony#17985 [Filesystem] Reduce complexity of ->remove() (nicolas-grekas)
This PR was merged into the 2.3 branch. Discussion ---------- [Filesystem] Reduce complexity of ->remove() | Q | A | ------------- | --- | Branch | 2.3 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | no | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 065acb7 [Filesystem] Reduce complexity of ->remove()
2 parents 8223305 + 065acb7 commit 9a4bf1e

File tree

2 files changed

+30
-37
lines changed

2 files changed

+30
-37
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -143,34 +143,23 @@ public function remove($files)
143143
$files = iterator_to_array($this->toIterator($files));
144144
$files = array_reverse($files);
145145
foreach ($files as $file) {
146-
if (!$this->exists($file) && !is_link($file)) {
147-
continue;
148-
}
149-
150-
if (is_dir($file) && !is_link($file)) {
146+
if (is_link($file)) {
147+
// Workaround https://bugs.php.net/52176
148+
if (!@unlink($file) && !@rmdir($file)) {
149+
$error = error_get_last();
150+
throw new IOException(sprintf('Failed to remove symlink "%s": %s.', $file, $error['message']));
151+
}
152+
} elseif (is_dir($file)) {
151153
$this->remove(new \FilesystemIterator($file));
152154

153-
if (true !== @rmdir($file)) {
154-
throw new IOException(sprintf('Failed to remove directory %s', $file));
155+
if (!@rmdir($file)) {
156+
$error = error_get_last();
157+
throw new IOException(sprintf('Failed to remove directory "%s": %s.', $file, $error['message']));
155158
}
156-
} else {
157-
// https://bugs.php.net/bug.php?id=52176
158-
if ('\\' === DIRECTORY_SEPARATOR && is_dir($file)) {
159-
if (true !== @rmdir($file)) {
160-
throw new IOException(sprintf('Failed to remove file %s', $file));
161-
}
162-
} else {
163-
if (true !== @unlink($file)) {
164-
// handle broken symlinks on Windows systems
165-
if (is_link($file) && false === @readlink($file)) {
166-
if (true !== @rmdir($file)) {
167-
throw new IOException(sprintf('Failed to remove broken symlink "%s".', $file), 0, null, $file);
168-
}
169-
} else {
170-
$error = error_get_last();
171-
throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, $error['message']));
172-
}
173-
}
159+
} elseif ($this->exists($file)) {
160+
if (!@unlink($file)) {
161+
$error = error_get_last();
162+
throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, $error['message']));
174163
}
175164
}
176165
}

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public function testRemoveCleansFilesAndDirectoriesIteratively()
281281

282282
$this->filesystem->remove($basePath);
283283

284-
$this->assertTrue(!is_dir($basePath));
284+
$this->assertFileNotExists($basePath);
285285
}
286286

287287
public function testRemoveCleansArrayOfFilesAndDirectories()
@@ -297,8 +297,8 @@ public function testRemoveCleansArrayOfFilesAndDirectories()
297297

298298
$this->filesystem->remove($files);
299299

300-
$this->assertTrue(!is_dir($basePath.'dir'));
301-
$this->assertTrue(!is_file($basePath.'file'));
300+
$this->assertFileNotExists($basePath.'dir');
301+
$this->assertFileNotExists($basePath.'file');
302302
}
303303

304304
public function testRemoveCleansTraversableObjectOfFilesAndDirectories()
@@ -314,8 +314,8 @@ public function testRemoveCleansTraversableObjectOfFilesAndDirectories()
314314

315315
$this->filesystem->remove($files);
316316

317-
$this->assertTrue(!is_dir($basePath.'dir'));
318-
$this->assertTrue(!is_file($basePath.'file'));
317+
$this->assertFileNotExists($basePath.'dir');
318+
$this->assertFileNotExists($basePath.'file');
319319
}
320320

321321
public function testRemoveIgnoresNonExistingFiles()
@@ -330,7 +330,7 @@ public function testRemoveIgnoresNonExistingFiles()
330330

331331
$this->filesystem->remove($files);
332332

333-
$this->assertTrue(!is_dir($basePath.'dir'));
333+
$this->assertFileNotExists($basePath.'dir');
334334
}
335335

336336
public function testRemoveCleansInvalidLinks()
@@ -342,11 +342,19 @@ public function testRemoveCleansInvalidLinks()
342342
mkdir($basePath);
343343
mkdir($basePath.'dir');
344344
// create symlink to nonexistent file
345-
@symlink($basePath.'file', $basePath.'link');
345+
@symlink($basePath.'file', $basePath.'file-link');
346+
347+
// create symlink to dir using trailing forward slash
348+
$this->filesystem->symlink($basePath.'dir/', $basePath.'dir-link');
349+
$this->assertTrue(is_dir($basePath.'dir-link'));
350+
351+
// create symlink to nonexistent dir
352+
rmdir($basePath.'dir');
353+
$this->assertFalse(is_dir($basePath.'dir-link'));
346354

347355
$this->filesystem->remove($basePath);
348356

349-
$this->assertTrue(!is_dir($basePath));
357+
$this->assertFileNotExists($basePath);
350358
}
351359

352360
public function testFilesExists()
@@ -1062,10 +1070,6 @@ private function getFileGroup($filepath)
10621070
*/
10631071
private function markAsSkippedIfSymlinkIsMissing($relative = false)
10641072
{
1065-
if (!function_exists('symlink')) {
1066-
$this->markTestSkipped('symlink is not supported');
1067-
}
1068-
10691073
if (false === self::$symlinkOnWindows) {
10701074
$this->markTestSkipped('symlink requires "Create symbolic links" privilege on Windows');
10711075
}

0 commit comments

Comments
 (0)