• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Unleash / unleash-client-php / 8726826229

17 Apr 2024 06:22PM UTC coverage: 99.933% (-0.07%) from 100.0%
8726826229

Pull #203

github

web-flow
Merge 919c1318c into 783f84b87
Pull Request #203: Chore: Make php 8.3 the main version

1493 of 1494 relevant lines covered (99.93%)

11.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.55
/src/Helper/StringStream.php
1
<?php
2

3
namespace Unleash\Client\Helper;
4

5
use Psr\Http\Message\StreamInterface;
6
use Unleash\Client\Exception\StreamException;
7

8
/**
9
 * @internal
10
 */
11
final class StringStream implements StreamInterface
12
{
13
    /**
14
     * @var resource|null
15
     */
16
    private $stream;
17

18
    private readonly int $size;
19

20
    public function __construct(
21
        string $content
22
    ) {
23
        $this->size = strlen($content);
23✔
24
        $stream = fopen('php://temp', 'w+');
23✔
25
        if (!is_resource($stream)) {
23✔
26
            // @codeCoverageIgnoreStart
27
            throw new StreamException('Cannot create temporary stream for storing data');
28
            // @codeCoverageIgnoreEnd
29
        }
30
        $this->stream = $stream;
23✔
31
        fwrite($this->stream, $content);
23✔
32
        rewind($this->stream);
23✔
33
    }
34

35
    public function __toString(): string
36
    {
37
        $stream = $this->stream;
3✔
38
        if ($stream === null) {
3✔
39
            return '';
1✔
40
        }
41
        $content = stream_get_contents($stream, -1, 0);
2✔
42
        if (!is_string($content)) {
2✔
43
            // @codeCoverageIgnoreStart
44
            $content = '';
45
            // @codeCoverageIgnoreEnd
46
        }
47

48
        return $content;
2✔
49
    }
50

51
    public function close(): void
52
    {
53
        if ($this->stream === null) {
2✔
54
            throw new StreamException('The stream is detached');
1✔
55
        }
56
        $type = get_resource_type($this->stream);
1✔
57
        if ($type !== 'stream') {
1✔
58
            throw new StreamException('The stream is already closed');
1✔
59
        }
60
        fclose($this->stream);
1✔
61
    }
62

63
    public function detach()
64
    {
65
        $resource = $this->stream;
1✔
66
        $this->stream = null;
1✔
67

68
        return $resource;
1✔
69
    }
70

71
    public function getSize(): int
72
    {
73
        if ($this->stream === null) {
11✔
74
            throw new StreamException('The stream is detached');
1✔
75
        }
76

77
        return $this->size;
10✔
78
    }
79

80
    public function tell(): int
81
    {
82
        if ($this->stream === null) {
3✔
83
            throw new StreamException('The stream is detached');
1✔
84
        }
85
        $tell = ftell($this->stream);
2✔
86
        if ($tell === false) {
2✔
87
            throw new StreamException('Could not retrieve stream position. Is the stream after EOF?');
×
88
        }
89

90
        return $tell;
2✔
91
    }
92

93
    public function eof(): bool
94
    {
95
        if ($this->stream === null) {
2✔
96
            throw new StreamException('The stream is detached');
1✔
97
        }
98

99
        return feof($this->stream);
1✔
100
    }
101

102
    public function isSeekable(): bool
103
    {
104
        if ($this->stream === null) {
10✔
105
            throw new StreamException('The stream is detached');
1✔
106
        }
107

108
        return true;
9✔
109
    }
110

111
    public function seek($offset, $whence = SEEK_SET): void
112
    {
113
        if ($this->stream === null) {
7✔
114
            throw new StreamException('The stream is detached');
1✔
115
        }
116
        fseek($this->stream, $offset, $whence);
6✔
117
    }
118

119
    public function rewind(): void
120
    {
121
        if ($this->stream === null) {
2✔
122
            throw new StreamException('The stream is detached');
1✔
123
        }
124
        rewind($this->stream);
1✔
125
    }
126

127
    public function isWritable(): bool
128
    {
129
        if ($this->stream === null) {
2✔
130
            throw new StreamException('The stream is detached');
1✔
131
        }
132

133
        return true;
1✔
134
    }
135

136
    public function write($string): int
137
    {
138
        if ($this->stream === null) {
2✔
139
            throw new StreamException('The stream is detached');
1✔
140
        }
141
        $result = fwrite($this->stream, $string);
1✔
142
        if ($result === false) {
1✔
143
            // @codeCoverageIgnoreStart
144
            throw new StreamException('Failed to write to the stream');
145
            // @codeCoverageIgnoreEnd
146
        }
147

148
        return $result;
1✔
149
    }
150

151
    public function isReadable(): bool
152
    {
153
        if ($this->stream === null) {
2✔
154
            throw new StreamException('The stream is detached');
1✔
155
        }
156

157
        return true;
1✔
158
    }
159

160
    /**
161
     * @param int<0, max> $length
162
     */
163
    public function read($length): string
164
    {
165
        if ($this->stream === null) {
3✔
166
            throw new StreamException('The stream is detached');
1✔
167
        }
168
        $result = fread($this->stream, $length);
2✔
169
        if ($result === false) {
2✔
170
            // @codeCoverageIgnoreStart
171
            throw new StreamException('Failed to read from stream');
172
            // @codeCoverageIgnoreEnd
173
        }
174

175
        return $result;
2✔
176
    }
177

178
    public function getContents(): string
179
    {
180
        if ($this->stream === null) {
5✔
181
            throw new StreamException('The stream is detached');
1✔
182
        }
183
        $result = stream_get_contents($this->stream);
4✔
184
        if ($result === false) {
4✔
185
            // @codeCoverageIgnoreStart
186
            throw new StreamException('Failed to read from stream');
187
            // @codeCoverageIgnoreEnd
188
        }
189

190
        return $result;
4✔
191
    }
192

193
    public function getMetadata($key = null): mixed
194
    {
195
        if ($this->stream === null) {
2✔
196
            throw new StreamException('The stream is detached');
1✔
197
        }
198
        $metadata = stream_get_meta_data($this->stream);
1✔
199
        if ($key === null) {
1✔
200
            return $metadata;
1✔
201
        }
202

203
        return $metadata[$key] ?? null;
1✔
204
    }
205
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc