Problem/Motivation

Every so often, I see this error in my watchdog log:

Warning: foreach() argument must be of type array|object, null given in Drupal\visitors\Controller\Visitors->doCounter() (line 311 of /var/www/mysite/web/modules/contrib/visitors/src/Controller/Visitors.php)
#0 /var/www/mysite/web/core/includes/bootstrap.inc(104): _drupal_error_handler_real()
#1 /var/www/mysite/web/modules/contrib/visitors/src/Controller/Visitors.php(311): _drupal_error_handler()
#2 /var/www/mysite/web/modules/contrib/visitors/src/Controller/Visitors.php(185): Drupal\visitors\Controller\Visitors->doCounter()
#3 [internal function]: Drupal\visitors\Controller\Visitors->track()
#4 /var/www/mysite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array()
#5 /var/www/mysite/web/core/lib/Drupal/Core/Render/Renderer.php(627): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#6 /var/www/mysite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(121): Drupal\Core\Render\Renderer->executeInRenderContext()
#7 /var/www/mysite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()
#8 /var/www/mysite/vendor/symfony/http-kernel/HttpKernel.php(183): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#9 /var/www/mysite/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
#10 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle()
#11 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
#12 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
#13 /var/www/mysite/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle()
#14 /var/www/mysite/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(116): Drupal\big_pipe\StackMiddleware\ContentLength->handle()
#15 /var/www/mysite/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(90): Drupal\page_cache\StackMiddleware\PageCache->pass()
#16 /var/www/mysite/web/modules/contrib/cleantalk/src/EventSubscriber/BootSubscriber.php(206): Drupal\page_cache\StackMiddleware\PageCache->handle()
#17 /var/www/mysite/web/core/modules/ban/src/BanMiddleware.php(50): Drupal\cleantalk\EventSubscriber\BootSubscriber->handle()
#18 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\ban\BanMiddleware->handle()
#19 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
#20 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(53): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
#21 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle()
#22 /var/www/mysite/web/core/lib/Drupal/Core/DrupalKernel.php(715): Drupal\Core\StackMiddleware\StackedHttpKernel->handle()
#23 /var/www/mysite/web/index.php(19): Drupal\Core\DrupalKernel->handle()
#24 {main}

Steps to reproduce

I don't know how to reproduce this.

Proposed resolution

The error is caused because the PHP json_decode function can return null, and the code isn't checking for that condition:

    $viewed = NULL;
    if (!is_null($cvar)) {
      $custom = json_decode($cvar);
      foreach ($custom as $c) {
        if ($c[0] == 'viewed') {
          $viewed = $c[1];
        }
      }
    }

The fix here is to check the value of $custom that's returned from json_decode() and only enter the foreach loop if it isn't null.

Issue fork visitors-3548920

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

sah62 created an issue. See original summary.

bluegeek9’s picture

Thank you Sah62 for reporting this bug. I will get it fixed, make a new release over the weekend.

  • bluegeek9 committed 4f55b265 on 8.x-2.x
    [#3548920] feat: Warning: foreach() argument must be of type array|...
bluegeek9’s picture

Status: Active » Fixed

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

bluegeek9’s picture

Status: Fixed » Active

bluegeek9’s picture

Status: Active » Fixed

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

  • bluegeek9 committed 59de6d6e on 3.0.x
    [#3548920] feat: Warning: foreach() argument must be of type array|...
bluegeek9’s picture

//www.flaticon.com/free-icons/thank-you Thank you for your contribution! Your continued support makes this project sustainable.
There are multiple ways to show appreciation, including:
  • Triage issues and adding more context to existing issues.
  • Writing documentation or patches for this project.
  • Flagging Visitors as a favorite on the project page to help others discover it and show your support.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.