Skip to content

Commit f818176

Browse files
committed
Remove implicit nullability deprecated in PHP 8.4
PHP 8.4 deprecates implicitly nullable parameters, i.e. typed parameter with a `null` default value, which are not explicitly declared as nullable. See <https://wiki.php.net/rfc/deprecate-implicitly-nullable-types> Ideally, we would just change the type hint `?DOMElement` but <https://wiki.php.net/rfc/nullable_types> was only introduced in PHP 7.1, while we still support PHP 5.6. This patch is a somewhat hacky alternative to <#264> that fixes those warnings without breaking backwards compatibility or removing PHP < 7.1 support. We remove the offending formal arguments in favour of obtaining them with `func_get_arg()` and checking their type ourselves. The PHPDoc param annotations were updated to match the actual types of the now virtual arguments. The downside is that it removes PHP 7.4’s parameter contravariance checks (https://wiki.php.net/rfc/covariant-returns-and-contravariant-parameters) so future re-introduction of the formal arguments would be a BC break.
1 parent af7522b commit f818176

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

Mf2/Parser.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,10 +1380,15 @@ public function upgradeRelTagToCategory(DOMElement $el) {
13801380
/**
13811381
* Kicks off the parsing routine
13821382
* @param bool $convertClassic whether to do backcompat parsing on microformats1. Defaults to true.
1383-
* @param DOMElement $context optionally specify an element from which to parse microformats
1383+
* @param ?DOMElement $context optionally specify an element from which to parse microformats
13841384
* @return array An array containing all the microformats found in the current document
13851385
*/
1386-
public function parse($convertClassic = true, DOMElement $context = null) {
1386+
public function parse($convertClassic = true) {
1387+
$context = func_num_args() > 1 ? func_get_arg(1) : null;
1388+
if ($context !== null && !$context instanceof DOMElement) {
1389+
throw new \InvalidArgumentException(__METHOD__ . ': Argument #1 ($context) must be of type ?DOMElement, ' . gettype($context) . ' given');
1390+
}
1391+
13871392
$this->convertClassic = $convertClassic;
13881393
$mfs = $this->parse_recursive($context);
13891394

@@ -1407,11 +1412,21 @@ public function parse($convertClassic = true, DOMElement $context = null) {
14071412
/**
14081413
* Parse microformats recursively
14091414
* Keeps track of whether inside a backcompat root or not
1410-
* @param DOMElement $context: node to start with
1415+
* @param ?DOMElement $context: node to start with
14111416
* @param int $depth: recursion depth
14121417
* @return array
14131418
*/
1414-
public function parse_recursive(DOMElement $context = null, $depth = 0) {
1419+
public function parse_recursive() {
1420+
$numArgs = func_num_args();
1421+
$context = $numArgs > 0 ? func_get_arg(0) : null;
1422+
$depth = $numArgs > 1 ? func_get_arg(1) : 0;
1423+
if ($context !== null && !$context instanceof DOMElement) {
1424+
throw new \InvalidArgumentException(__METHOD__ . ': Argument #0 ($context) must be of type ?DOMElement, ' . gettype($context) . ' given');
1425+
}
1426+
if ($depth !== null && !is_int($depth)) {
1427+
throw new \InvalidArgumentException(__METHOD__ . ': Argument #1 ($depth) must be of type int, ' . gettype($depth) . ' given');
1428+
}
1429+
14151430
$mfs = array();
14161431
$mfElements = $this->getRootMF($context);
14171432

@@ -1513,10 +1528,15 @@ public function parseFromId($id, $convertClassic=true) {
15131528

15141529
/**
15151530
* Get the root microformat elements
1516-
* @param DOMElement $context
1531+
* @param ?DOMElement $context
15171532
* @return DOMNodeList
15181533
*/
1519-
public function getRootMF(DOMElement $context = null) {
1534+
public function getRootMF() {
1535+
$context = func_num_args() > 0 ? func_get_arg(0) : null;
1536+
if ($context !== null && !$context instanceof DOMElement) {
1537+
throw new \InvalidArgumentException(__METHOD__ . ': Argument #0 ($context) must be of type ?DOMElement, ' . gettype($context) . ' given');
1538+
}
1539+
15201540
// start with mf2 root class name xpath
15211541
$xpaths = array(
15221542
'(php:function("\\Mf2\\classHasMf2RootClassname", normalize-space(@class)))'

0 commit comments

Comments
 (0)