PHP8 Compatibility. ValueError: DOMDocument::loadHTML()

Created on 19 September 2023, over 1 year ago

Problem/Motivation

When tome:static executes loadHTML for empty $content in modules/tome_static/src/StaticGenerator.php's getHtmlAssets, an error is produced:

[ERROR] Error when running "'/path/to/webroot/vendor/drush/drush/drush' '-l' 'my.domain.org'
         'tome:static-export-path' '/,/contact,/facets-block-ajax,/sitemap.xml,/rss.xml' '--return-json'
         '--process-count=5' '--uri=my.domain.org'":
           ValueError: DOMDocument::loadHTML(): Argument #1 ($source) must not be empty in
         /path/to/webroot/web/modules/contrib/tome/modules/tome_static/src/StaticGenerator.php on line 418 #0
         /path/to/webroot/web/modules/contrib/tome/modules/tome_static/src/StaticGenerator.php(418):
         DOMDocument->loadHTML()
         #1 /path/to/webroot/web/modules/contrib/tome/modules/tome_static/src/StaticGenerator.php(499):
         Drupal\tome_static\StaticGenerator->getHtmlAssets()
         #2 /path/to/webroot/web/modules/contrib/tome/modules/tome_static/src/StaticGenerator.php(184):
         Drupal\tome_static\StaticGenerator->getHtmlAssets()
         #3 /path/to/webroot/web/modules/contrib/tome/modules/tome_static/src/Commands/StaticExportPathCommand.php(70):
         Drupal\tome_static\StaticGenerator->requestPath()
         #4 /path/to/webroot/vendor/symfony/console/Command/Command.php(326):
         Drupal\tome_static\Commands\StaticExportPathCommand->execute()
         #5 /path/to/webroot/vendor/symfony/console/Application.php(1081): Symfony\Component\Console\Command\Command->run()
         #6 /path/to/webroot/vendor/symfony/console/Application.php(320):
         Symfony\Component\Console\Application->doRunCommand()
         #7 /path/to/webroot/vendor/symfony/console/Application.php(174): Symfony\Component\Console\Application->doRun()
         #8 /path/to/webroot/vendor/drush/drush/src/Runtime/Runtime.php(110): Symfony\Component\Console\Application->run()
         #9 /path/to/webroot/vendor/drush/drush/src/Runtime/Runtime.php(40): Drush\Runtime\Runtime->doRun()
         #10 /path/to/webroot/vendor/drush/drush/drush.php(139): Drush\Runtime\Runtime->run()
         #11 /path/to/webroot/vendor/drush/drush/drush(4): require('...')
         #12 {main}
          [warning] Drush command terminated abnormally.

I don't know why this happens, but it seems this was expected somehow, as can be seen from the error suppression in @$document->loadHTML($content);.
This is however not possible in PHP 8.0+ (shamelessly copying from 🐛 PHP8 Compatibility. ValueError: DOMDocument::loadHTML() Needs review ):
According to https://www.php.net/manual/en/language.operators.errorcontrol.php:

Prior to PHP 8.0.0, the error_reporting() called inside the custom error handler always returned 0 if the error was suppressed by the @ operator.
As of PHP 8.0.0, it returns the value E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR | E_PARSE.

and

Prior to PHP 8.0.0, it was possible for the @ operator to disable critical errors that will terminate script execution. For example, prepending @ to a call of a function which did not exist, by being unavailable or mistyped, would cause the script to terminate with no indication as to why.

Steps to reproduce

Hm... Unsure. I don't know when this happens. Probably with dead links? I do have some on my test setup.
A simple drush tome:static produces the above errors here.

Proposed resolution

The patch attached fixes it for me, but please review carefully. I am just a sysadmin.

Remaining tasks

Probably add a test to deliberately produce an empty $content and see whether it gets caught?

🐛 Bug report
Status

Active

Version

1.11

Component

Code

Created by

🇦🇹Austria tgoeg

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024