Skip to content

Commit d87d8c8

Browse files
brainfoolongGianluca Giovinazzo
authored and
Gianluca Giovinazzo
committed
fixed php8 deprecation warning for libxml_disable_entity_loader() (PHPOffice#1625)
* fixed php8 deprecation warning for libxml_disable_entity_loader()
1 parent 0b2c2e1 commit d87d8c8

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
560560
- Ignore inlineStr type if formula element exists - @ncrypthic [#570](https://github.com/PHPOffice/PHPExcel/issues/570)
561561
- Excel 2007 Reader freezes because of conditional formatting - @rentalhost [#575](https://github.com/PHPOffice/PHPExcel/issues/575)
562562
- Readers will now parse files containing worksheet titles over 31 characters [#176](https://github.com/PHPOffice/PhpSpreadsheet/pull/176)
563+
- Fixed PHP8 deprecation warning for libxml_disable_entity_loader() [#1625](https://github.com/phpoffice/phpspreadsheet/pull/1625)
563564

564565
### General
565566

@@ -581,4 +582,4 @@ For a comprehensive list of all class changes, and a semi-automated migration pa
581582

582583
## Previous versions of PHPExcel
583584

584-
The changelog for the project when it was called PHPExcel is [still available](./CHANGELOG.PHPExcel.md).
585+
The changelog for the project when it was called PHPExcel is [still available](./CHANGELOG.PHPExcel.md).

src/PhpSpreadsheet/Reader/Security/XmlScanner.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function threadSafeLibxmlDisableEntityLoaderAvailability()
6363

6464
private function disableEntityLoaderCheck(): void
6565
{
66-
if (Settings::getLibXmlDisableEntityLoader()) {
66+
if (Settings::getLibXmlDisableEntityLoader() && \PHP_VERSION_ID < 80000) {
6767
$libxmlDisableEntityLoaderValue = libxml_disable_entity_loader(true);
6868

6969
if (self::$libxmlDisableEntityLoaderValue === null) {
@@ -74,7 +74,7 @@ private function disableEntityLoaderCheck(): void
7474

7575
public static function shutdown(): void
7676
{
77-
if (self::$libxmlDisableEntityLoaderValue !== null) {
77+
if (self::$libxmlDisableEntityLoaderValue !== null && \PHP_VERSION_ID < 80000) {
7878
libxml_disable_entity_loader(self::$libxmlDisableEntityLoaderValue);
7979
self::$libxmlDisableEntityLoaderValue = null;
8080
}

tests/PhpSpreadsheetTests/Reader/Security/XmlScannerTest.php

+20-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ class XmlScannerTest extends TestCase
1212
{
1313
protected function setUp(): void
1414
{
15-
libxml_disable_entity_loader(false);
15+
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
16+
if (\PHP_VERSION_ID < 80000) {
17+
libxml_disable_entity_loader(false);
18+
}
1619
}
1720

1821
/**
@@ -24,13 +27,19 @@ protected function setUp(): void
2427
*/
2528
public function testValidXML($filename, $expectedResult, $libxmlDisableEntityLoader): void
2629
{
27-
$oldDisableEntityLoaderState = libxml_disable_entity_loader($libxmlDisableEntityLoader);
30+
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
31+
if (\PHP_VERSION_ID < 80000) {
32+
$oldDisableEntityLoaderState = libxml_disable_entity_loader($libxmlDisableEntityLoader);
33+
}
2834

2935
$reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml());
3036
$result = $reader->scanFile($filename);
3137
self::assertEquals($expectedResult, $result);
3238

33-
libxml_disable_entity_loader($oldDisableEntityLoaderState);
39+
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
40+
if (\PHP_VERSION_ID < 80000) {
41+
libxml_disable_entity_loader($oldDisableEntityLoaderState);
42+
}
3443
}
3544

3645
public function providerValidXML()
@@ -56,13 +65,19 @@ public function testInvalidXML($filename, $libxmlDisableEntityLoader): void
5665
{
5766
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
5867

59-
libxml_disable_entity_loader($libxmlDisableEntityLoader);
68+
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
69+
if (\PHP_VERSION_ID < 80000) {
70+
libxml_disable_entity_loader($libxmlDisableEntityLoader);
71+
}
6072

6173
$reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml());
6274
$expectedResult = 'FAILURE: Should throw an Exception rather than return a value';
6375
$result = $reader->scanFile($filename);
6476
self::assertEquals($expectedResult, $result);
65-
self::assertEquals($libxmlDisableEntityLoader, libxml_disable_entity_loader());
77+
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
78+
if (\PHP_VERSION_ID < 80000) {
79+
self::assertEquals($libxmlDisableEntityLoader, libxml_disable_entity_loader());
80+
}
6681
}
6782

6883
public function providerInvalidXML()

tests/PhpSpreadsheetTests/SettingsTest.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,39 @@ class SettingsTest extends TestCase
1414

1515
protected function setUp(): void
1616
{
17-
$this->prevValue = libxml_disable_entity_loader();
18-
libxml_disable_entity_loader(false); // Enable entity loader
17+
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
18+
if (\PHP_VERSION_ID < 80000) {
19+
$this->prevValue = libxml_disable_entity_loader();
20+
libxml_disable_entity_loader(false); // Enable entity loader
21+
}
1922
}
2023

2124
protected function tearDown(): void
2225
{
23-
libxml_disable_entity_loader($this->prevValue);
26+
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
27+
if (\PHP_VERSION_ID < 80000) {
28+
libxml_disable_entity_loader($this->prevValue);
29+
}
2430
}
2531

2632
public function testGetXMLSettings(): void
2733
{
2834
$result = Settings::getLibXmlLoaderOptions();
2935
self::assertTrue((bool) ((LIBXML_DTDLOAD | LIBXML_DTDATTR) & $result));
30-
self::assertFalse(libxml_disable_entity_loader());
36+
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
37+
if (\PHP_VERSION_ID < 80000) {
38+
self::assertFalse(libxml_disable_entity_loader());
39+
}
3140
}
3241

3342
public function testSetXMLSettings(): void
3443
{
3544
Settings::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_DTDVALID);
3645
$result = Settings::getLibXmlLoaderOptions();
3746
self::assertTrue((bool) ((LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_DTDVALID) & $result));
38-
self::assertFalse(libxml_disable_entity_loader());
47+
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
48+
if (\PHP_VERSION_ID < 80000) {
49+
self::assertFalse(libxml_disable_entity_loader());
50+
}
3951
}
4052
}

0 commit comments

Comments
 (0)