Passed
Pull Request — master (#95)
by Dmitriy
02:26
created

StreamHandler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 57.58%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 28
c 1
b 0
f 1
dl 0
loc 71
ccs 19
cts 33
cp 0.5758
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A withEncoder() 0 5 1
A writeToStream() 0 3 1
A initializeStream() 0 10 3
A __construct() 0 8 3
A handle() 0 18 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\VarDumper\Handler;
6
7
use RuntimeException;
8
use Yiisoft\VarDumper\HandlerInterface;
9
10
final class StreamHandler implements HandlerInterface
11
{
12
    /**
13
     * @var callable
14
     */
15
    private $encoder;
16
    /**
17
     * @var null|resource
18
     */
19
    private $stream = null;
20
21 10
    public function __construct(
22
        private $uri = 'udp://127.0.0.1:8890'
23
    ) {
24 10
        if (!is_string($this->uri) && !is_resource($this->uri)) {
0 ignored issues
show
introduced by
The condition is_resource($this->uri) is always false.
Loading history...
introduced by
The condition is_string($this->uri) is always false.
Loading history...
25
            throw new \InvalidArgumentException(
26
                sprintf(
27
                    'Argument $uri must be a string or a resource, "%s" given.',
28
                    gettype($this->uri)
29
                )
30
            );
31
        }
32
    }
33
34
    /**
35
     * Sends encode with {@see \json_encode()} function $variable to a UDP socket.
36
     */
37 10
    public function handle(mixed $variable, int $depth, bool $highlight = false): void
38
    {
39 10
        $data = ($this->encoder ?? 'json_encode')($variable);
40 10
        if (!is_string($data)) {
41
            throw new RuntimeException(
42
                sprintf(
43
                    'Encoder must return a string, %s returned.',
44
                    gettype($data)
45
                )
46
            );
47
        }
48
49 10
        $this->initializeStream();
50
51 10
        if (!$this->writeToStream($data)) {
52
            $this->initializeStream();
53
54
            $this->writeToStream($data);
55
        }
56
    }
57
58 2
    public function withEncoder(callable $encoder): HandlerInterface
59
    {
60 2
        $new = clone $this;
61 2
        $new->encoder = $encoder;
62 2
        return $new;
63
    }
64
65 10
    private function initializeStream(): void
66
    {
67 10
        if (is_string($this->uri)) {
0 ignored issues
show
introduced by
The condition is_string($this->uri) is always false.
Loading history...
68 1
            $this->stream = fsockopen($this->uri);
69
        } else {
70 9
            $this->stream = $this->uri;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->uri of type mixed is incompatible with the declared type null|resource 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...
71
        }
72
73 10
        if (!is_resource($this->stream)) {
0 ignored issues
show
introduced by
The condition is_resource($this->stream) is always false.
Loading history...
74 1
            throw new RuntimeException('Cannot initialize a stream.');
75
        }
76
    }
77
78 10
    private function writeToStream(bool|string $data): bool
79
    {
80 10
        return @fwrite($this->stream, $data) !== false;
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type boolean; however, parameter $data of fwrite() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        return @fwrite($this->stream, /** @scrutinizer ignore-type */ $data) !== false;
Loading history...
81
    }
82
}
83