DroppingStream::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Thruster\Component\HttpMessage;
4
5
use Psr\Http\Message\StreamInterface;
6
7
/**
8
 * Class DroppingStream
9
 *
10
 * @package Thruster\Component\HttpMessage
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
class DroppingStream implements StreamInterface
14
{
15
    use StreamDecoratorTrait;
16
17
    private $maxLength;
18
19
    /**
20
     * @param StreamInterface $stream    Underlying stream to decorate.
21
     * @param int             $maxLength Maximum size before dropping data.
22
     */
23 1
    public function __construct(StreamInterface $stream, int $maxLength)
24
    {
25 1
        $this->stream = $stream;
0 ignored issues
show
Bug introduced by
The property stream does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26 1
        $this->maxLength = $maxLength;
27 1
    }
28
29 1
    public function write($string)
30
    {
31 1
        $diff = $this->maxLength - $this->stream->getSize();
32
33
        // Begin returning 0 when the underlying stream is too large.
34 1
        if (0 >= $diff) {
35 1
            return 0;
36
        }
37
38
        // Write the stream or a subset of the stream if needed.
39 1
        if (strlen($string) < $diff) {
40 1
            return $this->stream->write($string);
41
        }
42
43 1
        return $this->stream->write(substr($string, 0, $diff));
44
    }
45
}
46