Skip to content

Commit ade225f

Browse files
authored
Merge pull request #4669 from karrakoliko/fix-undefined-array-key-x
fix: do not trigger `Undefined array key "x"`
2 parents 4b24f82 + b8687ed commit ade225f

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
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;
1011

@@ -92,7 +93,7 @@ public function testLoadXlsBug1592(): void
9293
public function testLoadXlsBug1114(): void
9394
{
9495
$filename = 'tests/data/Reader/XLS/bug1114.xls';
95-
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($filename);
96+
$spreadsheet = IOFactory::load($filename);
9697
$sheet = $spreadsheet->getActiveSheet();
9798
self::assertSame(1148140800.0, $sheet->getCell('B2')->getValue());
9899
$spreadsheet->disconnectWorksheets();

tests/PhpSpreadsheetTests/Reader/Xml/XmlTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xml;
66

77
use PhpOffice\PhpSpreadsheet\Cell\DataType;
8+
use PhpOffice\PhpSpreadsheet\IOFactory;
89
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
910
use PhpOffice\PhpSpreadsheet\Reader\Xml;
1011
use PHPUnit\Framework\TestCase;
@@ -102,4 +103,24 @@ public function testEmptyFilename(): void
102103
$this->expectExceptionMessage('File "" does not exist');
103104
$xmlReader->load('');
104105
}
106+
107+
/**
108+
* Ensures that a PHP warning for `Undefined array key "x"` is not triggered.
109+
*
110+
* Relies on PHPUnit's conversion of PHP warnings, deprecations, and notices to exceptions.
111+
* If that warning occurs, the test should fail.
112+
*/
113+
public function testLoadXlsBug4669(): void
114+
{
115+
$filename = 'tests/data/Reader/Xml/bug4669.xml';
116+
117+
$reader = IOFactory::createReaderForFile($filename);
118+
$reader->setReadDataOnly(true);
119+
120+
$spreadsheet = $reader->load($filename);
121+
122+
$sheet = $spreadsheet->getActiveSheet();
123+
self::assertSame('Report Date', $sheet->getCell('A1')->getValue());
124+
$spreadsheet->disconnectWorksheets();
125+
}
105126
}

tests/data/Reader/Xml/bug4669.xml

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)