-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathdelete_file.php
34 lines (28 loc) · 906 Bytes
/
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
<?php
declare(strict_types=1);
namespace Psl\Filesystem;
use Psl;
use Psl\Internal;
use Psl\Str;
use function unlink;
/**
* Delete the file specified by $filename.
*
* @throws Exception\RuntimeException If unable to delete the file.
* @throws Psl\Exception\InvariantViolationException If the file specified by
* $filename does not exist.
*/
function delete_file(string $filename): void
{
Psl\invariant(is_file($filename), '$filename does not exists.');
[$result, $error_message] = Internal\box(static fn() => unlink($filename));
// @codeCoverageIgnoreStart
if (false === $result && is_file($filename)) {
throw new Exception\RuntimeException(Str\format(
'Failed to delete file "%s": %s.',
$filename,
$error_message ?? 'internal error'
));
}
// @codeCoverageIgnoreEnd
}