Skip to content

Commit 6ee6211

Browse files
committed
fix: do not trigger Undefined array key "x"
1 parent 4fc82f4 commit 6ee6211

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

src/PhpSpreadsheet/Reader/Xml/Style.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function parseStyles(SimpleXMLElement $xml, array $namespaces): array
7676
break;
7777
case 'Protection':
7878
$locked = $hidden = null;
79-
$styleAttributesP = $styleData->attributes($namespaces['x']);
79+
$styleAttributesP = array_key_exists('x', $namespaces) ? $styleData->attributes($namespaces['x']) : [];
8080
if (isset($styleAttributes['Protected'])) {
8181
$locked = ((bool) (string) $styleAttributes['Protected']) ? Protection::PROTECTION_PROTECTED : Protection::PROTECTION_UNPROTECTED;
8282
}

tests/PhpSpreadsheetTests/Reader/Xls/XlsTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls;
66

77
use PhpOffice\PhpSpreadsheet\Cell\Cell;
8+
use PhpOffice\PhpSpreadsheet\IOFactory;
89
use PhpOffice\PhpSpreadsheet\Reader\Xls;
910
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
11+
use PHPUnit\Framework\Attributes\Group;
12+
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
1013

1114
class XlsTest extends AbstractFunctional
1215
{
@@ -92,9 +95,39 @@ public function testLoadXlsBug1592(): void
9295
public function testLoadXlsBug1114(): void
9396
{
9497
$filename = 'tests/data/Reader/XLS/bug1114.xls';
95-
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($filename);
98+
$spreadsheet = IOFactory::load($filename);
9699
$sheet = $spreadsheet->getActiveSheet();
97100
self::assertSame(1148140800.0, $sheet->getCell('B2')->getValue());
98101
$spreadsheet->disconnectWorksheets();
99102
}
103+
104+
#[WithoutErrorHandler]
105+
public function testLoadXlsBug4669(): void
106+
{
107+
error_reporting(E_ALL);
108+
$phpWarningMessage = null;
109+
110+
set_error_handler(function ($severity, $message) use (&$phpWarningMessage) {
111+
if ($message === 'Undefined array key "x"') {
112+
$phpWarningMessage = $message;
113+
return true;
114+
}
115+
116+
return false;
117+
});
118+
119+
$filename = 'tests/data/Reader/XLS/bug4669.xls';
120+
121+
$reader = IOFactory::createReaderForFile($filename);
122+
$reader->setReadDataOnly(true);
123+
124+
$reader->load($filename);
125+
126+
restore_error_handler();
127+
128+
$this->assertNull(
129+
$phpWarningMessage,
130+
sprintf('PHP warning "%s" occurred', $phpWarningMessage)
131+
);
132+
}
100133
}

tests/data/Reader/XLS/bug4669.xls

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
3+
<Styles>
4+
<Style ss:ID="Default" ss:Name="Normal">
5+
<Alignment ss:Vertical="Bottom"/>
6+
<Borders/>
7+
<Font ss:FontName="Verdana"/>
8+
<Interior/>
9+
<NumberFormat/>
10+
<Protection/>
11+
</Style>
12+
<Style ss:ID="s22">
13+
<NumberFormat ss:Format="General Date"/>
14+
</Style>
15+
<Style ss:ID="s66">
16+
<NumberFormat ss:Format="Fixed"/>
17+
</Style>
18+
</Styles>
19+
<Worksheet ss:Name="stats by transaction_notes">
20+
<Table>
21+
<Row>
22+
<Cell ss:StyleID="Default">
23+
<Data ss:Type="String">Report Date</Data>
24+
</Cell>
25+
<Cell ss:StyleID="Default">
26+
<Data ss:Type="String">Trx Date</Data>
27+
</Cell>
28+
<Cell ss:StyleID="Default">
29+
<Data ss:Type="String">Merchant</Data>
30+
</Cell>
31+
<Cell ss:StyleID="Default">
32+
<Data ss:Type="String">Terminal</Data>
33+
</Cell>
34+
<Cell ss:StyleID="Default">
35+
<Data ss:Type="String">Report Type</Data>
36+
</Cell>
37+
<Cell ss:StyleID="Default">
38+
<Data ss:Type="String">Transition</Data>
39+
</Cell>
40+
<Cell ss:StyleID="Default">
41+
<Data ss:Type="String">Report Message</Data>
42+
</Cell>
43+
<Cell ss:StyleID="Default">
44+
<Data ss:Type="String">Report Info</Data>
45+
</Cell>
46+
<Cell ss:StyleID="Default">
47+
<Data ss:Type="String">Status</Data>
48+
</Cell>
49+
<Cell ss:StyleID="Default">
50+
<Data ss:Type="String">Merchant Transaction ID</Data>
51+
</Cell>
52+
<Cell ss:StyleID="Default">
53+
<Data ss:Type="String">Unique ID</Data>
54+
</Cell>
55+
<Cell ss:StyleID="Default">
56+
<Data ss:Type="String">Reference Transaction ID</Data>
57+
</Cell>
58+
<Cell ss:StyleID="Default">
59+
<Data ss:Type="String">Reference Transaction Type</Data>
60+
</Cell>
61+
<Cell ss:StyleID="Default">
62+
<Data ss:Type="String">Currency</Data>
63+
</Cell>
64+
<Cell ss:StyleID="Default">
65+
<Data ss:Type="String">Amount (in minor currency unit)</Data>
66+
</Cell>
67+
<Cell ss:StyleID="Default">
68+
<Data ss:Type="String">Card Holder</Data>
69+
</Cell>
70+
<Cell ss:StyleID="Default">
71+
<Data ss:Type="String">Card Brand</Data>
72+
</Cell>
73+
<Cell ss:StyleID="Default">
74+
<Data ss:Type="String">Card Number</Data>
75+
</Cell>
76+
<Cell ss:StyleID="Default">
77+
<Data ss:Type="String">Email</Data>
78+
</Cell>
79+
<Cell ss:StyleID="Default">
80+
<Data ss:Type="String">Country</Data>
81+
</Cell>
82+
<Cell ss:StyleID="Default">
83+
<Data ss:Type="String">Remote IP</Data>
84+
</Cell>
85+
</Row>
86+
</Table>
87+
</Worksheet>
88+
</Workbook>

0 commit comments

Comments
 (0)