Test Failed
Push — master ( 0b6467...e6142c )
by Alexpts
16:38
created

Stream   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 256
Duplicated Lines 0 %

Test Coverage

Coverage 96.84%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 122
c 2
b 1
f 0
dl 0
loc 256
ccs 92
cts 95
cp 0.9684
rs 8.96
wmc 43

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 7 2
A detach() 0 13 2
A __destruct() 0 3 1
A create() 0 25 5
A isSeekable() 0 3 1
A getContents() 0 11 3
A getSize() 0 22 5
A close() 0 5 3
A isReadable() 0 3 1
A seek() 0 11 3
A isWritable() 0 3 1
A read() 0 12 3
A rewind() 0 3 1
A write() 0 14 3
A getMetadata() 0 13 4
A eof() 0 3 2
A tell() 0 8 3

How to fix   Complexity   

Complex Class

Complex classes like Stream often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Stream, and based on these observations, apply Extract Interface, too.

1
<?php
2
declare(strict_types=1);
3
4
namespace PTS\Psr7;
5
6
use InvalidArgumentException;
7
use Psr\Http\Message\StreamInterface;
8
use RuntimeException;
9
use function clearstatcache;
10
use function fclose;
11
use function feof;
12
use function fopen;
13
use function fread;
14
use function fseek;
15
use function fstat;
16
use function ftell;
17
use function fwrite;
18
use function is_resource;
19
use function is_string;
20
use function stream_get_contents;
21
use function stream_get_meta_data;
22
use const SEEK_SET;
23
24
class Stream implements StreamInterface
25
{
26
    /** @var resource|null A resource reference */
27
    protected $stream = null;
28
    protected bool $seekable = false;
29
    protected bool $readable = false;
30
    protected bool $writable = false;
31
    /** @var array|mixed|void|null */
32
    protected $uri;
33
    /** @var int|null */
34
    protected ?int $size = null;
35
36
    protected const READ_WRITE_HASH = [
37
        'read' => [
38
            'r' => true,
39
            'w+' => true,
40
            'r+' => true,
41
            'x+' => true,
42
            'c+' => true,
43
            'rb' => true,
44
            'w+b' => true,
45
            'r+b' => true,
46
            'x+b' => true,
47
            'c+b' => true,
48
            'rt' => true,
49
            'w+t' => true,
50
            'r+t' => true,
51
            'x+t' => true,
52
            'c+t' => true,
53
            'a+' => true,
54
        ],
55
        'write' => [
56
            'w' => true,
57
            'w+' => true,
58
            'rw' => true,
59
            'r+' => true,
60 53
            'x+' => true,
61
            'c+' => true,
62 53
            'wb' => true,
63 2
            'w+b' => true,
64
            'r+b' => true,
65
            'x+b' => true,
66 51
            'c+b' => true,
67 32
            'w+t' => true,
68 32
            'r+t' => true,
69 32
            'x+t' => true,
70
            'c+t' => true,
71
            'a' => true,
72 51
            'a+' => true,
73 50
        ],
74 50
    ];
75 50
76 50
    /**
77 50
     * Creates a new PSR-7 stream.
78 50
     *
79 50
     * @param string|resource|StreamInterface $body
80
     *
81 50
     * @return StreamInterface
82
     * @throws InvalidArgumentException
83
     */
84 1
    public static function create($body = ''): StreamInterface
85
    {
86
        if ($body instanceof StreamInterface) {
87
            return $body;
88
        }
89
90 50
        if (is_string($body)) {
91
            $resource = fopen('php://temp', 'rw+');
92 50
            fwrite($resource, $body);
93 50
            $body = $resource;
94
        }
95 23
96
        if (is_resource($body)) {
97 23
            $new = new self();
98 22
            $new->stream = $body;
99
            $meta = stream_get_meta_data($new->stream);
100
            $new->seekable = $meta['seekable'] && 0 === \fseek($new->stream, 0, \SEEK_CUR);
101 23
            $new->readable = isset(self::READ_WRITE_HASH['read'][$meta['mode']]);
102
            $new->writable = isset(self::READ_WRITE_HASH['write'][$meta['mode']]);
103
            $new->uri = $new->getMetadata('uri');
104 50
105
            return $new;
106 50
        }
107 48
108 48
        throw new InvalidArgumentException('First argument to Stream::create() must be a string, resource or StreamInterface.');
109
    }
110 50
111
    /**
112 50
     * Closes the stream when the destructed.
113
     */
114 50
    public function __destruct()
115 1
    {
116
        $this->close();
117
    }
118 50
119 50
    public function __toString(): string
120
    {
121 50
        if ($this->isSeekable()) {
122 50
            $this->seek(0);
123
        }
124 50
125
        return $this->getContents();
126
    }
127 7
128
    public function close(): void
129 7
    {
130 3
        if (isset($this->stream) && is_resource($this->stream)) {
131
            fclose($this->stream);
132
            $this->detach();
133 7
        }
134 2
    }
135
136
    public function detach()
137
    {
138 5
        if ($this->stream === null) {
139 5
            return null;
140
        }
141
142 5
        $result = $this->stream;
143 5
        $this->stream = null;
144 5
145 5
        $this->size = $this->uri = null;
146
        $this->readable = $this->writable = $this->seekable = false;
147
148
        return $result;
149
    }
150
151 2
    public function getSize(): ?int
152
    {
153 2
        if (null !== $this->size) {
154 2
            return $this->size;
155 1
        }
156
157
        if (!isset($this->stream)) {
158 1
            return null;
159
        }
160
161 6
        // Clear the stat cache if the stream has a URI
162
        if ($this->uri) {
163 6
            clearstatcache(true, $this->uri);
0 ignored issues
show
Bug introduced by
It seems like $this->uri can also be of type array; however, parameter $filename of clearstatcache() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

163
            clearstatcache(true, /** @scrutinizer ignore-type */ $this->uri);
Loading history...
164
        }
165
166 29
        $stats = fstat($this->stream);
167
        if (isset($stats['size'])) {
168 29
            $this->size = $stats['size'];
169
            return $this->size;
170
        }
171 30
172
        return null;
173 30
    }
174 1
175
    public function tell(): int
176
    {
177 29
        $position = $this->stream === null ? false : ftell($this->stream);
178 1
        if (false === $position) {
179
            throw new RuntimeException('Unable to determine stream position');
180 28
        }
181
182 5
        return $position;
183
    }
184 5
185 5
    public function eof(): bool
186
    {
187 3
        return !$this->stream || feof($this->stream);
188
    }
189 3
190
    public function isSeekable(): bool
191
    {
192 7
        return $this->seekable;
193
    }
194 7
195 2
    public function seek($offset, $whence = SEEK_SET): void
196
    {
197
        if (!$this->seekable) {
198
            throw new RuntimeException('Stream is not seekable');
199 6
        }
200
201 6
        if (-1 === fseek($this->stream, $offset, $whence)) {
202
            throw new RuntimeException('Unable to seek to stream position ' .
203
                $offset .
204
                ' with whence ' .
205 6
                \var_export($whence, true));
206
        }
207
    }
208 3
209
    public function rewind(): void
210 3
    {
211
        $this->seek(0);
212
    }
213 5
214
    public function isWritable(): bool
215 5
    {
216 1
        return $this->writable;
217
    }
218
219 4
    public function write($string): int
220
    {
221
        if (!$this->writable) {
222 25
            throw new RuntimeException('Cannot write to a non-writable stream');
223
        }
224 25
225 1
        // We can't know the size after writing anything
226
        $this->size = null;
227
228 24
        if (false === $result = fwrite($this->stream, $string)) {
229
            throw new RuntimeException('Unable to write to stream');
230
        }
231
232 24
        return $result;
233
    }
234
235 50
    public function isReadable(): bool
236
    {
237 50
        return $this->readable;
238 1
    }
239
240
    public function read($length): string
241 50
    {
242
        if (!$this->readable) {
243 50
            throw new RuntimeException('Cannot read from non-readable stream');
244 1
        }
245
246
        $result = @fread($this->stream, $length);
247 50
        if (false === $result) {
248
            throw new RuntimeException('Unable to read from stream');
249
        }
250
251
        return $result;
252
    }
253
254
    public function getContents(): string
255
    {
256
        if (!isset($this->stream)) {
257
            throw new RuntimeException('Unable to read stream contents');
258
        }
259
260
        if (false === $contents = @stream_get_contents($this->stream)) {
261
            throw new RuntimeException('Unable to read stream contents');
262
        }
263
264
        return $contents;
265
    }
266
267
    public function getMetadata($key = null)
268
    {
269
        if (!isset($this->stream)) {
270
            return $key ? null : [];
271
        }
272
273
        $meta = stream_get_meta_data($this->stream);
274
275
        if (null === $key) {
276
            return $meta;
277
        }
278
279
        return $meta[$key] ?? null;
280
    }
281
}