Skip to content

Commit 4715329

Browse files
committed
Allow installing under PHP 8
1. Allow PHPUnit 9 (needed for PHP 8) 2. Shim deprecated libxml functions Make the call to libxml_disable_entity_loader conditional. This function has been deprecated in PHP 8.0 and the new default is equivalent to calling that function with `true`. https://php.watch/versions/8.0/libxml_disable_entity_loader-deprecation Note: there were two instances of exactly the same XML-parsing anonymous function. I've traced this duplication back to 2015 when the functions were one line long and explicitness might have been better than deduplication. Over the years these functions have been growing together and I see no obvious reason why two copies should be maintained.
1 parent 55a46cc commit 4715329

2 files changed

Lines changed: 21 additions & 21 deletions

File tree

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
}
2929
],
3030
"require": {
31-
"php": "^7.2",
31+
"php": "^7.2 || ^8.0",
3232
"ext-SimpleXML": "*",
3333
"ext-fileinfo": "*",
3434
"ext-json": "*",
@@ -42,7 +42,7 @@
4242
"nyholm/psr7": "^1.3",
4343
"php-http/psr7-integration-tests": "dev-master",
4444
"phpstan/phpstan": "^0.12.52",
45-
"phpunit/phpunit": "^8.5",
45+
"phpunit/phpunit": "^8.5 || ^9.3",
4646
"squizlabs/php_codesniffer": "^3.5"
4747
},
4848
"autoload": {

src/ServerRequest.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ final public function __construct(ServerRequestInterface $serverRequest)
6161
return $result;
6262
});
6363

64-
$this->registerMediaTypeParser('application/xml', function ($input) {
65-
$backup = libxml_disable_entity_loader(true);
64+
$xmlParserCallable = function ($input) {
65+
$backup = self::disableXmlEntityLoader(true);
6666
$backup_errors = libxml_use_internal_errors(true);
6767
$result = simplexml_load_string($input);
6868

69-
libxml_disable_entity_loader($backup);
69+
self::disableXmlEntityLoader($backup);
7070
libxml_clear_errors();
7171
libxml_use_internal_errors($backup_errors);
7272

@@ -75,23 +75,10 @@ final public function __construct(ServerRequestInterface $serverRequest)
7575
}
7676

7777
return $result;
78-
});
79-
80-
$this->registerMediaTypeParser('text/xml', function ($input) {
81-
$backup = libxml_disable_entity_loader(true);
82-
$backup_errors = libxml_use_internal_errors(true);
83-
$result = simplexml_load_string($input);
84-
85-
libxml_disable_entity_loader($backup);
86-
libxml_clear_errors();
87-
libxml_use_internal_errors($backup_errors);
88-
89-
if ($result === false) {
90-
return null;
91-
}
78+
};
9279

93-
return $result;
94-
});
80+
$this->registerMediaTypeParser('application/xml', $xmlParserCallable);
81+
$this->registerMediaTypeParser('text/xml', $xmlParserCallable);
9582

9683
$this->registerMediaTypeParser('application/x-www-form-urlencoded', function ($input) {
9784
parse_str($input, $data);
@@ -781,4 +768,17 @@ public function isXhr(): bool
781768
{
782769
return $this->serverRequest->getHeaderLine('X-Requested-With') === 'XMLHttpRequest';
783770
}
771+
772+
private static function disableXmlEntityLoader(bool $disable): bool
773+
{
774+
if (\LIBXML_VERSION >= 20900) {
775+
// libxml >= 2.9.0 disables entity loading by default, so it is
776+
// safe to skip the real call (deprecated in PHP 8).
777+
return true;
778+
}
779+
780+
// @codeCoverageIgnoreStart
781+
return libxml_disable_entity_loader($disable);
782+
// @codeCoverageIgnoreEnd
783+
}
784784
}

0 commit comments

Comments
 (0)