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

ListFunctions::listWorksheetInfo2()   C

Complexity

Conditions 14
Paths 15

Size

Total Lines 96
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 96
rs 6.2666
cc 14
nc 15
nop 2

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