Passed
Push — develop ( 83b649...63947e )
by Johnny
02:08
created

ContentStream::stream_tell()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * ContentStream handles all the information being loaded
5
 * into the Rivescript interpreter.
6
 *
7
 * @package      Rivescript-php
8
 * @subpackage   Core
9
 * @category     ContentLoader
10
 * @author       Johnny Mast <[email protected]>
11
 */
12
13
namespace Axiom\Rivescript\Cortex\ContentLoader;
14
15
/**
16
 * This class taken from php.net and changed
17
 * to be workable for this project.
18
 *
19
 * @see https://www.php.net/manual/en/stream.streamwrapper.example-1.php
20
 * @see https://www.php.net/manual/en/class.streamwrapper
21
 */
22
class ContentStream
23
{
24
    /**
25
     * The cursor position inside the stream.
26
     *
27
     * @var int
28
     */
29
    private $position;
30
31
    /**
32
     * The variable that holds
33
     * the contents of the stream.
34
     *
35
     * @var string
36
     */
37
    protected $content;
38
39
    /**
40
     * Specifies the URL that was passed to the original function.
41
     * This is currently not used.
42
     *
43
     * @var string
44
     */
45
    protected $path;
46
47
    /**
48
     * The mode to operate in.
49
     * This is currently not used.
50
     *
51
     * @var string
52
     */
53
    protected $mode;
54
55
    /**
56
     * @param string $path Specifies the URL that was passed to the original function.
57
     * @param string $mode The mode used to open the file, as detailed for fopen().
58
     *
59
     * @see https://www.php.net/manual/en/function.fopen.php
60
     *
61
     * @return bool
62
     */
63
    public function stream_open(string $path, string $mode): bool
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

63
    public function stream_open(/** @scrutinizer ignore-unused */ string $path, string $mode): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $mode is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

63
    public function stream_open(string $path, /** @scrutinizer ignore-unused */ string $mode): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65
        $this->content = '';
66
        $this->position = 0;
67
        return true;
68
    }
69
70
    /**
71
     * Read data from the stream.
72
     *
73
     * @param int $count
74
     *
75
     * @return string
76
     */
77
    public function stream_read(int $count): string
78
    {
79
        $p =& $this->position;
80
        $ret = substr($this->content, $p, $count);
81
        $p += strlen($ret);
82
        return $ret;
83
    }
84
85
    /**
86
     * Write to the content.
87
     *
88
     * @param string $data The data to add to the stream.
89
     *
90
     * @return int
91
     */
92
    public function stream_write(string $data): int
93
    {
94
        $v =& $this->content;
95
        $l = strlen($data);
96
        $p =& $this->position;
97
        $v = substr($v, 0, $p) . $data . substr($v, $p += $l);
98
        return $l;
99
    }
100
101
    /**
102
     * Return the current position
103
     * in the stream.
104
     *
105
     * @return int
106
     */
107
    public function stream_tell(): int
108
    {
109
        return $this->position;
110
    }
111
112
    /**
113
     * Tests for end-of-stream on a pointer position.
114
     *
115
     * @return bool
116
     */
117
    public function stream_eof(): bool
118
    {
119
        return ($this->position >= strlen($this->content));
120
    }
121
122
    /**
123
     * Move the cursor inside the stream to this position.
124
     *
125
     * @param int $offset Seek this position in the stream.
126
     * @param int $whence The seek type.
127
     *
128
     * @see https://www.php.net/fseek for more information.
129
     *
130
     * @return bool
131
     */
132
    public function stream_seek(int $offset, int $whence = SEEK_SET): bool
133
    {
134
        $l = strlen($this->content);
135
        $p =& $this->position;
136
        switch ($whence) {
137
            case SEEK_SET:
138
                $newPos = $offset;
139
                break;
140
            case SEEK_CUR:
141
                $newPos = $p + $offset;
142
                break;
143
            case SEEK_END:
144
                $newPos = $l + $offset;
145
                break;
146
            default:
147
                return false;
148
        }
149
        $ret = ($newPos >= 0 && $newPos <= $l);
150
        if ($ret) $p = $newPos;
151
        return $ret;
152
    }
153
}
154