Passed
Pull Request — master (#186)
by Dmitriy
05:22 queued 02:51
created

StreamWrapper   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 27
eloc 43
c 1
b 0
f 0
dl 0
loc 151
rs 10

23 Methods

Rating   Name   Duplication   Size   Complexity  
A stream_set_option() 0 7 1
A stream_open() 0 6 2
A stream_flush() 0 3 1
A stream_read() 0 3 2
A url_stat() 0 12 3
A stream_close() 0 4 1
A stream_write() 0 3 1
A stream_eof() 0 3 1
A rename() 0 3 1
A mkdir() 0 3 1
A stream_tell() 0 3 1
A dir_rewinddir() 0 3 1
A stream_lock() 0 3 1
A stream_seek() 0 3 1
A dir_readdir() 0 3 1
A dir_opendir() 0 3 1
A stream_truncate() 0 3 1
A dir_closedir() 0 3 1
A stream_cast() 0 2 1
A stream_stat() 0 3 1
A stream_metadata() 0 8 1
A unlink() 0 3 1
A rmdir() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Helper\StreamWrapper;
6
7
use Throwable;
8
9
use function trigger_error;
10
11
use const E_USER_ERROR;
12
use const STREAM_MKDIR_RECURSIVE;
13
use const STREAM_URL_STAT_QUIET;
14
use const STREAM_USE_PATH;
15
16
final class StreamWrapper implements StreamWrapperInterface
17
{
18
    /**
19
     * @var resource|null
20
     */
21
    public mixed $context = null;
22
23
    public ?string $filename = null;
24
25
    /**
26
     * @var resource|null
27
     */
28
    public $stream = null;
29
30
    public function dir_closedir(): bool
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::dir_closedir" is not in camel caps format
Loading history...
31
    {
32
        return true;
33
    }
34
35
    public function dir_opendir(string $path, int $options): bool
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::dir_opendir" is not in camel caps format
Loading history...
36
    {
37
        return true;
38
    }
39
40
    public function dir_readdir(): string
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::dir_readdir" is not in camel caps format
Loading history...
41
    {
42
        return readdir($this->stream);
43
    }
44
45
    public function dir_rewinddir(): bool
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::dir_rewinddir" is not in camel caps format
Loading history...
46
    {
47
        return rewinddir($this->stream);
0 ignored issues
show
Bug introduced by
Are you sure the usage of rewinddir($this->stream) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return rewinddir($this->stream) returns the type void which is incompatible with the type-hinted return boolean.
Loading history...
48
    }
49
50
    public function mkdir(string $path, int $mode, int $options): bool
51
    {
52
        return mkdir($path, $mode, ($options & STREAM_MKDIR_RECURSIVE) === STREAM_MKDIR_RECURSIVE);
53
    }
54
55
    public function rename(string $path_from, string $path_to): bool
56
    {
57
        return rename($path_from, $path_to);
58
    }
59
60
    public function rmdir(string $path, int $options): bool
61
    {
62
        return rmdir($path);
63
    }
64
65
    public function stream_cast(int $cast_as)
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_cast" is not in camel caps format
Loading history...
66
    {
67
        //????
68
    }
69
70
    public function stream_eof(): bool
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_eof" is not in camel caps format
Loading history...
71
    {
72
        return feof($this->stream);
73
    }
74
75
    public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_open" is not in camel caps format
Loading history...
76
    {
77
        $this->filename = realpath($path) ?: $path;
78
        $this->stream = fopen($path, $mode, ($options & STREAM_USE_PATH) === STREAM_USE_PATH);
79
80
        return $this->stream !== false;
81
    }
82
83
    public function stream_read(int $count): string|false
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_read" is not in camel caps format
Loading history...
84
    {
85
        return is_readable($this->filename) ? fread($this->stream, $count) : false;
0 ignored issues
show
Bug introduced by
It seems like $this->filename can also be of type null; however, parameter $filename of is_readable() 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

85
        return is_readable(/** @scrutinizer ignore-type */ $this->filename) ? fread($this->stream, $count) : false;
Loading history...
86
    }
87
88
    public function stream_seek(int $offset, int $whence = SEEK_SET): bool
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_seek" is not in camel caps format
Loading history...
89
    {
90
        return fseek($this->stream, $offset, $whence) === 0;
91
    }
92
93
    public function stream_set_option(int $option, int $arg1, ?int $arg2): bool
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_set_option" is not in camel caps format
Loading history...
94
    {
95
        return match ($option) {
96
            STREAM_OPTION_BLOCKING => stream_set_blocking($this->stream, $arg1 === STREAM_OPTION_BLOCKING),
97
            STREAM_OPTION_READ_TIMEOUT => stream_set_timeout($this->stream, $arg1, $arg2),
0 ignored issues
show
Bug introduced by
It seems like $arg2 can also be of type null; however, parameter $microseconds of stream_set_timeout() does only seem to accept integer, 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

97
            STREAM_OPTION_READ_TIMEOUT => stream_set_timeout($this->stream, $arg1, /** @scrutinizer ignore-type */ $arg2),
Loading history...
98
            STREAM_OPTION_WRITE_BUFFER => stream_set_write_buffer($this->stream, $arg2) === 0,
0 ignored issues
show
Bug introduced by
It seems like $arg2 can also be of type null; however, parameter $size of stream_set_write_buffer() does only seem to accept integer, 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

98
            STREAM_OPTION_WRITE_BUFFER => stream_set_write_buffer($this->stream, /** @scrutinizer ignore-type */ $arg2) === 0,
Loading history...
99
            default => false,
100
        };
101
    }
102
103
    public function stream_stat(): array
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_stat" is not in camel caps format
Loading history...
104
    {
105
        return fstat($this->stream);
106
    }
107
108
    public function stream_tell(): int
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_tell" is not in camel caps format
Loading history...
109
    {
110
        return ftell($this->stream);
111
    }
112
113
    public function stream_write(string $data): int
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_write" is not in camel caps format
Loading history...
114
    {
115
        return fwrite($this->stream, $data);
116
    }
117
118
    public function url_stat(string $path, int $flags): array|false
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::url_stat" is not in camel caps format
Loading history...
119
    {
120
        try {
121
            return stat($path);
122
        } catch (Throwable $e) {
123
            if (($flags & STREAM_URL_STAT_QUIET) === STREAM_URL_STAT_QUIET) {
124
                return false;
125
            }
126
            trigger_error($e->getMessage(), E_USER_ERROR);
127
        }
128
129
        return false;
130
    }
131
132
    public function stream_metadata(string $path, int $option, mixed $value): bool
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_metadata" is not in camel caps format
Loading history...
133
    {
134
        return match ($option) {
135
            STREAM_META_TOUCH => touch($path, ...$value),
0 ignored issues
show
Bug introduced by
$value is expanded, but the parameter $mtime of touch() does not expect variable arguments. ( Ignorable by Annotation )

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

135
            STREAM_META_TOUCH => touch($path, /** @scrutinizer ignore-type */ ...$value),
Loading history...
136
            STREAM_META_OWNER_NAME, STREAM_META_OWNER => chown($path, $value),
137
            STREAM_META_GROUP_NAME, STREAM_META_GROUP => chgrp($path, $value),
138
            STREAM_META_ACCESS => chmod($path, $value),
139
            default => false
140
        };
141
    }
142
143
    public function stream_flush(): bool
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_flush" is not in camel caps format
Loading history...
144
    {
145
        return fflush($this->stream);
146
    }
147
148
    public function stream_close(): void
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_close" is not in camel caps format
Loading history...
149
    {
150
        fclose($this->stream);
151
        $this->stream = null;
152
    }
153
154
    public function stream_lock(int $operation): bool
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_lock" is not in camel caps format
Loading history...
155
    {
156
        return flock($this->stream, $operation);
157
    }
158
159
    public function stream_truncate(int $new_size): bool
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_truncate" is not in camel caps format
Loading history...
160
    {
161
        return ftruncate($this->stream, $new_size);
162
    }
163
164
    public function unlink(string $path): bool
165
    {
166
        return unlink($path, $this->context);
167
    }
168
}
169