StreamReader   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 122
Duplicated Lines 40.16 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 88.1%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 49
loc 122
ccs 37
cts 42
cp 0.881
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A __destruct() 0 7 1
A isReadable() 0 4 1
A setBufferSize() 0 4 1
A getBufferSize() 0 4 1
B read() 34 34 6
A close() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Dazzle\Stream\Sync;
4
5
use Dazzle\Stream\StreamSeeker;
6
use Dazzle\Throwable\Exception\Runtime\ReadException;
7
use Dazzle\Throwable\Exception\Logic\InvalidArgumentException;
8
9
class StreamReader extends StreamSeeker implements StreamReaderInterface
10
{
11
    /**
12
     * @var bool
13
     */
14
    protected $readable;
15
16
    /**
17
     * @var int
18
     */
19
    protected $bufferSize;
20
21
    /**
22
     * @param resource $resource
23
     * @param bool $autoClose
24
     * @throws InvalidArgumentException
25
     */
26 21
    public function __construct($resource, $autoClose = true)
27
    {
28 21
        parent::__construct($resource, $autoClose);
29
30 20
        $this->readable = true;
31 20
        $this->bufferSize = 4096;
32 20
    }
33
34
    /**
35
     *
36
     */
37 15
    public function __destruct()
38
    {
39 15
        unset($this->readable);
40 15
        unset($this->bufferSize);
41
42 15
        parent::__destruct();
43 15
    }
44
45
    /**
46
     * @override
47
     * @inheritDoc
48
     */
49 2
    public function isReadable()
50
    {
51 2
        return $this->readable;
52
    }
53
54
    /**
55
     * @override
56
     * @inheritDoc
57
     */
58 1
    public function setBufferSize($bufferSize)
59
    {
60 1
        $this->bufferSize = $bufferSize;
61 1
    }
62
63
    /**
64
     * @override
65
     * @inheritDoc
66
     */
67 2
    public function getBufferSize()
68
    {
69 2
        return $this->bufferSize;
70
    }
71
72
    /**
73
     * @override
74
     * @inheritDoc
75
     */
76 2 View Code Duplication
    public function read($length = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78 2
        if (!$this->readable)
79
        {
80
            return $this->throwAndEmitException(
81
                new ReadException('Stream is no longer readable.')
82
            );
83
        }
84
85 2
        if ($length === null)
86
        {
87 2
            $length = $this->bufferSize;
88
        }
89
90 2
        $ret = fread($this->resource, $length);
91
92 2
        if ($ret === false)
93
        {
94
            return $this->throwAndEmitException(
95
                new ReadException('Cannot read stream.')
96
            );
97
        }
98 2
        else if ($ret !== '')
99
        {
100 2
            $this->emit('data', [ $this, $ret ]);
101
102 2
            if (strlen($ret) < $length)
103
            {
104 2
                $this->emit('end', [ $this ]);
105
            }
106
        }
107
108 2
        return $ret;
109
    }
110
111
    /**
112
     * @override
113
     * @inheritDoc
114
     */
115 5 View Code Duplication
    public function close()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117 5
        if ($this->closing)
118
        {
119
            return;
120
        }
121
122 5
        $this->closing = true;
123 5
        $this->readable = false;
124 5
        $this->writable = false;
0 ignored issues
show
Bug introduced by
The property writable 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...
125
126 5
        $this->emit('close', [ $this ]);
127 5
        $this->handleClose();
128 5
        $this->emit('done', [ $this ]);
129 5
    }
130
}
131