These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php declare(strict_types=1); |
||
2 | |||
3 | namespace WyriHaximus\React\Stream\Base64; |
||
4 | |||
5 | use Evenement\EventEmitter; |
||
6 | use React\Stream\ReadableStreamInterface; |
||
7 | use React\Stream\Util; |
||
8 | use React\Stream\WritableStreamInterface; |
||
9 | |||
10 | View Code Duplication | final class ReadableStreamBase64Encode extends EventEmitter implements ReadableStreamInterface |
|
0 ignored issues
–
show
|
|||
11 | { |
||
12 | /** |
||
13 | * @var WritableStreamInterface |
||
14 | */ |
||
15 | private $stream; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $buffer = ''; |
||
21 | |||
22 | /** |
||
23 | * @param ReadableStreamInterface $stream |
||
24 | */ |
||
25 | public function __construct(ReadableStreamInterface $stream) |
||
26 | { |
||
27 | $this->stream = $stream; |
||
0 ignored issues
–
show
It seems like
$stream of type object<React\Stream\ReadableStreamInterface> is incompatible with the declared type object<React\Stream\WritableStreamInterface> of property $stream .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..
Loading history...
|
|||
28 | $this->stream->on('data', function ($data) { |
||
29 | $this->buffer .= $data; |
||
30 | $this->emit('data', [$this->processBuffer()]); |
||
31 | }); |
||
32 | $this->stream->once('close', function () { |
||
33 | $this->emit('data', [base64_encode($this->buffer)]); |
||
34 | $this->emit('close'); |
||
35 | }); |
||
36 | Util::forwardEvents($stream, $this, ['error', 'end']); |
||
37 | } |
||
38 | |||
39 | public function isReadable() |
||
40 | { |
||
41 | return $this->stream->isReadable(); |
||
0 ignored issues
–
show
It seems like you code against a concrete implementation and not the interface
React\Stream\WritableStreamInterface as the method isReadable() does only exist in the following implementations of said interface: React\Stream\CompositeStream , React\Stream\DuplexResourceStream , React\Stream\ThroughStream .
Let’s take a look at an example: interface User
{
/** @return string */
public function getPassword();
}
class MyUser implements User
{
public function getPassword()
{
// return something
}
public function getDisplayName()
{
// return some name.
}
}
class AuthSystem
{
public function authenticate(User $user)
{
$this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
// do something.
}
}
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break. Available Fixes
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
Loading history...
|
|||
42 | } |
||
43 | |||
44 | public function pause() |
||
45 | { |
||
46 | return $this->stream->pause(); |
||
0 ignored issues
–
show
It seems like you code against a concrete implementation and not the interface
React\Stream\WritableStreamInterface as the method pause() does only exist in the following implementations of said interface: React\Stream\CompositeStream , React\Stream\DuplexResourceStream , React\Stream\ThroughStream .
Let’s take a look at an example: interface User
{
/** @return string */
public function getPassword();
}
class MyUser implements User
{
public function getPassword()
{
// return something
}
public function getDisplayName()
{
// return some name.
}
}
class AuthSystem
{
public function authenticate(User $user)
{
$this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
// do something.
}
}
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break. Available Fixes
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
Loading history...
|
|||
47 | } |
||
48 | |||
49 | public function resume() |
||
50 | { |
||
51 | return $this->stream->resume(); |
||
0 ignored issues
–
show
It seems like you code against a concrete implementation and not the interface
React\Stream\WritableStreamInterface as the method resume() does only exist in the following implementations of said interface: React\Stream\CompositeStream , React\Stream\DuplexResourceStream , React\Stream\ThroughStream .
Let’s take a look at an example: interface User
{
/** @return string */
public function getPassword();
}
class MyUser implements User
{
public function getPassword()
{
// return something
}
public function getDisplayName()
{
// return some name.
}
}
class AuthSystem
{
public function authenticate(User $user)
{
$this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
// do something.
}
}
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break. Available Fixes
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
Loading history...
|
|||
52 | } |
||
53 | |||
54 | public function pipe(WritableStreamInterface $dest, array $options = []) |
||
55 | { |
||
56 | return $this->stream->pipe($dest, $options); |
||
0 ignored issues
–
show
It seems like you code against a concrete implementation and not the interface
React\Stream\WritableStreamInterface as the method pipe() does only exist in the following implementations of said interface: React\Stream\CompositeStream , React\Stream\DuplexResourceStream , React\Stream\ThroughStream .
Let’s take a look at an example: interface User
{
/** @return string */
public function getPassword();
}
class MyUser implements User
{
public function getPassword()
{
// return something
}
public function getDisplayName()
{
// return some name.
}
}
class AuthSystem
{
public function authenticate(User $user)
{
$this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
// do something.
}
}
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break. Available Fixes
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
Loading history...
|
|||
57 | } |
||
58 | |||
59 | public function close() |
||
60 | { |
||
61 | $this->stream->close(); |
||
62 | } |
||
63 | |||
64 | private function processBuffer(): string |
||
65 | { |
||
66 | $length = strlen($this->buffer); |
||
67 | $buffer = base64_encode(substr($this->buffer, 0, $length - $length % 3)); |
||
68 | $this->buffer = substr($this->buffer, $length - $length % 3); |
||
69 | |||
70 | return $buffer; |
||
71 | } |
||
72 | } |
||
73 |
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.