Root::saveHeader()   F
last analyzed

Complexity

Conditions 14
Paths 480

Size

Total Lines 69
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 44
CRAP Score 14.713

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 69
ccs 44
cts 52
cp 0.8462
rs 2.8222
c 0
b 0
f 0
cc 14
nc 480
nop 3
crap 14.713

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared\OLE\PPS;
4
5
// vim: set expandtab tabstop=4 shiftwidth=4:
6
// +----------------------------------------------------------------------+
7
// | PHP Version 4                                                        |
8
// +----------------------------------------------------------------------+
9
// | Copyright (c) 1997-2002 The PHP Group                                |
10
// +----------------------------------------------------------------------+
11
// | This source file is subject to version 2.02 of the PHP license,      |
12
// | that is bundled with this package in the file LICENSE, and is        |
13
// | available at through the world-wide-web at                           |
14
// | http://www.php.net/license/2_02.txt.                                 |
15
// | If you did not receive a copy of the PHP license and are unable to   |
16
// | obtain it through the world-wide-web, please send a note to          |
17
// | [email protected] so we can mail you a copy immediately.               |
18
// +----------------------------------------------------------------------+
19
// | Author: Xavier Noguer <[email protected]>                              |
20
// | Based on OLE::Storage_Lite by Kawai, Takanori                        |
21
// +----------------------------------------------------------------------+
22
//
23
use PhpOffice\PhpSpreadsheet\Shared\OLE;
24
use PhpOffice\PhpSpreadsheet\Shared\OLE\PPS;
25
26
/**
27
 * Class for creating Root PPS's for OLE containers.
28
 *
29
 * @author   Xavier Noguer <[email protected]>
30
 */
31
class Root extends PPS
32
{
33
    /**
34
     * @var resource
35
     */
36
    private $fileHandle;
37
38
    private ?int $smallBlockSize = null;
39
40
    private ?int $bigBlockSize = null;
41
42
    /**
43
     * @param null|float|int $time_1st A timestamp
44
     * @param null|float|int $time_2nd A timestamp
45
     * @param File[] $raChild
46
     */
47 100
    public function __construct($time_1st, $time_2nd, array $raChild)
48
    {
49 100
        parent::__construct(null, OLE::ascToUcs('Root Entry'), OLE::OLE_PPS_TYPE_ROOT, null, null, null, $time_1st, $time_2nd, null, $raChild);
50
    }
51
52
    /**
53
     * Method for saving the whole OLE container (including files).
54
     * In fact, if called with an empty argument (or '-'), it saves to a
55
     * temporary file and then outputs it's contents to stdout.
56
     * If a resource pointer to a stream created by fopen() is passed
57
     * it will be used, but you have to close such stream by yourself.
58
     *
59
     * @param resource $fileHandle the name of the file or stream where to save the OLE container
60
     *
61
     * @return bool true on success
62
     */
63 99
    public function save($fileHandle): bool
64
    {
65 99
        $this->fileHandle = $fileHandle;
66
67
        // Initial Setting for saving
68 99
        $this->bigBlockSize = (int) (2 ** (
69 99
            (isset($this->bigBlockSize)) ? self::adjust2($this->bigBlockSize) : 9
70 99
        ));
71 99
        $this->smallBlockSize = (int) (2 ** (
72 99
            (isset($this->smallBlockSize)) ? self::adjust2($this->smallBlockSize) : 6
73 99
        ));
74
75
        // Make an array of PPS's (for Save)
76 99
        $aList = [];
77 99
        PPS::savePpsSetPnt($aList, [$this]);
78
        // calculate values for header
79 99
        [$iSBDcnt, $iBBcnt, $iPPScnt] = $this->calcSize($aList); //, $rhInfo);
80
        // Save Header
81 99
        $this->saveHeader((int) $iSBDcnt, (int) $iBBcnt, (int) $iPPScnt);
82
83
        // Make Small Data string (write SBD)
84 99
        $this->_data = $this->makeSmallData($aList);
85
86
        // Write BB
87 99
        $this->saveBigData((int) $iSBDcnt, $aList);
88
        // Write PPS
89 99
        $this->savePps($aList);
90
        // Write Big Block Depot and BDList and Adding Header informations
91 99
        $this->saveBbd((int) $iSBDcnt, (int) $iBBcnt, (int) $iPPScnt);
92
93 99
        return true;
94
    }
95
96
    /**
97
     * Calculate some numbers.
98
     *
99
     * @param array $raList Reference to an array of PPS's
100
     *
101
     * @return float[] The array of numbers
102
     */
103 99
    private function calcSize(array &$raList): array
104
    {
105
        // Calculate Basic Setting
106 99
        [$iSBDcnt, $iBBcnt, $iPPScnt] = [0, 0, 0];
107 99
        $iSBcnt = 0;
108 99
        $iCount = count($raList);
109 99
        for ($i = 0; $i < $iCount; ++$i) {
110 99
            if ($raList[$i]->Type == OLE::OLE_PPS_TYPE_FILE) {
111 99
                $raList[$i]->Size = $raList[$i]->getDataLen();
112 99
                if ($raList[$i]->Size < OLE::OLE_DATA_SIZE_SMALL) {
113 99
                    $iSBcnt += floor($raList[$i]->Size / $this->smallBlockSize)
114 99
                        + (($raList[$i]->Size % $this->smallBlockSize) ? 1 : 0);
115
                } else {
116 29
                    $iBBcnt += (floor($raList[$i]->Size / $this->bigBlockSize)
117 29
                        + (($raList[$i]->Size % $this->bigBlockSize) ? 1 : 0));
118
                }
119
            }
120
        }
121 99
        $iSmallLen = $iSBcnt * $this->smallBlockSize;
122 99
        $iSlCnt = floor($this->bigBlockSize / OLE::OLE_LONG_INT_SIZE);
123 99
        $iSBDcnt = floor($iSBcnt / $iSlCnt) + (($iSBcnt % $iSlCnt) ? 1 : 0);
124 99
        $iBBcnt += (floor($iSmallLen / $this->bigBlockSize)
125 99
            + (($iSmallLen % $this->bigBlockSize) ? 1 : 0));
126 99
        $iCnt = count($raList);
127 99
        $iBdCnt = $this->bigBlockSize / OLE::OLE_PPS_SIZE;
128 99
        $iPPScnt = (floor($iCnt / $iBdCnt) + (($iCnt % $iBdCnt) ? 1 : 0));
129
130 99
        return [$iSBDcnt, $iBBcnt, $iPPScnt];
131
    }
132
133
    /**
134
     * Helper function for caculating a magic value for block sizes.
135
     *
136
     * @param int $i2 The argument
137
     *
138
     * @see save()
139
     */
140
    private static function adjust2(int $i2): float
141
    {
142
        $iWk = log($i2) / log(2);
143
144
        return ($iWk > floor($iWk)) ? floor($iWk) + 1 : $iWk;
145
    }
146
147
    /**
148
     * Save OLE header.
149
     */
150 99
    private function saveHeader(int $iSBDcnt, int $iBBcnt, int $iPPScnt): void
151
    {
152 99
        $FILE = $this->fileHandle;
153
154
        // Calculate Basic Setting
155 99
        $iBlCnt = $this->bigBlockSize / OLE::OLE_LONG_INT_SIZE;
156 99
        $i1stBdL = ($this->bigBlockSize - 0x4C) / OLE::OLE_LONG_INT_SIZE;
157
158 99
        $iBdExL = 0;
159 99
        $iAll = $iBBcnt + $iPPScnt + $iSBDcnt;
160 99
        $iAllW = $iAll;
161 99
        $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt) ? 1 : 0);
162 99
        $iBdCnt = floor(($iAll + $iBdCntW) / $iBlCnt) + ((($iAllW + $iBdCntW) % $iBlCnt) ? 1 : 0);
163
164
        // Calculate BD count
165 99
        if ($iBdCnt > $i1stBdL) {
166
            while (1) {
167
                ++$iBdExL;
168
                ++$iAllW;
169
                $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt) ? 1 : 0);
170
                $iBdCnt = floor(($iAllW + $iBdCntW) / $iBlCnt) + ((($iAllW + $iBdCntW) % $iBlCnt) ? 1 : 0);
171
                if ($iBdCnt <= ($iBdExL * $iBlCnt + $i1stBdL)) {
172
                    break;
173
                }
174
            }
175
        }
176
177
        // Save Header
178 99
        fwrite(
179 99
            $FILE,
180 99
            "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1"
181 99
            . "\x00\x00\x00\x00"
182 99
            . "\x00\x00\x00\x00"
183 99
            . "\x00\x00\x00\x00"
184 99
            . "\x00\x00\x00\x00"
185 99
            . pack('v', 0x3B)
186 99
            . pack('v', 0x03)
187 99
            . pack('v', -2)
188 99
            . pack('v', 9)
189 99
            . pack('v', 6)
190 99
            . pack('v', 0)
191 99
            . "\x00\x00\x00\x00"
192 99
            . "\x00\x00\x00\x00"
193 99
            . pack('V', $iBdCnt)
194 99
            . pack('V', $iBBcnt + $iSBDcnt) //ROOT START
195 99
            . pack('V', 0)
196 99
            . pack('V', 0x1000)
197 99
            . pack('V', $iSBDcnt ? 0 : -2) //Small Block Depot
198 99
            . pack('V', $iSBDcnt)
199 99
        );
200
        // Extra BDList Start, Count
201 99
        if ($iBdCnt < $i1stBdL) {
202 99
            fwrite(
203 99
                $FILE,
204 99
                pack('V', -2) // Extra BDList Start
205 99
                . pack('V', 0)// Extra BDList Count
206 99
            );
207
        } else {
208
            fwrite($FILE, pack('V', $iAll + $iBdCnt) . pack('V', $iBdExL));
209
        }
210
211
        // BDList
212 99
        for ($i = 0; $i < $i1stBdL && $i < $iBdCnt; ++$i) {
213 99
            fwrite($FILE, pack('V', $iAll + $i));
214
        }
215 99
        if ($i < $i1stBdL) {
216 99
            $jB = $i1stBdL - $i;
217 99
            for ($j = 0; $j < $jB; ++$j) {
218 99
                fwrite($FILE, (pack('V', -1)));
219
            }
220
        }
221
    }
222
223
    /**
224
     * Saving big data (PPS's with data bigger than \PhpOffice\PhpSpreadsheet\Shared\OLE::OLE_DATA_SIZE_SMALL).
225
     *
226
     * @param array $raList Reference to array of PPS's
227
     */
228 99
    private function saveBigData(int $iStBlk, array &$raList): void
229
    {
230 99
        $FILE = $this->fileHandle;
231
232
        // cycle through PPS's
233 99
        $iCount = count($raList);
234 99
        for ($i = 0; $i < $iCount; ++$i) {
235 99
            if ($raList[$i]->Type != OLE::OLE_PPS_TYPE_DIR) {
236 99
                $raList[$i]->Size = $raList[$i]->getDataLen();
237 99
                if (($raList[$i]->Size >= OLE::OLE_DATA_SIZE_SMALL) || (($raList[$i]->Type == OLE::OLE_PPS_TYPE_ROOT) && isset($raList[$i]->_data))) {
238 99
                    fwrite($FILE, $raList[$i]->_data);
239
240 99
                    if ($raList[$i]->Size % $this->bigBlockSize) {
241 91
                        fwrite($FILE, str_repeat("\x00", $this->bigBlockSize - ($raList[$i]->Size % $this->bigBlockSize)));
242
                    }
243
                    // Set For PPS
244 99
                    $raList[$i]->startBlock = $iStBlk;
245 99
                    $iStBlk
246 99
                        += (floor($raList[$i]->Size / $this->bigBlockSize)
247 99
                            + (($raList[$i]->Size % $this->bigBlockSize) ? 1 : 0));
248
                }
249
            }
250
        }
251
    }
252
253
    /**
254
     * get small data (PPS's with data smaller than \PhpOffice\PhpSpreadsheet\Shared\OLE::OLE_DATA_SIZE_SMALL).
255
     *
256
     * @param array $raList Reference to array of PPS's
257
     */
258 99
    private function makeSmallData(array &$raList): string
259
    {
260 99
        $sRes = '';
261 99
        $FILE = $this->fileHandle;
262 99
        $iSmBlk = 0;
263
264 99
        $iCount = count($raList);
265 99
        for ($i = 0; $i < $iCount; ++$i) {
266
            // Make SBD, small data string
267 99
            if ($raList[$i]->Type == OLE::OLE_PPS_TYPE_FILE) {
268 99
                if ($raList[$i]->Size <= 0) {
269
                    continue;
270
                }
271 99
                if ($raList[$i]->Size < OLE::OLE_DATA_SIZE_SMALL) {
272 99
                    $iSmbCnt = floor($raList[$i]->Size / $this->smallBlockSize)
273 99
                        + (($raList[$i]->Size % $this->smallBlockSize) ? 1 : 0);
274
                    // Add to SBD
275 99
                    $jB = $iSmbCnt - 1;
276 99
                    for ($j = 0; $j < $jB; ++$j) {
277 99
                        fwrite($FILE, pack('V', $j + $iSmBlk + 1));
278
                    }
279 99
                    fwrite($FILE, pack('V', -2));
280
281
                    // Add to Data String(this will be written for RootEntry)
282 99
                    $sRes .= $raList[$i]->_data;
283 99
                    if ($raList[$i]->Size % $this->smallBlockSize) {
284 99
                        $sRes .= str_repeat("\x00", $this->smallBlockSize - ($raList[$i]->Size % $this->smallBlockSize));
285
                    }
286
                    // Set for PPS
287 99
                    $raList[$i]->startBlock = $iSmBlk;
288 99
                    $iSmBlk += $iSmbCnt;
289
                }
290
            }
291
        }
292 99
        $iSbCnt = floor($this->bigBlockSize / OLE::OLE_LONG_INT_SIZE);
293 99
        if ($iSmBlk % $iSbCnt) {
294 99
            $iB = $iSbCnt - ($iSmBlk % $iSbCnt);
295 99
            for ($i = 0; $i < $iB; ++$i) {
296 99
                fwrite($FILE, pack('V', -1));
297
            }
298
        }
299
300 99
        return $sRes;
301
    }
302
303
    /**
304
     * Saves all the PPS's WKs.
305
     *
306
     * @param array $raList Reference to an array with all PPS's
307
     */
308 99
    private function savePps(array &$raList): void
309
    {
310
        // Save each PPS WK
311 99
        $iC = count($raList);
312 99
        for ($i = 0; $i < $iC; ++$i) {
313 99
            fwrite($this->fileHandle, $raList[$i]->getPpsWk());
314
        }
315
        // Adjust for Block
316 99
        $iCnt = count($raList);
317 99
        $iBCnt = $this->bigBlockSize / OLE::OLE_PPS_SIZE;
318 99
        if ($iCnt % $iBCnt) {
319
            fwrite($this->fileHandle, str_repeat("\x00", ($iBCnt - ($iCnt % $iBCnt)) * OLE::OLE_PPS_SIZE));
320
        }
321
    }
322
323
    /**
324
     * Saving Big Block Depot.
325
     */
326 99
    private function saveBbd(int $iSbdSize, int $iBsize, int $iPpsCnt): void
327
    {
328 99
        $FILE = $this->fileHandle;
329
        // Calculate Basic Setting
330 99
        $iBbCnt = $this->bigBlockSize / OLE::OLE_LONG_INT_SIZE;
331 99
        $i1stBdL = ($this->bigBlockSize - 0x4C) / OLE::OLE_LONG_INT_SIZE;
332
333 99
        $iBdExL = 0;
334 99
        $iAll = $iBsize + $iPpsCnt + $iSbdSize;
335 99
        $iAllW = $iAll;
336 99
        $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt) ? 1 : 0);
337 99
        $iBdCnt = floor(($iAll + $iBdCntW) / $iBbCnt) + ((($iAllW + $iBdCntW) % $iBbCnt) ? 1 : 0);
338
        // Calculate BD count
339 99
        if ($iBdCnt > $i1stBdL) {
340
            while (1) {
341
                ++$iBdExL;
342
                ++$iAllW;
343
                $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt) ? 1 : 0);
344
                $iBdCnt = floor(($iAllW + $iBdCntW) / $iBbCnt) + ((($iAllW + $iBdCntW) % $iBbCnt) ? 1 : 0);
345
                if ($iBdCnt <= ($iBdExL * $iBbCnt + $i1stBdL)) {
346
                    break;
347
                }
348
            }
349
        }
350
351
        // Making BD
352
        // Set for SBD
353 99
        if ($iSbdSize > 0) {
354 99
            for ($i = 0; $i < ($iSbdSize - 1); ++$i) {
355
                fwrite($FILE, pack('V', $i + 1));
356
            }
357 99
            fwrite($FILE, pack('V', -2));
358
        }
359
        // Set for B
360 99
        for ($i = 0; $i < ($iBsize - 1); ++$i) {
361 99
            fwrite($FILE, pack('V', $i + $iSbdSize + 1));
362
        }
363 99
        fwrite($FILE, pack('V', -2));
364
365
        // Set for PPS
366 99
        for ($i = 0; $i < ($iPpsCnt - 1); ++$i) {
367
            fwrite($FILE, pack('V', $i + $iSbdSize + $iBsize + 1));
368
        }
369 99
        fwrite($FILE, pack('V', -2));
370
        // Set for BBD itself ( 0xFFFFFFFD : BBD)
371 99
        for ($i = 0; $i < $iBdCnt; ++$i) {
372 99
            fwrite($FILE, pack('V', 0xFFFFFFFD));
373
        }
374
        // Set for ExtraBDList
375 99
        for ($i = 0; $i < $iBdExL; ++$i) {
376
            fwrite($FILE, pack('V', 0xFFFFFFFC));
377
        }
378
        // Adjust for Block
379 99
        if (($iAllW + $iBdCnt) % $iBbCnt) {
380 99
            $iBlock = ($iBbCnt - (($iAllW + $iBdCnt) % $iBbCnt));
381 99
            for ($i = 0; $i < $iBlock; ++$i) {
382 99
                fwrite($FILE, pack('V', -1));
383
            }
384
        }
385
        // Extra BDList
386 99
        if ($iBdCnt > $i1stBdL) {
387
            $iN = 0;
388
            $iNb = 0;
389
            for ($i = $i1stBdL; $i < $iBdCnt; $i++, ++$iN) {
390
                if ($iN >= ($iBbCnt - 1)) {
391
                    $iN = 0;
392
                    ++$iNb;
393
                    fwrite($FILE, pack('V', $iAll + $iBdCnt + $iNb));
394
                }
395
                fwrite($FILE, pack('V', $iBsize + $iSbdSize + $iPpsCnt + $i));
396
            }
397
            if (($iBdCnt - $i1stBdL) % ($iBbCnt - 1)) {
398
                $iB = ($iBbCnt - 1) - (($iBdCnt - $i1stBdL) % ($iBbCnt - 1));
399
                for ($i = 0; $i < $iB; ++$i) {
400
                    fwrite($FILE, pack('V', -1));
401
                }
402
            }
403
            fwrite($FILE, pack('V', -2));
404
        }
405
    }
406
}
407