ChainedBlockStream   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Test Coverage

Coverage 68.42%

Importance

Changes 0
Metric Value
wmc 26
eloc 58
c 0
b 0
f 0
dl 0
loc 175
rs 10
ccs 39
cts 57
cp 0.6842

7 Methods

Rating   Name   Duplication   Size   Complexity  
A stream_stat() 0 4 1
A stream_tell() 0 3 1
B stream_seek() 0 13 7
C stream_open() 0 55 13
A stream_eof() 0 3 1
A stream_close() 0 4 1
A stream_read() 0 9 2
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared\OLE;
4
5
use PhpOffice\PhpSpreadsheet\Exception;
6
use PhpOffice\PhpSpreadsheet\Shared\OLE;
7
8
class ChainedBlockStream
9
{
10
    /** @var mixed */
11
    public $context;
12
13
    /**
14
     * The OLE container of the file that is being read.
15
     */
16
    public ?OLE $ole = null;
17
18
    /**
19
     * Parameters specified by fopen().
20
     *
21
     * @var mixed[]
22
     */
23
    public array $params = [];
24
25
    /**
26
     * The binary data of the file.
27
     */
28
    public string $data;
29
30
    /**
31
     * The file pointer.
32
     *
33
     * @var int byte offset
34
     */
35
    public int $pos = 0;
36
37
    /**
38
     * Implements support for fopen().
39
     * For creating streams using this wrapper, use OLE_PPS_File::getStream().
40
     *
41
     * @param string $path resource name including scheme, e.g.
42
     *                                    ole-chainedblockstream://oleInstanceId=1
43
     * @param string $mode only "r" is supported
44
     * @param int $options mask of STREAM_REPORT_ERRORS and STREAM_USE_PATH
45
     * @param ?string $openedPath absolute path of the opened stream (out parameter)
46
     *
47
     * @return bool true on success
48
     */
49 5
    public function stream_open(string $path, string $mode, int $options, ?string &$openedPath): bool // @codingStandardsIgnoreLine
50
    {
51 5
        if ($mode[0] !== 'r') {
52 2
            if ($options & STREAM_REPORT_ERRORS) {
53 1
                trigger_error('Only reading is supported', E_USER_WARNING);
54
            }
55
56 2
            return false;
57
        }
58
59
        // 25 is length of "ole-chainedblockstream://"
60 3
        parse_str(substr($path, 25), $this->params);
61 3
        if (!isset($this->params['oleInstanceId'], $this->params['blockId'], $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']])) { //* @phpstan-ignore-line
62 2
            if ($options & STREAM_REPORT_ERRORS) {
63 1
                trigger_error('OLE stream not found', E_USER_WARNING);
64
            }
65
66 2
            return false;
67
        }
68 1
        $this->ole = $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']]; //* @phpstan-ignore-line
69 1
        if (!($this->ole instanceof OLE)) { //* @phpstan-ignore-line
70
            throw new Exception('class is not OLE');
71
        }
72
73 1
        $blockId = $this->params['blockId'];
74 1
        $this->data = '';
75 1
        if (isset($this->params['size']) && $this->params['size'] < $this->ole->bigBlockThreshold && $blockId != $this->ole->root->startBlock) {
76
            // Block id refers to small blocks
77
            $rootPos = $this->ole->getBlockOffset((int) $this->ole->root->startBlock);
78
            while ($blockId != -2) {
79
                /** @var int $blockId */
80
                $pos = $rootPos + $blockId * $this->ole->bigBlockSize;
81
                $blockId = $this->ole->sbat[$blockId];
82
                fseek($this->ole->_file_handle, $pos);
83
                $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
84
            }
85
        } else {
86
            // Block id refers to big blocks
87 1
            while ($blockId != -2) {
88
                /** @var int $blockId */
89 1
                $pos = $this->ole->getBlockOffset($blockId);
90 1
                fseek($this->ole->_file_handle, $pos);
91 1
                $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
92 1
                $blockId = $this->ole->bbat[$blockId];
93
            }
94
        }
95 1
        if (isset($this->params['size'])) {
96
            $this->data = substr($this->data, 0, $this->params['size']); //* @phpstan-ignore-line
97
        }
98
99 1
        if ($options & STREAM_USE_PATH) {
100
            $openedPath = $path;
101
        }
102
103 1
        return true;
104
    }
105
106
    /**
107
     * Implements support for fclose().
108
     */
109 1
    public function stream_close(): void // @codingStandardsIgnoreLine
110
    {
111 1
        $this->ole = null;
112 1
        unset($GLOBALS['_OLE_INSTANCES']);
113
    }
114
115
    /**
116
     * Implements support for fread(), fgets() etc.
117
     *
118
     * @param int $count maximum number of bytes to read
119
     *
120
     * @return false|string
121
     */
122 1
    public function stream_read(int $count): bool|string // @codingStandardsIgnoreLine
123
    {
124 1
        if ($this->stream_eof()) {
125
            return false;
126
        }
127 1
        $s = substr($this->data, (int) $this->pos, $count);
128 1
        $this->pos += $count;
129
130 1
        return $s;
131
    }
132
133
    /**
134
     * Implements support for feof().
135
     *
136
     * @return bool TRUE if the file pointer is at EOF; otherwise FALSE
137
     */
138 1
    public function stream_eof(): bool // @codingStandardsIgnoreLine
139
    {
140 1
        return $this->pos >= strlen($this->data);
141
    }
142
143
    /**
144
     * Returns the position of the file pointer, i.e. its offset into the file
145
     * stream. Implements support for ftell().
146
     */
147 1
    public function stream_tell(): int // @codingStandardsIgnoreLine
148
    {
149 1
        return $this->pos;
150
    }
151
152
    /**
153
     * Implements support for fseek().
154
     *
155
     * @param int $offset byte offset
156
     * @param int $whence SEEK_SET, SEEK_CUR or SEEK_END
157
     */
158 1
    public function stream_seek(int $offset, int $whence): bool // @codingStandardsIgnoreLine
159
    {
160 1
        if ($whence == SEEK_SET && $offset >= 0) {
161 1
            $this->pos = $offset;
162
        } elseif ($whence == SEEK_CUR && -$offset <= $this->pos) {
163
            $this->pos += $offset;
164
        } elseif ($whence == SEEK_END && -$offset <= count($this->data)) { // @phpstan-ignore-line
165
            $this->pos = strlen($this->data) + $offset;
166
        } else {
167
            return false;
168
        }
169
170 1
        return true;
171
    }
172
173
    /**
174
     * Implements support for fstat(). Currently the only supported field is
175
     * "size".
176
     *
177
     * @return array{size: int}
178
     */
179
    public function stream_stat(): array // @codingStandardsIgnoreLine
180
    {
181
        return [
182
            'size' => strlen($this->data),
183
        ];
184
    }
185
186
    // Methods used by stream_wrapper_register() that are not implemented:
187
    // bool stream_flush ( void )
188
    // int stream_write ( string data )
189
    // bool rename ( string path_from, string path_to )
190
    // bool mkdir ( string path, int mode, int options )
191
    // bool rmdir ( string path, int options )
192
    // bool dir_opendir ( string path, int options )
193
    // array url_stat ( string path, int flags )
194
    // string dir_readdir ( void )
195
    // bool dir_rewinddir ( void )
196
    // bool dir_closedir ( void )
197
}
198