StreamWrapper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 71
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A stream_open() 0 13 3
A stream_eof() 0 4 1
A stream_read() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JsonCollectionParser\Stream;
6
7
use Psr\Http\Message\StreamInterface;
8
use RuntimeException;
9
10
/**
11
 * Class StreamWrapper
12
 *
13
 * An instance of this class is initialized as soon as a stream function
14
 * tries to access the protocol it is associated with.
15
 *
16
 * @phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
17
 */
18
class StreamWrapper
19
{
20
    /** @var string */
21
    public const NAME = 'json-collection-parser';
22
23
    /**
24
     * The current context, or NULL if no context
25
     * was passed to the caller function.
26
     *
27
     * @var resource|null
28
     */
29
    public $context;
30
31
    /** @var StreamInterface */
32
    protected $stream;
33
34
    /**
35
     * Opens file or URL
36
     *
37
     * This method is called immediately after the wrapper is initialized
38
     * (f.e. by fopen() and file_get_contents()).
39
     *
40
     * @param string $path
41
     * @param string $mode
42
     * @param int    $options
43
     * @param string $opened_path
44
     *
45
     * @return bool
46
     */
47
    public function stream_open(string $path, string $mode, int $options, &$opened_path): bool
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $mode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $opened_path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
    {
49
        $options = stream_context_get_options($this->context);
50
        $stream  = $options[static::NAME]['stream'] ?? null;
51
52
        if (! $stream instanceof StreamInterface || ! $stream->isReadable()) {
53
            return false;
54
        }
55
56
        $this->stream = $stream;
57
58
        return true;
59
    }
60
61
    /**
62
     * Tests for end-of-file on a file pointer
63
     *
64
     * This method is called in response to feof().
65
     *
66
     * @return bool
67
     */
68
    public function stream_eof(): bool
69
    {
70
        return $this->stream->eof();
71
    }
72
73
    /**
74
     * Read from stream
75
     *
76
     * This method is called in response to fread() and fgets().
77
     *
78
     * @param int $count
79
     *
80
     * @throws RuntimeException if an error occurs.
81
     *
82
     * @return string
83
     */
84
    public function stream_read(int $count): string
85
    {
86
        return $this->stream->read($count);
87
    }
88
}
89