Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
IOUtility
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 getState
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 exists
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 clearCache
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 move
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\IO;
6
7use PeServer\Core\Errors\ErrorHandler;
8use PeServer\Core\IO\IOState;
9use PeServer\Core\ResultData;
10use PeServer\Core\Text;
11use PeServer\Core\Throws\IOException;
12
13/**
14 * ファイル+ディレクトリ処理系。
15 */
16abstract class IOUtility
17{
18    #region function
19
20    public static function getState(string $path): IOState
21    {
22        $result = ErrorHandler::trap(fn () => stat($path));
23        if ($result->isFailureOrFalse()) {
24            throw new IOException();
25        }
26
27        return IOState::createFromStat($result->value);
28    }
29
30    /**
31     * ファイル・ディレクトリが存在するか。
32     *
33     * `file_exists` ラッパー。
34     *
35     * @param string $path
36     * @return boolean 存在するか。
37     * @see https://www.php.net/manual/function.file-exists.php
38     */
39    public static function exists(string $path): bool
40    {
41        return file_exists($path);
42    }
43
44    /**
45     * ファイルのステータスのキャッシュをクリア
46     *
47     * `clearstatcache` ラッパー。
48     *
49     * @param string|null $path
50     * @return void
51     * @see https://www.php.net/manual/function.clearstatcache.php
52     */
53    public static function clearCache(?string $path)
54    {
55        if ($path === null) {
56            clearstatcache(true);
57            return;
58        }
59
60        if (Text::isNullOrWhiteSpace($path)) {
61            throw new IOException();
62        }
63
64        clearstatcache(true, $path);
65    }
66
67
68    /**
69     * ファイル移動。
70     *
71     * @param string $fromPath
72     * @param string $toPath
73     * @return bool
74     * @see https://www.php.net/manual/function.rename.php
75     */
76    public static function move(string $fromPath, string $toPath): bool
77    {
78        return \rename($fromPath, $toPath);
79    }
80
81
82    #endregion
83}