Fsp   C
last analyzed

Complexity

Total Complexity 55

Size/Duplication

Total Lines 336
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 149
dl 0
loc 336
rs 6
c 0
b 0
f 0
wmc 55

28 Methods

Rating   Name   Duplication   Size   Complexity  
A stream_metadata() 0 7 2
A mkdir() 0 7 2
A __destruct() 0 6 2
A stream_close() 0 6 2
A dir_rewinddir() 0 7 2
A stream_lock() 0 7 2
A url_stat() 0 22 3
A register() 0 6 2
A errorReport() 0 4 2
A stream_cast() 0 7 2
A stream_stat() 0 7 2
A __construct() 0 6 1
A dir_readdir() 0 7 2
A unlink() 0 7 2
A dir_opendir() 0 7 2
A stream_tell() 0 7 2
A dir_closedir() 0 7 2
A stream_truncate() 0 7 2
A stream_eof() 0 7 2
A stream_seek() 0 7 2
A stream_write() 0 7 2
A rename() 0 7 2
A stream_read() 0 7 2
A stream_set_option() 0 7 2
A stream_open() 0 8 2
A stream_flush() 0 7 2
A rmdir() 0 7 2
A canReport() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Fsp often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Fsp, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace kalanis\RemoteRequest\Wrappers;
4
5
6
use kalanis\RemoteRequest\RequestException;
7
use kalanis\RemoteRequest\Translations;
8
9
10
/**
11
 * Class Fsp
12
 * @package kalanis\RemoteRequest\Wrappers
13
 * Wrapper to plug FSP info into PHP
14
 * - for direct call fsp via php - the connection itself is in libraries outside
15
 * @link https://www.php.net/manual/en/class.streamwrapper.php
16
 * @link https://www.php.net/manual/en/stream.streamwrapper.example-1.php
17
 *
18
 * Usage:
19
 * - In initialization:
20
kalanis\RemoteRequest\Wrappers\Fsp::register();
21
 * - somewhere in code:
22
file_get_contents('fsp://user:pass@server:12345/dir/file');
23
 */
24
class Fsp
25
{
26
    /** @var resource */
27
    public $context;
28
29
    protected Fsp\Runner $runner;
30
    protected Fsp\Dir $dir;
31
    protected Fsp\File $file;
32
    protected bool $showErrors = true;
33
34
    public static function register(): void
35
    {
36
        if (in_array("fsp", stream_get_wrappers())) {
37
            stream_wrapper_unregister("fsp");
38
        }
39
        stream_wrapper_register("fsp", Fsp::class);
40
    }
41
42
    /**
43
     * @throws RequestException
44
     */
45
    public function __construct()
46
    {
47
        $lang = new Translations();
48
        $this->runner = new Fsp\Runner($lang);
49
        $this->dir = new Fsp\Dir($this->runner, $lang);
50
        $this->file = new Fsp\File($this->runner, $lang);
51
    }
52
53
    public function __destruct()
54
    {
55
        try {
56
            $this->runner->__destruct();
57
        } catch (RequestException $ex) {
58
            $this->errorReport($ex);
59
        }
60
    }
61
62
    public function dir_closedir(): bool
63
    {
64
        try {
65
            return $this->dir->close();
66
        } catch (RequestException $ex) {
67
            $this->errorReport($ex);
68
            return false;
69
        }
70
    }
71
72
    public function dir_opendir(string $path, int $options): bool
73
    {
74
        try {
75
            return $this->dir->open($path, $options);
76
        } catch (RequestException $ex) {
77
            $this->errorReport($ex);
78
            return false;
79
        }
80
    }
81
82
    /**
83
     * @return string|bool
84
     */
85
    public function dir_readdir()
86
    {
87
        try {
88
            return $this->dir->read();
89
        } catch (RequestException $ex) {
90
            $this->errorReport($ex);
91
            return false;
92
        }
93
    }
94
95
    public function dir_rewinddir(): bool
96
    {
97
        try {
98
            return $this->dir->rewind();
99
        } catch (RequestException $ex) {
100
            $this->errorReport($ex);
101
            return false;
102
        }
103
    }
104
105
    /**
106
     * @param string $path
107
     * @param int $mode
108
     * @param int $options
109
     * @return bool
110
     */
111
    public function mkdir(string $path, int $mode, int $options): bool
112
    {
113
        try {
114
            return $this->dir->make($path, $mode, $options);
115
        } catch (RequestException $ex) {
116
            $this->errorReport($ex);
117
            return false;
118
        }
119
    }
120
121
    /**
122
     * @param string $path_from
123
     * @param string $path_to
124
     * @return bool
125
     */
126
    public function rename(string $path_from, string $path_to): bool
127
    {
128
        try {
129
            return $this->dir->rename($path_from, $path_to);
130
        } catch (RequestException $ex) {
131
            $this->errorReport($ex);
132
            return false;
133
        }
134
    }
135
136
    /**
137
     * @param string $path
138
     * @param int $options
139
     * @return bool
140
     */
141
    public function rmdir(string $path, int $options): bool
142
    {
143
        try {
144
            return $this->dir->remove($path, $options);
145
        } catch (RequestException $ex) {
146
            $this->errorReport($ex);
147
            return false;
148
        }
149
    }
150
151
    /**
152
     * @param int $cast_as
153
     * @return resource|bool
154
     */
155
    public function stream_cast(int $cast_as)
156
    {
157
        try {
158
            return $this->file->stream_cast($cast_as);
159
        } catch (RequestException $ex) {
160
            $this->errorReport($ex);
161
            return false;
162
        }
163
    }
164
165
    public function stream_close(): void
166
    {
167
        try {
168
            $this->file->stream_close();
169
        } catch (RequestException $ex) {
170
            $this->errorReport($ex);
171
        }
172
    }
173
174
    public function stream_eof(): bool
175
    {
176
        try {
177
            return $this->file->stream_eof();
178
        } catch (RequestException $ex) {
179
            $this->errorReport($ex);
180
            return true;
181
        }
182
    }
183
184
    public function stream_flush(): bool
185
    {
186
        try {
187
            return $this->file->stream_flush();
188
        } catch (RequestException $ex) {
189
            $this->errorReport($ex);
190
            return false;
191
        }
192
    }
193
194
    public function stream_lock(int $operation): bool
195
    {
196
        try {
197
            return $this->file->stream_lock($operation);
198
        } catch (RequestException $ex) {
199
            $this->errorReport($ex);
200
            return false;
201
        }
202
    }
203
204
    /**
205
     * @param string $path
206
     * @param int $option
207
     * @param mixed $var
208
     * @return bool
209
     */
210
    public function stream_metadata(string $path, int $option, $var): bool
211
    {
212
        try {
213
            return $this->file->stream_metadata($path, $option, $var);
214
        } catch (RequestException $ex) {
215
            $this->errorReport($ex);
216
            return false;
217
        }
218
    }
219
220
    public function stream_open(string $path, string $mode, int $options, /** @scrutinizer ignore-unused */ string &$opened_path): bool
221
    {
222
        try {
223
            $this->canReport($options);
224
            return $this->file->stream_open($this->dir, $path, $mode);
225
        } catch (RequestException $ex) {
226
            $this->errorReport($ex);
227
            return false;
228
        }
229
    }
230
231
    public function stream_read(int $count): string
232
    {
233
        try {
234
            return $this->file->stream_read($count);
235
        } catch (RequestException $ex) {
236
            $this->errorReport($ex);
237
            return '';
238
        }
239
    }
240
241
    public function stream_seek(int $offset, int $whence = SEEK_SET): bool
242
    {
243
        try {
244
            return $this->file->stream_seek($offset, $whence);
245
        } catch (RequestException $ex) {
246
            $this->errorReport($ex);
247
            return false;
248
        }
249
    }
250
251
    public function stream_set_option(int $option, int $arg1, int $arg2): bool
252
    {
253
        try {
254
            return $this->file->stream_set_option($option, $arg1, $arg2);
255
        } catch (RequestException $ex) {
256
            $this->errorReport($ex);
257
            return false;
258
        }
259
    }
260
261
    /**
262
     * @return array<int, int>
263
     */
264
    public function stream_stat(): array
265
    {
266
        try {
267
            return $this->file->stream_stat($this->dir);
268
        } catch (RequestException $ex) {
269
            $this->errorReport($ex);
270
            return [];
271
        }
272
    }
273
274
    public function stream_tell(): int
275
    {
276
        try {
277
            return $this->file->stream_tell();
278
        } catch (RequestException $ex) {
279
            $this->errorReport($ex);
280
            return -1;
281
        }
282
    }
283
284
    public function stream_truncate(int $new_size): bool
285
    {
286
        try {
287
            return $this->file->stream_truncate($new_size);
288
        } catch (RequestException $ex) {
289
            $this->errorReport($ex);
290
            return false;
291
        }
292
    }
293
294
    public function stream_write(string $data): int
295
    {
296
        try {
297
            return $this->file->stream_write($data);
298
        } catch (RequestException $ex) {
299
            $this->errorReport($ex);
300
            return 0;
301
        }
302
    }
303
304
    /**
305
     * @param string $path
306
     * @return bool
307
     */
308
    public function unlink(string $path): bool
309
    {
310
        try {
311
            return $this->file->unlink($path);
312
        } catch (RequestException $ex) {
313
            $this->errorReport($ex);
314
            return false;
315
        }
316
    }
317
318
    protected function canReport(int $opts): void
319
    {
320
        $this->showErrors = boolval($opts & STREAM_REPORT_ERRORS);
321
    }
322
323
    /**
324
     * @param RequestException $ex
325
     */
326
    protected function errorReport(RequestException $ex): void
327
    {
328
        if ($this->showErrors) {
329
            trigger_error($ex->getMessage(), E_USER_ERROR);
330
        }
331
    }
332
333
    /**
334
     * @param string $path
335
     * @param int $flags
336
     * @return array<int, int>
337
     */
338
    public function url_stat(string $path, int $flags): array
339
    {
340
        try {
341
            return $this->dir->stats($path, $flags);
342
        } catch (RequestException $ex) {
343
            if ($flags & ~STREAM_URL_STAT_QUIET) {
344
                trigger_error($ex->getMessage(), E_USER_ERROR);
345
            }
346
            return [
347
                0 => 0,
348
                1 => 0,
349
                2 => 0,
350
                3 => 0,
351
                4 => 0,
352
                5 => 0,
353
                6 => 0,
354
                7 => 0,
355
                8 => 0,
356
                9 => 0,
357
                10 => 0,
358
                11 => -1,
359
                12 => -1,
360
            ];
361
        }
362
    }
363
}
364