Passed
Pull Request — master (#186)
by Alexander
02:41
created

StreamWrapper::stream_flush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
    private const STREAM_OPEN_FOR_INCLUDE = 128;
19
20
    /**
21
     * @var resource|null
22
     */
23
    public mixed $context = null;
24
25
    public ?string $filename = null;
26
27
    /**
28
     * @var resource|null
29
     */
30
    public $stream = null;
31
32
    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...
33
    {
34
        closedir($this->stream);
35
        return is_resource($this->stream);
36
    }
37
38
    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...
39
    {
40
        $this->filename = $path;
41
        $this->stream = opendir($path, $this->context);
42
        return is_resource($this->stream);
43
    }
44
45
    public function dir_readdir(): false|string
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::dir_readdir" is not in camel caps format
Loading history...
46
    {
47
        return readdir($this->stream);
48
    }
49
50
    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...
51
    {
52
        if (!is_resource($this->stream)) {
53
            return false;
54
        }
55
56
        return rewinddir($this->stream);
0 ignored issues
show
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...
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...
57
    }
58
59
    public function mkdir(string $path, int $mode, int $options): bool
60
    {
61
        $this->filename = $path;
62
        return mkdir($path, $mode, ($options & STREAM_MKDIR_RECURSIVE) === STREAM_MKDIR_RECURSIVE, $this->context);
63
    }
64
65
    public function rename(string $path_from, string $path_to): bool
66
    {
67
        return rename($path_from, $path_to, $this->context);
68
    }
69
70
    public function rmdir(string $path, int $options): bool
71
    {
72
        return rmdir($path, $this->context);
73
    }
74
75
    public function stream_cast(int $castAs)
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_cast" is not in camel caps format
Loading history...
76
    {
77
        //????
78
    }
79
80
    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...
81
    {
82
        return feof($this->stream);
83
    }
84
85
    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...
86
    {
87
        $this->filename = realpath($path) ?: $path;
88
89
        if ((self::STREAM_OPEN_FOR_INCLUDE & $options) === self::STREAM_OPEN_FOR_INCLUDE && function_exists('opcache_invalidate')) {
90
            opcache_invalidate($path, false);
91
        }
92
        $this->stream = fopen(
93
            $path,
94
            $mode,
95
            ($options & STREAM_USE_PATH) === STREAM_USE_PATH,
96
            (self::STREAM_OPEN_FOR_INCLUDE & $options) === self::STREAM_OPEN_FOR_INCLUDE ? null : $this->context
97
        );
98
99
        if (!is_resource($this->stream)) {
100
            return false;
101
        }
102
103
        if ($opened_path !== null) {
104
            $metaData = stream_get_meta_data($this->stream);
105
            $opened_path = $metaData['uri'];
106
        }
107
        return true;
108
    }
109
110
    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...
111
    {
112
        return fread($this->stream, $count);
113
    }
114
115
    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...
116
    {
117
        return fseek($this->stream, $offset, $whence) !== -1;
118
    }
119
120
    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...
121
    {
122
        return match ($option) {
123
            STREAM_OPTION_BLOCKING => stream_set_blocking($this->stream, $arg1 === STREAM_OPTION_BLOCKING),
124
            STREAM_OPTION_READ_TIMEOUT => stream_set_timeout($this->stream, $arg1, $arg2),
125
            STREAM_OPTION_WRITE_BUFFER => stream_set_write_buffer($this->stream, $arg2) === 0,
126
            default => false,
127
        };
128
    }
129
130
    public function stream_stat(): array|false
0 ignored issues
show
Coding Style introduced by
Method name "StreamWrapper::stream_stat" is not in camel caps format
Loading history...
131
    {
132
        return fstat($this->stream);
133
    }
134
135
    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...
136
    {
137
        return ftell($this->stream);
138
    }
139
140
    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...
141
    {
142
        return fwrite($this->stream, $data);
143
    }
144
145
    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...
146
    {
147
        try {
148
            if (($flags & STREAM_URL_STAT_QUIET) === STREAM_URL_STAT_QUIET) {
149
                return @stat($path);
150
            }
151
            return stat($path);
152
        } catch (Throwable $e) {
153
            if (($flags & STREAM_URL_STAT_QUIET) === STREAM_URL_STAT_QUIET) {
154
                return false;
155
            }
156
            trigger_error($e->getMessage(), E_USER_ERROR);
157
        }
158
159
        return false;
160
    }
161
162
    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...
163
    {
164
        return match ($option) {
165
            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

165
            STREAM_META_TOUCH => touch($path, /** @scrutinizer ignore-type */ ...$value),
Loading history...
166
            STREAM_META_OWNER_NAME, STREAM_META_OWNER => chown($path, $value),
167
            STREAM_META_GROUP_NAME, STREAM_META_GROUP => chgrp($path, $value),
168
            STREAM_META_ACCESS => chmod($path, $value),
169
            default => false
170
        };
171
    }
172
173
    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...
174
    {
175
        return fflush($this->stream);
176
    }
177
178
    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...
179
    {
180
        /**
181
         * @psalm-suppress InvalidPropertyAssignmentValue
182
         */
183
        fclose($this->stream);
184
        $this->stream = null;
185
    }
186
187
    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...
188
    {
189
        if ($operation === 0) {
190
            $operation = LOCK_EX;
191
        }
192
        return flock($this->stream, $operation);
193
    }
194
195
    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...
196
    {
197
        return ftruncate($this->stream, $new_size);
198
    }
199
200
    public function unlink(string $path): bool
201
    {
202
        return unlink($path, $this->context);
203
    }
204
}
205