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

StringInputStream::skip()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 7
Ratio 33.33 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 21
rs 9.3142
cc 3
eloc 11
nc 3
nop 1
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(string $str)
22
    {
23
        $this->cursorPosition = 0;
24
        $this->markedPosition = 0;
25
26
        // 文字列をストリームとして扱う
27
        $this->stream = $str;
0 ignored issues
show
Documentation Bug introduced by
It seems like $str of type string is incompatible with the declared type resource of property $stream.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
        $this->length = count($str);
29
    }
30
31
    /**
32
     * 入力ストリームを閉じる
33
     */
34
    public function close()
35
    {
36
        $this->stream = null;
37
    }
38
39
    /**
40
     * 入力ストリームからデータを読み込む
41
     * 引数に数値を指定した場合、指定数値バイトだけ読み込む
42
     * @param int length 読み込みバイト数
43
     * @return string 読み込みデータ
44
     */
45
    public function read($length = null)
46
    {
47
        if ($this->eof()) {
48
            return -1;
49
        }
50
51
        $out = "";
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...
52
        if ($length === null) {
53
            $endPosition = $this->length > $this->cursorPosition ?
54
                $this->cursorPosition : $this->length;
55
56
            $out = substr($this->stream, $endPosition);
57
        } else {
58
            // $lengthがファイル終端を越えないようにする
59
            if ($this->length > $this->cursorPosition + $length) {
60
                $length = $this->length - $this->cursorPosition;
61
            }
62
63
            $out = substr($this->stream, $this->cursorPosition, $length);
64
        }
65
66
        return $out;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function skip(int $pos)
73
    {
74
        // 文字列長より後方を指定した場合、-1を返す
75
        if ($this->cursorPosition + $pos > $this->length - 1) {
76
            return -1;
77
        }
78
79
        $start = $this->cursorPosition;
80
        $this->cursorPosition = $pos;
81
82
        $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...
83 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...
84
            // 前方へ移動
85
            $skipNum = $start - $this->cursorPosition;
86
        } else {
87
            // 後方へ移動
88
            $skipNum = $this->cursorPosition - $start;
89
        }
90
91
        return $skipNum;
92
    }
93
94
    /**
95
     * 入力ストリームの現在位置にmarkを設定する
96
     */
97
    public function mark()
98
    {
99
        $this->markedPosition = $this->cursorPosition;
100
    }
101
102
    /**
103
     * 最後にmarkされた位置に再配置する
104
     */
105
    public function reset()
106
    {
107
        $this->markedPosition = 0;
108
    }
109
110
    public function eof()
111
    {
112
        return $this->cursorPosition >= $this->length - 1;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function isMarkSupported()
119
    {
120
        return true;
121
    }
122
}
123