From 628ac318767fcb4c5fef47e8506ecff9fb41c9a2 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 12 Sep 2024 05:12:37 +0200 Subject: [PATCH] Fix PHP 8.4 compatibility / drop support for PHP < 7.1 PHP 8.4 deprecates implicitly nullable parameters, i.e. typed parameter with a `null` default value, which are not explicitly declared as nullable. Ref: https://wiki.php.net/rfc/deprecate-implicitly-nullable-types There are multiple ways to fix this, though most involve breaking changes: 1. Remove the type declaration and do in-method type validation. As this is not a `final` class, nor are these `final` or `private` methods, this would be a breaking change for any class extending the `Parser` class as parameter types are contra-variant, so this would cause a signature mismatch. 2. Remove the `null` default value. This, again, would be a breaking change and an even bigger one as it turns an optional parameter into a required one, so this wouldn't just have an impact on overloaded methods, but on all _calls_ to the methods too. 3. Declare the parameters as nullable. This would not cause a signature mismatch. This is the change with the least impact, although it does require PHP 7.1. If this is released as a next _minor_ though, the impact will be minimal as Composer can handle the version negotiations and will just install an older version for PHP 5.6/7.0. Also note that based on the Packagist stats, this version negotiation would rarely be needed as the users of this package hardly use PHP 5.6/7.0 anymore: https://packagist.org/packages/mf2/mf2/php-stats With this in mind, I'm proposing dropping support for PHP < 7.1 and fixing the PHP 8.4 deprecations by making the relevant parameters explicitly nullable. Includes updating CI and the PHPCS config. --- .github/workflows/main.yml | 2 +- Mf2/Parser.php | 6 +++--- composer.json | 2 +- phpcs.xml | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index eca517e..1f8be23 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -43,7 +43,7 @@ jobs: strategy: matrix: - php: ['5.6', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4'] + php: ['7.1', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4'] runs-on: ubuntu-latest diff --git a/Mf2/Parser.php b/Mf2/Parser.php index c7cc02d..5670b61 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -1383,7 +1383,7 @@ public function upgradeRelTagToCategory(DOMElement $el) { * @param DOMElement $context optionally specify an element from which to parse microformats * @return array An array containing all the microformats found in the current document */ - public function parse($convertClassic = true, DOMElement $context = null) { + public function parse($convertClassic = true, ?DOMElement $context = null) { $this->convertClassic = $convertClassic; $mfs = $this->parse_recursive($context); @@ -1411,7 +1411,7 @@ public function parse($convertClassic = true, DOMElement $context = null) { * @param int $depth: recursion depth * @return array */ - public function parse_recursive(DOMElement $context = null, $depth = 0) { + public function parse_recursive(?DOMElement $context = null, $depth = 0) { $mfs = array(); $mfElements = $this->getRootMF($context); @@ -1516,7 +1516,7 @@ public function parseFromId($id, $convertClassic=true) { * @param DOMElement $context * @return DOMNodeList */ - public function getRootMF(DOMElement $context = null) { + public function getRootMF(?DOMElement $context = null) { // start with mf2 root class name xpath $xpaths = array( '(php:function("\\Mf2\\classHasMf2RootClassname", normalize-space(@class)))' diff --git a/composer.json b/composer.json index e8add59..2ad1df4 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ ], "bin": ["bin/fetch-mf2", "bin/parse-mf2"], "require": { - "php": ">=5.6.0" + "php": ">=7.1.0" }, "config": { "platform": { diff --git a/phpcs.xml b/phpcs.xml index 8e94cdd..7597fef 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -3,5 +3,5 @@ PHP-MF2 Standards ./Mf2/Parser.php - +