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

StringInputStream::readLine()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 3
eloc 12
nc 3
nop 0
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 = "";
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...
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;
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...
98 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...
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