Failed Conditions
Pull Request — master (#4118)
by Owen
13:53
created

Biff5   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
c 1
b 0
f 0
dl 0
loc 58
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
    public static function readBIFF5CellRangeAddressFixed(string $subData): string
17
    {
18
        // offset: 0; size: 2; index to first row
19
        $fr = self::getUInt2d($subData, 0) + 1;
20
21
        // offset: 2; size: 2; index to last row
22
        $lr = self::getUInt2d($subData, 2) + 1;
23
24
        // offset: 4; size: 1; index to first column
25
        $fc = ord($subData[4]);
26
27
        // offset: 5; size: 1; index to last column
28
        $lc = ord($subData[5]);
29
30
        // check values
31
        if ($fr > $lr || $fc > $lc) {
32
            throw new ReaderException('Not a cell range address');
33
        }
34
35
        // column index to letter
36
        $fc = Coordinate::stringFromColumnIndex($fc + 1);
37
        $lc = Coordinate::stringFromColumnIndex($lc + 1);
38
39
        if ($fr == $lr && $fc == $lc) {
40
            return "$fc$fr";
41
        }
42
43
        return "$fc$fr:$lc$lr";
44
    }
45
46
    /**
47
     * Read BIFF5 cell range address list
48
     * section 2.5.15.
49
     */
50
    public static function readBIFF5CellRangeAddressList(string $subData): array
51
    {
52
        $cellRangeAddresses = [];
53
54
        // offset: 0; size: 2; number of the following cell range addresses
55
        $nm = self::getUInt2d($subData, 0);
56
57
        $offset = 2;
58
        // offset: 2; size: 6 * $nm; list of $nm (fixed) cell range addresses
59
        for ($i = 0; $i < $nm; ++$i) {
60
            $cellRangeAddresses[] = self::readBIFF5CellRangeAddressFixed(substr($subData, $offset, 6));
61
            $offset += 6;
62
        }
63
64
        return [
65
            'size' => 2 + 6 * $nm,
66
            'cellRangeAddresses' => $cellRangeAddresses,
67
        ];
68
    }
69
}
70