SlimStreamFactory::createStream()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
nc 4
nop 1
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 5
rs 9.6111
c 4
b 0
f 0
1
<?php
2
3
namespace Http\Message\StreamFactory;
4
5
use Http\Message\StreamFactory;
6
use Psr\Http\Message\StreamInterface;
7
use Slim\Http\Stream;
8
9
/**
10
 * Creates Slim 3 streams.
11
 *
12
 * @author Mika Tuupola <[email protected]>
13
 *
14
 * @deprecated This will be removed in php-http/message2.0. Consider using the official Slim PSR-17 factory
15
 */
16
final class SlimStreamFactory implements StreamFactory
17
{
18
    /**
19 9
     * {@inheritdoc}
20
     */
21 9
    public function createStream($body = null)
22 2
    {
23
        if ($body instanceof StreamInterface) {
24
            return $body;
25 7
        }
26 3
27
        if (is_resource($body)) {
28
            return new Stream($body);
29 4
        }
30 4
31 4
        $resource = fopen('php://memory', 'r+');
32 2
        $stream = new Stream($resource);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $stream of Slim\Http\Stream::__construct() does only seem to accept resource, 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

32
        $stream = new Stream(/** @scrutinizer ignore-type */ $resource);
Loading history...
33
        if (null !== $body && '' !== $body) {
34
            $stream->write((string) $body);
35 4
        }
36
37
        return $stream;
38
    }
39
}
40