Passed
Push — master ( 35a69c...54bf96 )
by Ryuichi
01:46
created

File::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1.0527

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 5
cts 8
cp 0.625
crap 1.0527
rs 10
1
<?php
2
3
namespace WebStream\IO;
4
5
use WebStream\Exception\Extend\IOException;
0 ignored issues
show
Bug introduced by
The type WebStream\Exception\Extend\IOException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
/**
8
 * File
9
 * 状態を表さないもの(ファイルパス等)はキャッシュし
10
 * 状態を表すもの(存在チェク、ファイル種別、権限等)はキャッシュクリアし都度取得する
11
 * @author Ryuichi TANAKA.
12
 * @since 2016/02/05
13
 * @version 0.7
14
 */
15
class File
16
{
17
    /**
18
     * @var string ファイルパス
19
     */
20
    private $filePath;
21
22
    /**
23
     * @var string ファイル名
24
     */
25
    private $fileName;
26
27
    /**
28
     * @var string ファイル拡張子
29
     */
30
    private $fileExt;
31
32
    /**
33
     * constructor
34
     * @param string $filepath ファイルパス
35
     */
36 19
    public function __construct(string $filePath)
37
    {
38
        // realpathを含めてキャッシュクリア
39 19
        clearstatcache(true);
40
41 19
        $this->filePath = $filePath;
42 19
        $this->fileName = basename($this->filePath);
43 19
        $this->fileExt = pathinfo($this->filePath, PATHINFO_EXTENSION);
44
    }
45
46
    /**
47
     * ファイル名を返却する
48
     * @return string ファイル名
49
     */
50 3
    public function getFileName()
51
    {
52 3
        return $this->fileName;
53
    }
54
55
    /**
56
     * ファイル拡張子を返却する
57
     * @return string ファイル拡張子
58
     */
59 1
    public function getFileExtension()
60
    {
61 1
        return $this->fileExt;
62
    }
63
64
    /**
65
     * ファイルパスを返却する
66
     * シンボリックリンクの場合、シンボリックリンクファイルパスを返却する
67
     * @return string ファイルパス
68
     */
69 25
    public function getFilePath()
70
    {
71 25
        return $this->filePath;
72
    }
73
74
    /**
75
     * ファイルパスを返却する
76
     * シンボリックリンクの場合、実ファイルパスを返却する
77
     * @throws IOException
78
     * @return string ファイルパス
79
     */
80 14
    public function getAbsoluteFilePath()
81
    {
82 14
        if ($this->isLink()) {
83 1
            $filePath = $this->filePath;
84 1
            while (@is_link($filePath)) {
85 1
                $linkPath = readlink($filePath);
86 1
                if ($linkPath === false) {
87
                    throw new IOException("Symbolic link read error: " . $filePath);
88
                }
89 1
                $filePath = $linkPath;
90
            }
91
92 1
            $absoluteFilePath = realpath($filePath);
93 1
            if ($absoluteFilePath === false) {
94
                throw new IOException("File not found: " . $filePath);
95
            }
96
97 1
            return $absoluteFilePath;
98
        }
99
100 14
        $filePath = $this->getFilePath();
101 14
        $absoluteFilePath = realpath($filePath);
102 14
        if ($absoluteFilePath === false) {
103 1
            throw new IOException("File not found: " . $filePath);
104
        }
105
106 13
        return $absoluteFilePath;
107
    }
108
109
    /**
110
     * 読み込み権限があるか返却する
111
     * @return bool ファイルが存在し、読み込み権限があればtrue
112
     */
113 1
    public function isReadable()
114
    {
115
        // Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
116 1
        clearstatcache();
117
118 1
        return @is_readable($this->filePath);
119
    }
120
121
    /**
122
     * 書き込み権限があるか返却する
123
     * @return bool ファイルが存在し、書き込み権限があればtrue
124
     */
125 15
    public function isWritable()
126
    {
127
        // Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
128 15
        clearstatcache();
129
130 15
        return @is_writable($this->filePath);
131
    }
132
133
    /**
134
     * 実行権限があるか返却する
135
     * @return bool ファイルが存在し、実行権限があればtrue
136
     */
137 1
    public function isExecutable()
138
    {
139
        // Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
140 1
        clearstatcache();
141
142 1
        return @is_executable($this->filePath);
143
    }
144
145
    /**
146
     * ファイルかどうか
147
     * @return bool ファイルならtrue
148
     */
149 3
    public function isFile()
150
    {
151 3
        clearstatcache();
152
153 3
        return is_file($this->filePath);
154
    }
155
156
    /**
157
     * ディレクトリかどうか
158
     * @return bool ディレクトリならtrue
159
     */
160 8
    public function isDirectory()
161
    {
162 8
        clearstatcache();
163
164 8
        return is_dir($this->filePath);
165
    }
166
167
    /**
168
     * リンクかどうか
169
     * @return bool リンクならtrue
170
     */
171 18
    public function isLink()
172
    {
173
        // Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
174 18
        clearstatcache();
175
176 18
        return @is_link($this->filePath);
177
    }
178
179
    /**
180
     * ファイルサイズを返却する
181
     * ファイルが存在しなければ0
182
     * @return int ファイルサイズ
183
     */
184 1
    public function length()
185
    {
186 1
        $length = 0;
187
188 1
        if ($this->exists()) {
189 1
            $filePath = $this->getAbsoluteFilePath();
190 1
            $length = filesize($filePath);
191
192 1
            if ($length === false) {
193
                throw new IOException("Cannot get filesize of " . $filePath);
194
            }
195
        }
196
197 1
        return $length;
198
    }
199
200
    /**
201
     * ファイル(ディレクトリ、リンク含む)が存在するか
202
     * @return bool 存在すればtrue
203
     */
204 5
    public function exists()
205
    {
206
        // Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
207 5
        clearstatcache();
208
209 5
        return $this->isLink() || $this->isDirectory() || $this->isFile();
210
    }
211
212
    /**
213
     * ファイルを削除する
214
     * @return bool 削除結果
215
     */
216 6
    public function delete()
217
    {
218
        // Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
219 6
        clearstatcache();
220
221 6
        $isDeleted = false;
222 6
        if ($this->isWritable()) {
223 4
            if ($this->isDirectory()) {
224 1
                $isDeleted = rmdir($this->filePath);
225
            } else {
226 3
                $isDeleted = unlink($this->filePath);
227
            }
228
        }
229
230 6
        return $isDeleted;
231
    }
232
233
    /**
234
     * ファイルをリネームする
235
     * @param string $destPath 変更後ファイル名
236
     * @return bool リネーム結果
237
     */
238 2
    public function renameTo($destPath)
239
    {
240 2
        $dirname = dirname($destPath);
241 2
        $dir = new File($dirname);
242 2
        if (!$dir->isWritable()) {
243 1
            throw new IOException("Cannot writable: " . $destPath);
244
        }
245 1
        $dirPath = $dir->getFilePath();
246 1
        $absDestPath = $dirPath . "/" . basename($destPath);
247
248 1
        return rename($this->filePath, $absDestPath);
249
    }
250
251
    /**
252
     * ファイル更新日時を返却する
253
     * @return int ファイル更新日時
254
     */
255 1
    public function lastModified()
256
    {
257 1
        return $this->exists() ? filemtime($this->filePath) : 0;
258
    }
259
}
260