BufferStream::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of mracine/php-streams.
4
 *
5
 * (c) Matthieu Racine <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace mracine\Streams;
11
12
use mracine\Streams\Stream;
13
use mracine\Streams\Exception\StreamException;
14
15
/**
16
 * class BufferStream
17
 *
18
 * Initialized with a string, the read method retreive it as done with fread, consuming the buffer.
19
 * When all the buffer has been read, exception is thrown when try to read again.
20
 *
21
 * @since   0.2.0
22
 */
23
24
class BufferStream implements Stream
25
{
26
    /**
27
     * @var string $buffer Stores the string to use when read rhe stream
28
     */
29
    protected $buffer;
30
31
    /**
32
     * Constuctor
33
     *
34
     * @param string $string the initial data accessible via read method
35
     */
36 7
    public function __construct(string $string)
37
    {
38 7
        $this->buffer = $string;
39 7
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44 6
    public function read(int $length) : string
45
    {
46 6
        if (is_null($this->buffer)) {
0 ignored issues
show
introduced by
The condition is_null($this->buffer) is always false.
Loading history...
47 1
            throw new StreamException(sprintf("Cannot read from closed (null) stream"));
48
        }
49
50 5
        $remaining = strlen($this->buffer);
51
52 5
        if ($length<0) {
53 1
            throw new \InvalidArgumentException("Cannot read a negative count of bytes from a stream");
54
        }
55
56 4
        if (0 == $remaining) {
57 3
            throw new StreamException("End of stream");
58
        }
59
60 1
        if ($length>=$remaining) {
61
            // returns all
62 1
            $result = $this->buffer;
63
            // No more in the buffer
64 1
            $this->buffer = '';
65
        } else {
66
            // acquire $length characters from the buffer
67 1
            $result = substr($this->buffer, 0, $length);
68
            // remove $length characters from the buffer
69 1
            $this->buffer = substr_replace($this->buffer, '', 0, $length);
0 ignored issues
show
Documentation Bug introduced by
It seems like substr_replace($this->buffer, '', 0, $length) can also be of type array. However, the property $buffer is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
70
        }
71
72 1
        return $result;
73
    }
74
75
    /**
76
     * Fake write method, do nothing except return the pseudo "writen" length, i.e. the length of data
77
     *
78
     * {@inheritDoc}
79
     */
80 14
    public function write(string $data, $length = null) : int
81
    {
82 14
        if (is_null($this->buffer)) {
0 ignored issues
show
introduced by
The condition is_null($this->buffer) is always false.
Loading history...
83 1
            throw new StreamException(sprintf("Cannot write to closed (null) stream"));
84
        }
85
86 13
        if (is_null($length)) {
87 3
            $length = strlen($data);
88
        }
89
90 13
        if ($length<0) {
91 1
            throw new \InvalidArgumentException("Cannot write a negative count of bytes");
92
        }
93
94 12
        return min($length, strlen($data));
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100 1
    public function close()
101
    {
102 1
        $this->buffer = null;
103 1
    }
104
}
105