Problem/Motivation
When manually testing a website on PHP 8 beta 3, I saw the log filled up with warnings from CKEditorPluginManager::getEnabledButtons from the array_reduce call. It can be seen in this snippet: https://3v4l.org/CUCvi . In case the 3v4l is no longer available, here is the snippet:
$arr = [1,2,3];
$reduced = array_reduce($arr, function (&$result, $items) { }, []);
It only reports warnings from PHP 8.0.0 alpha 2 onwards which means this worked fine on PHP 7.4 and even early versions of PHP 8.
The warning (in case of Drupal) is:
Warning: Drupal\ckeditor\CKEditorPluginManager::Drupal\ckeditor\{closure}(): Argument #1 ($result) must be passed by reference, value given in Drupal\ckeditor\CKEditorPluginManager::getEnabledButtons() (line 127 of /var/www/html/web/core/modules/ckeditor/src/CKEditorPluginManager.php)
#0 /var/www/html/web/core/includes/bootstrap.inc(308): _drupal_error_handler_real(2, 'Drupal\\ckeditor...', '/var/www/html/w...', 127, NULL)
#1 [internal function]: _drupal_error_handler(2, 'Drupal\\ckeditor...', '/var/www/html/w...', 127)
...
The problem is that in the callback for array_reduce, the first parameter is passed by reference. At the same time, the initial value passed to array_reduce is a literal [].
Steps to reproduce
Setup Drupal core 9.1.x on PHP 8 beta 3 and apply patches from #3156595: Make Drupal 9 installable on PHP8 and other issues to get Drupal installable and working. Go to /node/add/article. The node add page should load properly. Go to dblog to see it filled with warnings.
Proposed resolution
From reading the code, I'm not sure why the parameter in the callback should be a reference. The function body returns the modified array which should be passed back in the next iteration. We could just let it be passed as value.
Remaining tasks
Patch
User interface changes
None
API changes
None
Data model changes
None
Release notes snippet
N/A
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | 3170648-2.patch | 804 bytes | hussainweb |
Comments
Comment #2
hussainwebEven though the Issue Summary is so long, the patch is just a single character change. :)
Comment #3
hussainwebFixing a small mistake in IS where I mention the problem is from PHP 8 beta 2. It's actually from PHP 8 alpha 2.
Comment #4
andypostI guess it caused by change in php.ini to default to E_ALL cos I see no relation to in https://wiki.php.net/rfc/engine_warnings
Comment #5
alexpottThis looks good to me. I'd rtbc it but then I can not commit.
Comment #6
andypostI missed 3v4l link in summary, it explains
Pretty straightforward patch, rtbc
Comment #7
alexpottCommitted and pushed 7a2d7e0494 to 9.1.x and 9bb004cd41 to 9.0.x and fd821085bc to 8.9.x. Thanks!
Backported this to 8.9.x as it looks risk free and will make PHP 8 compat in Drupal 8 possible.