Completed
Push — feature/0.7.0 ( c0c2ef...83eb72 )
by Ryuichi
02:50
created

File::exists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 3
eloc 3
nc 3
nop 0
1
<?php
2
namespace WebStream\IO;
3
4
use WebStream\Exception\Extend\IOException;
5
6
/**
7
 * File
8
 * 状態を表さないもの(ファイルパス等)はキャッシュし、
9
 * 状態を表すもの(存在チェク、ファイル種別、権限等)はキャッシュクリアし都度取得する
10
 * @author Ryuichi TANAKA.
11
 * @since 2016/02/05
12
 * @version 0.7
13
 */
14
class File
15
{
16
    /**
17
     * @var string ファイルパス
18
     */
19
    private $filePath;
20
21
    /**
22
     * @var string ファイル名
23
     */
24
    private $fileName;
25
26
    /**
27
     * @var string ファイル拡張子
28
     */
29
    private $fileExt;
30
31
    /**
32
     * constructor
33
     * @param string $filepath ファイルパス
0 ignored issues
show
Documentation introduced by
There is no parameter named $filepath. Did you maybe mean $filePath?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
34
     */
35
    public function __construct(stirng $filePath)
36
    {
37
        // realpathを含めてキャッシュクリア
38
        clearstatcache(true);
39
40
        if ($realpath = realpath($filePath)) {
41
            $this->filePath = $realpath;
42
            $this->fileName = basename($filePath);
43
            $this->fileExt = pathinfo($filePath, PATHINFO_FILENAME);
44
        }
45
    }
46
47
    /**
48
     * ファイル名を返却する
49
     * @return string ファイル名
50
     */
51
    public function getFileName()
52
    {
53
        return $this->fileName;
54
    }
55
56
    /**
57
     * ファイル拡張子を返却する
58
     * @return string ファイル拡張子
59
     */
60
    public function getFileExtension()
61
    {
62
        return $this->fileExt;
63
    }
64
65
    /**
66
     * ファイルパスを返却する
67
     * シンボリックリンクの場合、シンボリックリンクファイルパスを返却する
68
     * @return string ファイルパス
69
     */
70
    public function getFilePath()
71
    {
72
        return $this->filePath;
73
    }
74
75
    /**
76
     * ファイルパスを返却する
77
     * シンボリックリンクの場合、実ファイルパスを返却する
78
     * @throws IOException
79
     * @return string ファイルパス
80
     */
81
    public function getAbsoluteFilePath()
82
    {
83
        if ($this->isLink()) {
84
            $filePath = $this->filePath;
85
            while (@is_link($filePath)) {
86
                $linkPath = readlink($filePath);
87
                if ($linkPath === false) {
88
                    throw new IOException("Symbolic link read error: " . $filePath);
89
                }
90
                $filePath = $linkPath;
91
            }
92
93
            return $filePath;
94
        }
95
96
        return $this->getFilePath();
97
    }
98
99
    /**
100
     * 読み込み権限があるか返却する
101
     * @return bool ファイルが存在し、読み込み権限があればtrue
102
     */
103
    public function isReadable()
104
    {
105
        if (!$this->exists()) {
106
            return false;
107
        }
108
109
        // Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
110
        clearstatcache();
111
112
        return @is_readable($this->filePath);
113
    }
114
115
    /**
116
     * 書き込み権限があるか返却する
117
     * @return bool ファイルが存在し、書き込み権限があればtrue
118
     */
119
    public function isWritable()
120
    {
121
        if (!$this->exists()) {
122
            return false;
123
        }
124
125
        // Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
126
        clearstatcache();
127
128
        return @is_writable($this->filePath);
129
    }
130
131
    /**
132
     * 実行権限があるか返却する
133
     * @return bool ファイルが存在し、実行権限があればtrue
134
     */
135
    public function isExecutable()
136
    {
137
        if (!$this->exists()) {
138
            return false;
139
        }
140
141
        // Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
142
        clearstatcache();
143
144
        return @is_executable($this->filePath);
145
    }
146
147
    /**
148
     * ファイルかどうか
149
     * @return bool ファイルならtrue
150
     */
151
    public function isFile()
152
    {
153
        clearstatcache();
154
155
        return is_file($this->filePath);
156
    }
157
158
    /**
159
     * ディレクトリかどうか
160
     * @return bool ディレクトリならtrue
161
     */
162
    public function isDirectory()
163
    {
164
        clearstatcache();
165
166
        return is_dir($this->filePath);
167
    }
168
169
    /**
170
     * リンクかどうか
171
     * @return bool リンクならtrue
172
     */
173
    public function isLink()
174
    {
175
        // Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
176
        clearstatcache();
177
178
        // 読み込み不可のシンボリックリンクの場合、例外を発生させる
179
        if (!$this->isReadable()) {
180
            throw new IOException("No read access to " . $this->filePath);
181
        }
182
183
        return @is_link($this->filePath);
184
    }
185
186
    /**
187
     * ファイルサイズを返却する
188
     * ファイルが存在しなければ0
189
     * @return int ファイルサイズ
190
     */
191
    public function length()
192
    {
193
        $length = 0;
194
195
        if ($this->exists()) {
196
            $filePath = $this->getAbsoluteFilePath();
197
            $length = filesize($filePath);
198
199
            if ($length === false) {
200
                throw new IOException("Cannot get filesize of " . $filePath);
201
            }
202
        }
203
204
        return $length;
205
    }
206
207
    /**
208
     * ファイル(ディレクトリ、リンク含む)が存在するか
209
     * @return bool 存在すればtrue
210
     */
211
    public function exists()
212
    {
213
        // Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
214
        clearstatcache();
215
216
        return $this->isLink() || $this->isDirectory() || $this->isFile();
217
    }
218
219
}
220