Skip to content

Commit 12e2b03

Browse files
authored
fix(filesystem): add ability to delete invalid symlinks (#1206)
1 parent 5b8be8a commit 12e2b03

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

packages/support/src/Filesystem/functions.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,20 @@ function delete(string $path, bool $recursive = true): void
214214
*/
215215
function delete_file(string $file): void
216216
{
217+
if (namespace\is_symbolic_link($file)) {
218+
[$result, $errorMessage] = box(static fn (): bool => unlink($file));
219+
220+
if ($result === false && namespace\is_symbolic_link($file)) { // @phpstan-ignore booleanAnd.rightAlwaysTrue
221+
throw new Exceptions\RuntimeException(sprintf(
222+
'Failed to delete symbolic link "%s": %s.',
223+
$file,
224+
$errorMessage ?? 'internal error',
225+
));
226+
}
227+
228+
return;
229+
}
230+
217231
if (! namespace\exists($file)) {
218232
throw Exceptions\NotFoundException::forFile($file);
219233
}

packages/support/tests/Filesystem/UnixFunctionsTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,4 +493,18 @@ public function test_ensure_directory_exists_on_existent_directory(): void
493493

494494
$this->assertTrue(is_dir($dir));
495495
}
496+
497+
public function test_delete_file_for_invalid_symlink(): void
498+
{
499+
$file = $this->fixtures . '/file.txt';
500+
\file_put_contents($file, 'hello');
501+
$link = $this->fixtures . '/link.txt';
502+
symlink($file, $link);
503+
unlink($file);
504+
505+
Filesystem\delete_file($link);
506+
507+
$this->assertFalse(is_file($link));
508+
$this->assertFalse(is_link($link));
509+
}
496510
}

0 commit comments

Comments
 (0)