SecureStream::isWritable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Thruster\Component\SocketClient;
4
5
use Thruster\Component\EventLoop\EventLoopInterface;
6
use Thruster\Component\Stream\DuplexStreamInterface;
7
use Thruster\Component\Stream\Stream;
8
use Thruster\Component\Stream\WritableStreamInterface;
9
10
/**
11
 * Class SecureStream
12
 *
13
 * @package Thruster\Component\SocketClient
14
 * @author  Aurimas Niekis <[email protected]>
15
 */
16
class SecureStream extends Stream implements DuplexStreamInterface
17
{
18
    /**
19
     * @var Stream
20
     */
21
    private $decorating;
22
23
    public function __construct(Stream $stream, EventLoopInterface $loop)
24
    {
25
        $this->stream     = $stream->stream;
26
        $this->decorating = $stream;
27
        $this->loop       = $loop;
28
29
        $stream->on('error', function ($error) {
30
            $this->emit('error', [$error, $this]);
31
        });
32
33
        $stream->on('end', function () {
34
            $this->emit('end', [$this]);
35
        });
36
37
        $stream->on('close', function () {
38
            $this->emit('close', [$this]);
39
        });
40
41
        $stream->on('drain', function () {
42
            $this->emit('drain', [$this]);
43
        });
44
45
        $stream->pause();
46
47
        $this->resume();
48
    }
49
50
    public function handleData($stream)
51
    {
52
        $data = stream_get_contents($stream);
53
54
        $this->emit('data', [$data, $this]);
55
56
        if (false === is_resource($stream) || feof($stream)) {
57
            $this->end();
58
        }
59
    }
60
61
    /**
62
     * @return Stream
63
     */
64
    public function getDecorating()
65
    {
66
        return $this->decorating;
67
    }
68
69
    public function pause()
70
    {
71
        $this->loop->removeReadStream($this->decorating->stream);
72
    }
73
74
    public function resume()
75
    {
76
        if ($this->isReadable()) {
77
            $this->loop->addReadStream($this->decorating->stream, [$this, 'handleData']);
78
        }
79
    }
80
81
    public function isReadable() : bool
82
    {
83
        return $this->decorating->isReadable();
84
    }
85
86
    public function isWritable() : bool
87
    {
88
        return $this->decorating->isWritable();
89
    }
90
91
    public function write($data)
92
    {
93
        return $this->decorating->write($data);
94
    }
95
96
    public function close()
97
    {
98
        return $this->decorating->close();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->decorating->close(); (self) is incompatible with the return type declared by the interface Thruster\Component\Stream\StreamInterface::close of type Thruster\Component\Stream\StreamInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
99
    }
100
101
    public function end($data = null)
102
    {
103
        return $this->decorating->end($data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->decorating->end($data); (self) is incompatible with the return type declared by the interface Thruster\Component\Strea...bleStreamInterface::end of type Thruster\Component\Stream\WritableStreamInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
104
    }
105
106
    public function pipe(WritableStreamInterface $destination, array $options = [])
107
    {
108
        $this->pipeAll($this, $destination, $options);
109
110
        return $destination;
111
    }
112
}
113