Completed
Push — feature/0.7.0 ( 83eb72...2f2cdf )
by Ryuichi
06:35
created

FileInputStream::reset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
namespace WebStream\IO;
3
4
use WebStream\Exception\Extend\InvalidArgumentException;
5
use WebStream\Exception\Extend\IOException;
6
7
/**
8
 * FileInputStream
9
 * @author Ryuichi TANAKA.
10
 * @since 2016/02/05
11
 * @version 0.7
12
 */
13
class FileInputStream extends InputStream
14
{
15
    /**
16
     * @var File ファイルオブジェクト
17
     */
18
    protected $file;
19
20
    /**
21
     * constructor
22
     * @param mixed $file ファイルオブジェクトまたはファイルパス
23
     * @throws InvalidArgumentException
24
     * @throws IOException
25
     */
26
    public function __construct($file)
27
    {
28
        if ($file instanceof File) {
29
            $this->file = $file;
30
        } else if (is_string($file)) {
31
            $this->file = new File($file);
32
        } else {
33
            throw new InvalidArgumentException("Unable to open file: " . $file);
34
        }
35
        $stream = @fopen($this->file->getAbsoluteFilePath(), 'r');
36
        if (!is_resource($stream) || $stream === false) {
37
            throw new IOException("Unable open " . $this->file->getAbsoluteFilePath());
38
        }
39
40
        parent::__construct($stream);
41
    }
42
43
    /**
44
     * 入力ストリームを閉じる
45
     */
46
    public function close()
47
    {
48
        if ($this->stream === null) {
49
            return;
50
        }
51
52
        if (get_resource_type($this->stream) !== 'Unknown' && fclose($this->stream) === false) {
53
            throw new IOException("Cannot close input stream.");
54
        }
55
56
        $this->stream = null;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function read($length = null)
63
    {
64
        if ($this->eof()) {
65
            return null;
66
        }
67
68
        $out = null;
0 ignored issues
show
Unused Code introduced by
$out is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
        if ($length === null) {
70
            if (($out = fread($this->stream, 1)) === false) {
71
                throw new IOException("Failed to fread.");
72
            }
73
        } else {
74
            if (!is_int($length)) {
75
                throw new InvalidArgumentException("Stream read must be a numeric value.");
76
            }
77
            if (($out = fread($this->stream, $length)) === false) {
78
                throw new IOException("Failed to fread.");
79
            }
80
        }
81
82
        return $out;
83
    }
84
85
    /**
86
     * 入力ストリームから行単位でデータを読み込む
87
     * 末尾に改行コードは含まない
88
     * @return string 読み込みデータ
89
     */
90
    public function readLine()
91
    {
92
        if ($this->eof()) {
93
            return null;
94
        }
95
96
        $out = fgets($this->stream);
97
        if ($out === false) {
98
            return null;
99
        }
100
101
        return trim($out);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function skip(int $pos)
108
    {
109
        // 現在のポインタ位置から$posだけ後方へ移動
110
        // シークに対応していないファイルシステムの場合、-1を返す
111
        if (fseek($this->stream, $pos, SEEK_CUR) === -1) {
112
            return -1;
113
        }
114
115
        $start = $this->cursorPosition;
116
        $this->cursorPosition = ftell($this->stream);
117
118
        $skipNum = 0;
0 ignored issues
show
Unused Code introduced by
$skipNum is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
119 View Code Duplication
        if ($start > $this->cursorPosition) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
            // 前方へ移動
121
            $skipNum = $start - $this->cursorPosition;
122
        } else {
123
            // 後方へ移動
124
            $skipNum = $this->cursorPosition - $start;
125
        }
126
127
        return $skipNum;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function reset()
134
    {
135
        if (!$this->isMarkSupported()) {
136
            throw new IOException(get_class($this) . " does not support mark and reset.");
137
        }
138
139
        // ポインタ位置をmark位置に移動
140
        fseek($this->stream, SEEK_SET, $this->markedPosition);
141
        // mark位置を初期値に戻す
142
        $this->markedPosition = 0;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function eof()
149
    {
150
        return feof($this->stream);
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function isMarkSupported()
157
    {
158
        return true;
159
    }
160
}
161