PHP Error - must contain at least one element in max() - getLastUpdatedTime

Created on 13 October 2022, over 2 years ago
Updated 27 February 2023, about 2 years ago

Problem/Motivation

Using latest Release (and 8.x-2.x) if you try to load the Status Page and there is a Client (plublisher or subscriber) that does not have a last_updated set on their status metrics, then this error will show up.

Full trace:

ValueError: max(): Argument #1 ($value) must contain at least one element in max() (line 452 of modules/contrib/acquia_contenthub/modules/acquia_contenthub_publisher/src/Controller/StatusReportController.php).

max(Array) (Line: 452)
Drupal\acquia_contenthub_publisher\Controller\StatusReportController->getLastUpdatedTime(Array) (Line: 192)
Drupal\acquia_contenthub_publisher\Controller\StatusReportController->getWebhooksPageSection(0) (Line: 88)
Drupal\acquia_contenthub_publisher\Controller\StatusReportController->statusReportPage(Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 159)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 709)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Steps to reproduce

Not sure when clients have their status metrics last_updated value cleared, BUT as soon as you try to load the Status Page with an empty last_updated you will get the PHP error.

Proposed resolution

Since max() will throw an error on empty arrays (https://www.php.net/manual/en/function.max.php#refsect1-function.max-ret...) we should check if the times array is empty, before passing it to the function.

So, on protected function getLastUpdatedTime(array $metrics) we could replace the return line with something like:

[...]

return $times ? $this->getTimeAgo(max($times)) : 'Not Found';

Remaining tasks

Guessing there is no test for the Status Report controller... but it would be nice to have one :)

🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇦🇷Argentina anairamzap Buenos Aires

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024