GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Stream::getMetadata()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 19
cp 0
rs 9.584
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
/**
4
 * This file is part of ReactGuzzleRing.
5
 *
6
 ** (c) 2015 Cees-Jan Kiewiet
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace WyriHaximus\React\Guzzle\HttpClient;
12
13
use Psr\Http\Message\StreamInterface;
14
15
/**
16
 * Class Stream
17
 *
18
 * @package WyriHaximus\React\RingPHP\HttpClient
19
 */
20
class Stream implements StreamInterface
21
{
22
    const OVERFLOW_LEVEL = 21632; // 2MB
23
24
    protected $stream;
25
    protected $eof = false;
26
    protected $size = 0;
27
    protected $buffer = '';
28
    protected $loop;
29
    protected $overflowStream;
30
31
    public function __construct(array $options)
32
    {
33
        $this->loop = $options['loop'];
34
35
        /*if (class_exists('React\Filesystem\Filesystem')) {
36
            $this->setUpFilesystemStream($options);
37
            return;
38
        }*/
39
40
        $this->setUpNormalStream($options);
41
    }
42
43
    protected function setUpFilesystemStream(array $options)
44
    {
45
        $this->overflowStream = new FileCacheStream([
46
            'loop' => $this->loop,
47
        ]);
48
49
        $options['response']->on(
50
            'data',
51
            function ($data) {
52
                if ($this->size >= static::OVERFLOW_LEVEL && $this->overflowStream->isOpen()) {
53
                    return $this->overflowStream->write($data);
54
                }
55
56
                if ($this->size >= static::OVERFLOW_LEVEL &&
57
                    !$this->overflowStream->isOpen() &&
58
                    !$this->overflowStream->isOpening()
59
                ) {
60
                    $this->overflowStream->open();
61
                }
62
63
                $this->buffer .= $data;
64
                $this->size = strlen($this->buffer);
65
            }
66
        );
67
68
        $options['request']->on(
69
            'end',
70
            function () {
71
                $this->eof = true;
72
            }
73
        );
74
    }
75
76
    protected function setUpNormalStream(array $options)
77
    {
78
        $options['response']->on(
79
            'data',
80
            function ($data) {
81
                $this->buffer .= $data;
82
                $this->size = strlen($this->buffer);
83
            }
84
        );
85
86
        $options['request']->on(
87
            'end',
88
            function () {
89
                $this->eof = true;
90
            }
91
        );
92
    }
93
94
    public function eof()
95
    {
96
        return $this->eof && $this->size === 0;
97
    }
98
99
    public function getSize()
100
    {
101
        return $this->size;
102
    }
103
104
    public function isReadable()
105
    {
106
        return true;
107
    }
108
109
    public function tell()
110
    {
111
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Psr\Http\Message\StreamInterface::tell of type integer.

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...
112
    }
113
114
    public function write($string)
115
    {
116
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Psr\Http\Message\StreamInterface::write of type integer.

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...
117
    }
118
119
    public function rewind()
120
    {
121
        return false;
122
    }
123
124
    public function isWritable()
125
    {
126
        return false;
127
    }
128
129
    public function isSeekable()
130
    {
131
        return false;
132
    }
133
134
    public function seek($offset, $whence = SEEK_SET)
135
    {
136
        return false;
137
    }
138
139
    public function read($length)
140
    {
141
        $this->toTickOrNotToTick();
142
143
        if (strlen($this->buffer) <= $length) {
144
            $buffer = $this->buffer;
145
            $this->buffer = '';
146
            $this->size = 0;
147
            return $buffer;
148
        }
149
150
        $buffer = substr($this->buffer, 0, $length);
151
        $this->buffer = substr($this->buffer, $length);
152
        $this->size = strlen($this->buffer);
153
        return $buffer;
154
    }
155
156
    public function getContents($maxLength = -1)
157
    {
158
        $buffer = '';
159
        while (!$this->eof()) {
160
            $buffer .= $this->read(1000000);
161
        }
162
        return $buffer;
163
    }
164
165
    public function __toString()
166
    {
167
        return $this->getContents();
168
    }
169
170
    public function getMetadata($key = null)
171
    {
172
        $metadata = [
173
            'timed_out'     => '',
174
            'blocked'       => false,
175
            'eof'           => $this->eof,
176
            'unread_bytes'  => '',
177
            'stream_type'   => '',
178
            'wrapper_type'  => '',
179
            'wrapper_data'  => '',
180
            'mode'          => '',
181
            'seekable'      => false,
182
            'uri'           => '',
183
        ];
184
185
        if (!$key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $key of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
186
            return $metadata;
187
        }
188
189
        return isset($metadata[$key]) ? $metadata[$key] : null;
190
    }
191
192
    public function attach($stream)
0 ignored issues
show
Unused Code introduced by
The parameter $stream is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
193
    {
194
    }
195
196
    public function detach()
197
    {
198
    }
199
200
    public function close()
201
    {
202
    }
203
204
    protected function toTickOrNotToTick()
205
    {
206
        if ($this->size === 0) {
207
            $this->loop->tick();
208
        }
209
    }
210
211
    /**
212
     * For Guzzle v4 compatibility
213
     */
214
    public function flush()
215
    {
216
    }
217
}
218