Failed Conditions
Pull Request — master (#4118)
by Owen
24:41 queued 14:40
created

Biff5   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
c 1
b 0
f 0
dl 0
loc 58
ccs 21
cts 22
cp 0.9545
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A readBIFF5CellRangeAddressFixed() 0 28 5
A readBIFF5CellRangeAddressList() 0 17 2
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Reader\Xls;
4
5
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
7
use PhpOffice\PhpSpreadsheet\Reader\Xls;
8
9
class Biff5 extends Xls
10
{
11
    /**
12
     * Reads a cell range address in BIFF5 e.g. 'A2:B6' or 'A1'
13
     * always fixed range
14
     * section 2.5.14.
15
     */
16 107
    public static function readBIFF5CellRangeAddressFixed(string $subData): string
17
    {
18
        // offset: 0; size: 2; index to first row
19 107
        $fr = self::getUInt2d($subData, 0) + 1;
20
21
        // offset: 2; size: 2; index to last row
22 107
        $lr = self::getUInt2d($subData, 2) + 1;
23
24
        // offset: 4; size: 1; index to first column
25 107
        $fc = ord($subData[4]);
26
27
        // offset: 5; size: 1; index to last column
28 107
        $lc = ord($subData[5]);
29
30
        // check values
31 107
        if ($fr > $lr || $fc > $lc) {
32
            throw new ReaderException('Not a cell range address');
33
        }
34
35
        // column index to letter
36 107
        $fc = Coordinate::stringFromColumnIndex($fc + 1);
37 107
        $lc = Coordinate::stringFromColumnIndex($lc + 1);
38
39 107
        if ($fr == $lr && $fc == $lc) {
40 87
            return "$fc$fr";
41
        }
42
43 34
        return "$fc$fr:$lc$lr";
44
    }
45
46
    /**
47
     * Read BIFF5 cell range address list
48
     * section 2.5.15.
49
     */
50 107
    public static function readBIFF5CellRangeAddressList(string $subData): array
51
    {
52 107
        $cellRangeAddresses = [];
53
54
        // offset: 0; size: 2; number of the following cell range addresses
55 107
        $nm = self::getUInt2d($subData, 0);
56
57 107
        $offset = 2;
58
        // offset: 2; size: 6 * $nm; list of $nm (fixed) cell range addresses
59 107
        for ($i = 0; $i < $nm; ++$i) {
60 107
            $cellRangeAddresses[] = self::readBIFF5CellRangeAddressFixed(substr($subData, $offset, 6));
61 107
            $offset += 6;
62
        }
63
64 107
        return [
65 107
            'size' => 2 + 6 * $nm,
66 107
            'cellRangeAddresses' => $cellRangeAddresses,
67 107
        ];
68
    }
69
}
70