Passed
Push — master ( 9b6318...46ddd5 )
by Evgeniy
01:25
created

PhpInputStream   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 23
dl 0
loc 77
ccs 22
cts 23
cp 0.9565
rs 10
c 1
b 0
f 1
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 7 2
A read() 0 13 3
A getContents() 0 11 2
A isWritable() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HttpSoft\ServerRequest;
6
7
use HttpSoft\Message\StreamTrait;
8
use Psr\Http\Message\StreamInterface;
9
10
final class PhpInputStream implements StreamInterface
11
{
12
    use StreamTrait {
13
        read as private readInternal;
14
        getContents as private getContentsInternal;
15
    }
16
17
    /**
18
     * @var string
19
     */
20
    private string $cache = '';
21
22
    /**
23
     * @var bool
24
     */
25
    private bool $isEof = false;
26
27
    /**
28
     * @param string|resource $stream
29
     */
30 18
    public function __construct($stream = 'php://input')
31
    {
32 18
        $this->init($stream, 'r');
33 18
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public function __toString(): string
39
    {
40 1
        if (!$this->isEof) {
41 1
            $this->getContents();
42
        }
43
44 1
        return $this->cache;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 2
    public function isWritable(): bool
51
    {
52 2
        return false;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 4
    public function read($length): string
59
    {
60 4
        $result = $this->readInternal($length);
61
62 3
        if (!$this->isEof) {
63 3
            $this->cache .= $result;
64
        }
65
66 3
        if ($this->eof()) {
67 2
            $this->isEof = true;
68
        }
69
70 3
        return $result;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 4
    public function getContents(): string
77
    {
78 4
        if ($this->isEof) {
79
            return $this->cache;
80
        }
81
82 4
        $result = $this->getContentsInternal();
83 3
        $this->cache .= $result;
84 3
        $this->isEof = true;
85
86 3
        return $result;
87
    }
88
}
89