StreamWriter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 111
Duplicated Lines 34.23 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.84%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 38
loc 111
ccs 33
cts 38
cp 0.8684
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 isWritable() 0 4 1
A setBufferSize() 0 4 1
A getBufferSize() 0 4 1
A write() 23 23 3
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\WriteException;
7
use Dazzle\Throwable\Exception\Logic\InvalidArgumentException;
8
9
class StreamWriter extends StreamSeeker implements StreamWriterInterface
10
{
11
    /**
12
     * @var bool
13
     */
14
    protected $writable;
15
16
    /**
17
     * @var int
18
     */
19
    protected $bufferSize;
20
21
    /**
22
     * @param resource $resource
23
     * @param bool $autoClose
24
     * @throws InvalidArgumentException
25
     */
26 22
    public function __construct($resource, $autoClose = true)
27
    {
28 22
        parent::__construct($resource, $autoClose);
29
30 21
        $this->writable = true;
31 21
        $this->bufferSize = 4096;
32 21
    }
33
34
    /**
35
     *
36
     */
37 15
    public function __destruct()
38
    {
39 15
        unset($this->writable);
40 15
        unset($this->bufferSize);
41
42 15
        parent::__destruct();
43 15
    }
44
45
    /**
46
     * @override
47
     * @inheritDoc
48
     */
49 2
    public function isWritable()
50
    {
51 2
        return $this->writable;
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 3
    public function getBufferSize()
68
    {
69 3
        return $this->bufferSize;
70
    }
71
72
    /**
73
     * @override
74
     * @inheritDoc
75
     */
76 2 View Code Duplication
    public function write($text = '')
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->writable)
79
        {
80
            return $this->throwAndEmitException(
81
                new WriteException('Stream is no longer writable.')
82
            );
83
        }
84
85 2
        $sent = fwrite($this->resource, $text);
86
87 2
        if ($sent === false)
88
        {
89
            return $this->throwAndEmitException(
90
                new WriteException('Error occurred while writing to the stream resource.')
91
            );
92
        }
93
94 2
        $this->emit('drain', [ $this ]);
95 2
        $this->emit('finish', [ $this ]);
96
97 2
        return true;
98
    }
99
100
    /**
101
     * @override
102
     * @inheritDoc
103
     */
104 4 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...
105
    {
106 4
        if ($this->closing)
107
        {
108
            return;
109
        }
110
111 4
        $this->closing = true;
112 4
        $this->readable = false;
0 ignored issues
show
Bug introduced by
The property readable 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...
113 4
        $this->writable = false;
114
115 4
        $this->emit('close', [ $this ]);
116 4
        $this->handleClose();
117 4
        $this->emit('done', [ $this ]);
118 4
    }
119
}
120