Stream   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 39
ccs 9
cts 18
cp 0.5
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 8 1
A fromFile() 0 8 2
B create() 0 18 8
1
<?php declare(strict_types=1);
2
3
namespace Igni\Network\Http;
4
5
use Igni\Exception\InvalidArgumentException;
6
use Psr\Http\Message\StreamInterface;
7
use Zend\Diactoros\Stream as BaseStream;
8
9
use function strpos;
10
use function is_string;
11
use function is_resource;
12
13
/**
14
 * @package Igni\Http
15
 */
16
class Stream extends BaseStream
17
{
18 24
    public static function fromString(string $content): self
19
    {
20 24
        $stream = new self('php://temp', 'wb+');
21 24
        $stream->write($content);
22 24
        $stream->rewind();
23
24 24
        return $stream;
25
    }
26
27
    public static function fromFile(string $path, string $mode = 'r'): self
28
    {
29
        if ($path === 'php://input') {
30
            return new self($path, 'r');
31
        }
32
33
        return new self($path, $mode);
34
    }
35
36 24
    public static function create($stream, string $mode = 'r'): self
37
    {
38 24
        if (!is_string($stream) && !is_resource($stream) && !$stream instanceof StreamInterface) {
39
            throw new InvalidArgumentException(
40
                'Stream must be a string stream resource identifier, '
41
                . 'an actual stream resource, '
42
                . 'or a Psr\Http\Message\StreamInterface implementation'
43
            );
44
        }
45
46 24
        if (is_string($stream) && (empty($stream) || 0 !== strpos('php://', $stream))) {
47 24
            return self::fromString($stream);
48
        }
49
50
        return $stream instanceof StreamInterface
51
            ? $stream
52
            : self::fromFile($stream, $mode);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type resource; however, Igni\Network\Http\Stream::fromFile() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
53
    }
54
}
55