Passed
Pull Request — master (#4279)
by Owen
18:14 queued 05:25
created

ListFunctions::listWorksheetInfo2()   C

Complexity

Conditions 14
Paths 15

Size

Total Lines 96
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 53
CRAP Score 14.0012

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 96
ccs 53
cts 54
cp 0.9815
rs 6.2666
cc 14
nc 15
nop 2
crap 14.0012

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\Reader\Xls;
4
5
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6
use PhpOffice\PhpSpreadsheet\Reader\Xls;
7
use PhpOffice\PhpSpreadsheet\Shared\File;
8
9
class ListFunctions extends Xls
10
{
11
    /**
12
     * Reads names of the worksheets from a file, without parsing the whole file to a PhpSpreadsheet object.
13
     */
14 6
    protected function listWorksheetNames2(string $filename, Xls $xls): array
15
    {
16 6
        File::assertFile($filename);
17
18 6
        $worksheetNames = [];
19
20
        // Read the OLE file
21 6
        $xls->loadOLE($filename);
22
23
        // total byte size of Excel data (workbook global substream + sheet substreams)
24 6
        $xls->dataSize = strlen($xls->data);
25
26 6
        $xls->pos = 0;
27 6
        $xls->sheets = [];
28
29
        // Parse Workbook Global Substream
30 6
        while ($xls->pos < $xls->dataSize) {
31 6
            $code = self::getUInt2d($xls->data, $xls->pos);
32
33
            match ($code) {
34 6
                self::XLS_TYPE_BOF => $xls->readBof(),
35 6
                self::XLS_TYPE_SHEET => $xls->readSheet(),
36 6
                self::XLS_TYPE_EOF => $xls->readDefault(),
37 6
                self::XLS_TYPE_CODEPAGE => $xls->readCodepage(),
38 6
                default => $xls->readDefault(),
39
            };
40
41 6
            if ($code === self::XLS_TYPE_EOF) {
42 6
                break;
43
            }
44
        }
45
46 6
        foreach ($xls->sheets as $sheet) {
47 6
            if ($sheet['sheetType'] === 0x00) {
48
                // 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
49 6
                $worksheetNames[] = $sheet['name'];
50
            }
51
        }
52
53 6
        return $worksheetNames;
54
    }
55
56
    /**
57
     * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
58
     */
59 4
    protected function listWorksheetInfo2(string $filename, Xls $xls): array
60
    {
61 4
        File::assertFile($filename);
62
63 4
        $worksheetInfo = [];
64
65
        // Read the OLE file
66 4
        $xls->loadOLE($filename);
67
68
        // total byte size of Excel data (workbook global substream + sheet substreams)
69 4
        $xls->dataSize = strlen($xls->data);
70
71
        // initialize
72 4
        $xls->pos = 0;
73 4
        $xls->sheets = [];
74
75
        // Parse Workbook Global Substream
76 4
        while ($xls->pos < $xls->dataSize) {
77 4
            $code = self::getUInt2d($xls->data, $xls->pos);
78
79
            match ($code) {
80 4
                self::XLS_TYPE_BOF => $xls->readBof(),
81 4
                self::XLS_TYPE_SHEET => $xls->readSheet(),
82 4
                self::XLS_TYPE_EOF => $xls->readDefault(),
83 4
                self::XLS_TYPE_CODEPAGE => $xls->readCodepage(),
84 4
                default => $xls->readDefault(),
85
            };
86
87 4
            if ($code === self::XLS_TYPE_EOF) {
88 4
                break;
89
            }
90
        }
91
92
        // Parse the individual sheets
93 4
        foreach ($xls->sheets as $sheet) {
94 4
            if ($sheet['sheetType'] !== 0x00) {
95
                // 0x00: Worksheet
96
                // 0x02: Chart
97
                // 0x06: Visual Basic module
98
                continue;
99
            }
100
101 4
            $tmpInfo = [];
102 4
            $tmpInfo['worksheetName'] = $sheet['name'];
103 4
            $tmpInfo['lastColumnLetter'] = 'A';
104 4
            $tmpInfo['lastColumnIndex'] = 0;
105 4
            $tmpInfo['totalRows'] = 0;
106 4
            $tmpInfo['totalColumns'] = 0;
107
108 4
            $xls->pos = $sheet['offset'];
109
110 4
            while ($xls->pos <= $xls->dataSize - 4) {
111 4
                $code = self::getUInt2d($xls->data, $xls->pos);
112
113
                switch ($code) {
114 4
                    case self::XLS_TYPE_RK:
115 4
                    case self::XLS_TYPE_LABELSST:
116 4
                    case self::XLS_TYPE_NUMBER:
117 4
                    case self::XLS_TYPE_FORMULA:
118 4
                    case self::XLS_TYPE_BOOLERR:
119 4
                    case self::XLS_TYPE_LABEL:
120 4
                        $length = self::getUInt2d($xls->data, $xls->pos + 2);
121 4
                        $recordData = $xls->readRecordData($xls->data, $xls->pos + 4, $length);
122
123
                        // move stream pointer to next record
124 4
                        $xls->pos += 4 + $length;
125
126 4
                        $rowIndex = self::getUInt2d($recordData, 0) + 1;
127 4
                        $columnIndex = self::getUInt2d($recordData, 2);
128
129 4
                        $tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex);
130 4
                        $tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex);
131
132 4
                        break;
133 4
                    case self::XLS_TYPE_BOF:
134 4
                        $xls->readBof();
135
136 4
                        break;
137 4
                    case self::XLS_TYPE_EOF:
138 4
                        $xls->readDefault();
139
140 4
                        break 2;
141
                    default:
142 4
                        $xls->readDefault();
143
144 4
                        break;
145
                }
146
            }
147
148 4
            $tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1);
149 4
            $tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
150
151 4
            $worksheetInfo[] = $tmpInfo;
152
        }
153
154 4
        return $worksheetInfo;
155
    }
156
}
157