Deprecated function: explode(): Passing null to parameter #2 ($string) of type string is deprecated

Created on 3 July 2024, 9 months ago

Problem/Motivation

I just noticed a few of these errors in my Watchdog log:

Deprecated function: explode(): Passing null to parameter #2 ($string) of type string is deprecated in Drupal\visitors\Service\CookieService->getId() (line 40 of /var/www/mysite/web/modules/contrib/visitors/src/Service/CookieService.php)

I'm running Drupal 10.3.0 with PHP 8.1.2. I'm not sure what triggered it, but I do see that the IP address associated with the error (66.249.64.164) is for a Google crawler. Here's the call stack:

#0 /var/www/mysite/web/core/includes/bootstrap.inc(166): _drupal_error_handler_real()
#1 [internal function]: _drupal_error_handler()
#2 /var/www/mysite/web/modules/contrib/visitors/src/Service/CookieService.php(40): explode()
#3 /var/www/mysite/web/modules/contrib/visitors/src/Service/TrackerService.php(293): Drupal\visitors\Service\CookieService->getId()
#4 /var/www/mysite/web/modules/contrib/visitors/src/Service/TrackerService.php(157): Drupal\visitors\Service\TrackerService->getVisitorId()
#5 /var/www/mysite/web/modules/contrib/visitors/src/Controller/Visitors.php(60): Drupal\visitors\Service\TrackerService->log()
#6 [internal function]: Drupal\visitors\Controller\Visitors->track()
#7 /var/www/mysite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array()
#8 /var/www/mysite/web/core/lib/Drupal/Core/Render/Renderer.php(638): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#9 /var/www/mysite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext()
#10 /var/www/mysite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()
#11 /var/www/mysite/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#12 /var/www/mysite/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
#13 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle()
#14 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
#15 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
#16 /var/www/mysite/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle()
#17 /var/www/mysite/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(191): Drupal\big_pipe\StackMiddleware\ContentLength->handle()
#18 /var/www/mysite/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(128): Drupal\page_cache\StackMiddleware\PageCache->fetch()
#19 /var/www/mysite/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(82): Drupal\page_cache\StackMiddleware\PageCache->lookup()
#20 /var/www/mysite/web/modules/contrib/cleantalk/src/EventSubscriber/BootSubscriber.php(193): Drupal\page_cache\StackMiddleware\PageCache->handle()
#21 /var/www/mysite/web/core/modules/ban/src/BanMiddleware.php(50): Drupal\cleantalk\EventSubscriber\BootSubscriber->handle()
#22 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\ban\BanMiddleware->handle()
#23 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
#24 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
#25 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle()
#26 /var/www/mysite/web/core/lib/Drupal/Core/DrupalKernel.php(741): Drupal\Core\StackMiddleware\StackedHttpKernel->handle()
#27 /var/www/mysite/web/index.php(19): Drupal\Core\DrupalKernel->handle()
#28 {main}

Proposed resolution

This is the code:

[$visitor_id] = explode('.', $_pk_id);

The error can be avoided if the call to explode is skipped if the value of $_pk_id is NULL, but I don't know what the value of [$visitor_id] should be set to in that case. Would it make sense to set it to an empty array ([]), like this?

[$visitor_id] = is_null($_pk_id) ? [] : explode('.', $_pk_id);

🐛 Bug report
Status

Active

Version

2.19

Component

Code

Created by

🇺🇸United States sah62 US

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024