Issues (158)

tests/Support/Stub/PhpStreamProxy.php (21 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Tests\Support\Stub;
6
7
use Yiisoft\Yii\Debug\Helper\StreamWrapper\StreamWrapper;
8
use Yiisoft\Yii\Debug\Helper\StreamWrapper\StreamWrapperInterface;
9
10
final class PhpStreamProxy implements StreamWrapperInterface
11
{
12
    public static bool $registered = false;
13
    /**
14
     * @var resource|null
15
     */
16
    public $context;
17
    public StreamWrapperInterface $decorated;
18
    public bool $ignored = false;
19
20
    public array $operations = [];
21
22
    public function __construct()
23
    {
24
        $this->decorated = new StreamWrapper();
25
        $this->decorated->context = $this->context;
0 ignored issues
show
Accessing context on the interface Yiisoft\Yii\Debug\Helper...\StreamWrapperInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
26
    }
27
28
    public function __destruct()
29
    {
30
        self::unregister();
31
    }
32
33
    public function __call(string $name, array $arguments)
34
    {
35
        try {
36
            self::unregister();
37
            return $this->decorated->{$name}(...$arguments);
38
        } finally {
39
            self::register();
40
        }
41
    }
42
43
    public function __get(string $name)
44
    {
45
        return $this->decorated->{$name};
46
    }
47
48
    public static function register(): void
49
    {
50
        if (self::$registered) {
51
            return;
52
        }
53
        /**
54
         * It's important to trigger autoloader before unregistering the file stream handler
55
         */
56
        class_exists(StreamWrapper::class);
57
        stream_wrapper_unregister('php');
58
        stream_wrapper_register('php', self::class, STREAM_IS_URL);
59
60
        self::$registered = true;
61
    }
62
63
    public static function unregister(): void
64
    {
65
        if (!self::$registered) {
66
            return;
67
        }
68
        @stream_wrapper_restore('php');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for stream_wrapper_restore(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

68
        /** @scrutinizer ignore-unhandled */ @stream_wrapper_restore('php');

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
69
        self::$registered = false;
70
    }
71
72
    public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool
0 ignored issues
show
Method name "PhpStreamProxy::stream_open" is not in camel caps format
Loading history...
73
    {
74
        return $this->__call(__FUNCTION__, func_get_args());
75
    }
76
77
    public function stream_read(int $count): string|false
0 ignored issues
show
Method name "PhpStreamProxy::stream_read" is not in camel caps format
Loading history...
78
    {
79
        return $this->__call(__FUNCTION__, func_get_args());
80
    }
81
82
    public function stream_set_option(int $option, int $arg1, ?int $arg2): bool
0 ignored issues
show
Method name "PhpStreamProxy::stream_set_option" is not in camel caps format
Loading history...
83
    {
84
        return $this->__call(__FUNCTION__, func_get_args());
85
    }
86
87
    public function stream_tell(): int
0 ignored issues
show
Method name "PhpStreamProxy::stream_tell" is not in camel caps format
Loading history...
88
    {
89
        return $this->__call(__FUNCTION__, func_get_args());
90
    }
91
92
    public function stream_eof(): bool
0 ignored issues
show
Method name "PhpStreamProxy::stream_eof" is not in camel caps format
Loading history...
93
    {
94
        return $this->__call(__FUNCTION__, func_get_args());
95
    }
96
97
    public function stream_seek(int $offset, int $whence = SEEK_SET): bool
0 ignored issues
show
Method name "PhpStreamProxy::stream_seek" is not in camel caps format
Loading history...
98
    {
99
        return $this->__call(__FUNCTION__, func_get_args());
100
    }
101
102
    public function stream_cast(int $castAs)
0 ignored issues
show
Method name "PhpStreamProxy::stream_cast" is not in camel caps format
Loading history...
103
    {
104
        return $this->__call(__FUNCTION__, func_get_args());
105
    }
106
107
    public function stream_stat(): array|false
0 ignored issues
show
Method name "PhpStreamProxy::stream_stat" is not in camel caps format
Loading history...
108
    {
109
        return $this->__call(__FUNCTION__, func_get_args());
110
    }
111
112
    public function dir_closedir(): bool
0 ignored issues
show
Method name "PhpStreamProxy::dir_closedir" is not in camel caps format
Loading history...
113
    {
114
        return $this->__call(__FUNCTION__, func_get_args());
115
    }
116
117
    public function dir_opendir(string $path, int $options): bool
0 ignored issues
show
Method name "PhpStreamProxy::dir_opendir" is not in camel caps format
Loading history...
118
    {
119
        return $this->__call(__FUNCTION__, func_get_args());
120
    }
121
122
    public function dir_readdir(): false|string
0 ignored issues
show
Method name "PhpStreamProxy::dir_readdir" is not in camel caps format
Loading history...
123
    {
124
        return $this->__call(__FUNCTION__, func_get_args());
125
    }
126
127
    public function dir_rewinddir(): bool
0 ignored issues
show
Method name "PhpStreamProxy::dir_rewinddir" is not in camel caps format
Loading history...
128
    {
129
        return $this->__call(__FUNCTION__, func_get_args());
130
    }
131
132
    public function mkdir(string $path, int $mode, int $options): bool
133
    {
134
        return $this->__call(__FUNCTION__, func_get_args());
135
    }
136
137
    public function rename(string $path_from, string $path_to): bool
138
    {
139
        return $this->__call(__FUNCTION__, func_get_args());
140
    }
141
142
    public function rmdir(string $path, int $options): bool
143
    {
144
        return $this->__call(__FUNCTION__, func_get_args());
145
    }
146
147
    public function stream_close(): void
0 ignored issues
show
Method name "PhpStreamProxy::stream_close" is not in camel caps format
Loading history...
148
    {
149
        $this->__call(__FUNCTION__, func_get_args());
150
    }
151
152
    public function stream_flush(): bool
0 ignored issues
show
Method name "PhpStreamProxy::stream_flush" is not in camel caps format
Loading history...
153
    {
154
        return $this->__call(__FUNCTION__, func_get_args());
155
    }
156
157
    public function stream_lock(int $operation): bool
0 ignored issues
show
Method name "PhpStreamProxy::stream_lock" is not in camel caps format
Loading history...
158
    {
159
        return $this->__call(__FUNCTION__, func_get_args());
160
    }
161
162
    public function stream_metadata(string $path, int $option, mixed $value): bool
0 ignored issues
show
Method name "PhpStreamProxy::stream_metadata" is not in camel caps format
Loading history...
163
    {
164
        return $this->__call(__FUNCTION__, func_get_args());
165
    }
166
167
    public function stream_truncate(int $new_size): bool
0 ignored issues
show
Method name "PhpStreamProxy::stream_truncate" is not in camel caps format
Loading history...
168
    {
169
        return $this->__call(__FUNCTION__, func_get_args());
170
    }
171
172
    public function stream_write(string $data): int
0 ignored issues
show
Method name "PhpStreamProxy::stream_write" is not in camel caps format
Loading history...
173
    {
174
        return $this->__call(__FUNCTION__, func_get_args());
175
    }
176
177
    public function unlink(string $path): bool
178
    {
179
        return $this->__call(__FUNCTION__, func_get_args());
180
    }
181
182
    public function url_stat(string $path, int $flags): array|false
0 ignored issues
show
Method name "PhpStreamProxy::url_stat" is not in camel caps format
Loading history...
183
    {
184
        return $this->__call(__FUNCTION__, func_get_args());
185
    }
186
}
187