DroppingStream   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 33
ccs 11
cts 11
cp 1
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A write() 0 16 3
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