-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathdelete_file.php
43 lines (36 loc) · 1.07 KB
/
delete_file.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
declare(strict_types=1);
namespace Psl\Filesystem;
use Psl\Internal;
use Psl\Str;
use function unlink;
/**
* Delete the file specified by $filename.
*
* @param non-empty-string $file
*
* @throws Exception\RuntimeException If unable to delete the file.
* @throws Exception\NotFileException If $file is not a file.
* @throws Exception\NotFoundException If $file is not found.
*
* @mago-expect best-practices/no-boolean-literal-comparison
*/
function delete_file(string $file): void
{
if (!namespace\exists($file)) {
throw Exception\NotFoundException::forFile($file);
}
if (!namespace\is_file($file)) {
throw Exception\NotFileException::for($file);
}
[$result, $error_message] = Internal\box(static fn(): bool => unlink($file));
// @codeCoverageIgnoreStart
if (false === $result && namespace\is_file($file)) {
throw new Exception\RuntimeException(Str\format(
'Failed to delete file "%s": %s.',
$file,
$error_message ?? 'internal error',
));
}
// @codeCoverageIgnoreEnd
}