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

StreamHandler   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
eloc 40
c 5
b 1
f 1
dl 0
loc 97
rs 10
wmc 20

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 4
A __destruct() 0 6 3
A withEncoder() 0 5 1
B initializeStream() 0 19 7
A handle() 0 22 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\VarDumper\Handler;
6
7
use InvalidArgumentException;
8
use RuntimeException;
9
use Socket;
10
use Yiisoft\VarDumper\HandlerInterface;
11
12
use function fsockopen;
13
use function fwrite;
14
use function get_debug_type;
15
use function is_resource;
16
use function is_string;
17
18
final class StreamHandler implements HandlerInterface
19
{
20
    /**
21
     * @var callable|null
22
     */
23
    private $encoder = null;
24
    /**
25
     * @var resource|Socket|null
26
     */
27
    private $stream = null;
28
29
    /**
30
     * @var resource|Socket|string|null
31
     */
32
    private $uri = null;
33
34
    /**
35
     * @param mixed|resource|string $uri
36
     */
37
    public function __construct(
38
        mixed $uri = 'udp://127.0.0.1:8890'
39
    ) {
40
        if (!is_string($uri) && !is_resource($uri) && !$uri instanceof Socket) {
41
            throw new InvalidArgumentException(
42
                sprintf(
43
                    'Argument $uri must be a string or a resource, "%s" given.',
44
                    get_debug_type($uri)
45
                )
46
            );
47
        }
48
        $this->uri = $uri;
49
    }
50
51
    /**
52
     * Encodes {@param $variable} with {@see self::$encoder} and sends the result to the stream.
0 ignored issues
show
Bug introduced by
The type Yiisoft\VarDumper\Handler\with was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
53
     */
54
    public function handle(mixed $variable, int $depth, bool $highlight = false): void
55
    {
56
        $data = ($this->encoder ?? '\json_encode')($variable);
57
        if (!is_string($data)) {
58
            throw new RuntimeException(
59
                sprintf(
60
                    'Encoder must return a string, "%s" returned.',
61
                    get_debug_type($data)
62
                )
63
            );
64
        }
65
66
67
        if (!is_resource($this->stream)) {
68
            $this->initializeStream();
69
        }
70
71
        if (@fwrite($this->stream, $data) === false) {
0 ignored issues
show
Bug introduced by
It seems like $this->stream can also be of type Socket; however, parameter $stream of fwrite() does only seem to accept resource, 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

71
        if (@fwrite(/** @scrutinizer ignore-type */ $this->stream, $data) === false) {
Loading history...
72
            $this->initializeStream();
73
74
            if (@fwrite($this->stream, $data) === false) {
75
                throw new RuntimeException('Cannot write a stream.');
76
            }
77
        }
78
    }
79
80
    public function withEncoder(callable $encoder): HandlerInterface
81
    {
82
        $new = clone $this;
83
        $new->encoder = $encoder;
84
        return $new;
85
    }
86
87
    private function initializeStream(): void
88
    {
89
        if (is_string($this->uri)) {
90
            if (
91
                str_starts_with($this->uri, 'udp://') ||
92
                str_starts_with($this->uri, 'udg://') ||
93
                str_starts_with($this->uri, 'tcp://') ||
94
                str_starts_with($this->uri, 'unix://')
95
            ) {
96
                $this->stream = fsockopen($this->uri);
97
            } else {
98
                $this->stream = fopen($this->uri, 'wb+');
99
            }
100
        } else {
101
            $this->stream = $this->uri;
102
        }
103
104
        if (!is_resource($this->stream)) {
105
            throw new RuntimeException('Cannot initialize a stream.');
106
        }
107
    }
108
109
    public function __destruct()
110
    {
111
        if (!is_string($this->uri) || !is_resource($this->stream)) {
112
            return;
113
        }
114
        fclose($this->stream);
115
    }
116
}
117