1
|
|
|
<?php |
2
|
|
|
namespace WebStream\IO; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* StringInputStream |
6
|
|
|
* @author Ryuichi TANAKA. |
7
|
|
|
* @since 2016/02/07 |
8
|
|
|
* @version 0.7 |
9
|
|
|
*/ |
10
|
|
|
class StringInputStream extends InputStream |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var int 文字列長 |
14
|
|
|
*/ |
15
|
|
|
private $length; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* construct |
19
|
|
|
* @param string $str 文字列 |
20
|
|
|
*/ |
21
|
|
|
public function __construct($str) |
22
|
|
|
{ |
23
|
|
|
parent::__construct($str); |
24
|
|
|
$this->length = strlen($str); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* 入力ストリームを閉じる |
29
|
|
|
*/ |
30
|
|
|
public function close() |
31
|
|
|
{ |
32
|
|
|
$this->stream = null; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function read($length = null) |
39
|
|
|
{ |
40
|
|
|
if ($this->eof()) { |
41
|
|
|
return -1; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$out = ""; |
|
|
|
|
45
|
|
|
if ($length === null) { |
46
|
|
|
$length = 1; |
47
|
|
|
$out = substr($this->stream, $this->cursorPosition, $length); |
48
|
|
|
$this->cursorPosition += $length; |
49
|
|
|
} else { |
50
|
|
|
// $lengthがファイル終端を越えないようにする |
51
|
|
|
if (($this->cursorPosition + $length) > $this->length) { |
52
|
|
|
$length = $this->length - $this->cursorPosition; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$out = substr($this->stream, $this->cursorPosition, $length); |
56
|
|
|
$this->cursorPosition += $length; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $out; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function readLine() |
63
|
|
|
{ |
64
|
|
|
if ($this->eof()) { |
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// 処理対象の残りのバイト数 |
69
|
|
|
$targetLength = $this->length - $this->cursorPosition; |
70
|
|
|
|
71
|
|
|
// 処理対象の文字列 |
72
|
|
|
$text = substr($this->stream, $this->cursorPosition, $targetLength); |
73
|
|
|
$lengthEOL = strlen(PHP_EOL); |
74
|
|
|
$notLinePart = strstr($text, PHP_EOL); |
75
|
|
|
|
76
|
|
|
// 残りの文字列に改行がない場合は0を設定 |
77
|
|
|
$notLinePartLength = $notLinePart === false ? 0 : strlen($notLinePart); |
78
|
|
|
$offset = $targetLength - $notLinePartLength; |
79
|
|
|
$out = substr($text, 0, $offset); |
80
|
|
|
$this->skip($offset + $lengthEOL); |
81
|
|
|
|
82
|
|
|
return $out; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function skip(int $pos) |
89
|
|
|
{ |
90
|
|
|
$start = $this->cursorPosition; |
91
|
|
|
$this->cursorPosition += $pos; |
92
|
|
|
|
93
|
|
|
if ($this->eof()) { |
94
|
|
|
return -1; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$skipNum = 0; |
|
|
|
|
98
|
|
View Code Duplication |
if ($start > $this->cursorPosition) { |
|
|
|
|
99
|
|
|
// 前方へ移動 |
100
|
|
|
$skipNum = $start - $this->cursorPosition; |
101
|
|
|
} else { |
102
|
|
|
// 後方へ移動 |
103
|
|
|
$skipNum = $this->cursorPosition - $start; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $skipNum; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function eof() |
113
|
|
|
{ |
114
|
|
|
return $this->cursorPosition > $this->length - 1; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function isMarkSupported() |
121
|
|
|
{ |
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.