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

Created on 14 March 2024, 8 months ago
Updated 10 June 2024, 5 months ago

Problem/Motivation

Deprecated function: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\views_json_source\Plugin\views\query\ViewsJsonQuery->Drupal\views_json_source\Plugin\views\query\{closure}() (line 381 of modules/contrib/views_json_source/src/Plugin/views/query/ViewsJsonQuery.php).

Our code is already patched so it may not be on that exact line in 2.0.1. Here is the offending block of code:

  /**
   * Define ops for using in filter.
   */
  public function ops($op, $l, $r) {
    $table = [
      '=' => function ($l, $r) {
        return $l == $r;
      },
      '!=' => function ($l, $r) {
        return $l != $r;
      },
      'contains' => function ($l, $r) {
        return stripos((!empty($l) ? $l : ''), $r) !== FALSE;
      },
      'starts' => function ($l, $r) {
        return strpos((!empty($l) ? $l : ''), $r) === 0;
      },
      'not_starts' => function ($l, $r) {
        return strpos((!empty($l) ? $l : ''), $r) !== 0;
      },
      'ends' => function ($l, $r) {
        $len = strlen($r);
        return $len > 0 ? substr($l, -$len) === $r : TRUE;
      },
      'not_ends' => function ($l, $r) {
        $len = strlen($r);
        return $len > 0 ? substr($l, -$len) !== $r : TRUE;
      },
      'not' => function ($l, $r) {
        return stripos((!empty($l) ? $l : ''), $r) === FALSE;
      },
      'shorterthan' => function ($l, $r) {
        return strlen($l) < $r;
      },
      'longerthan' => function ($l, $r) {
        return strlen($l) > $r;
      },
      'regular_expression' => function ($l, $r) {
        return preg_match($r, $l) === 1;
      },
    ];

    return call_user_func_array($table[$op], [$l, $r]);
  }

Specifically:


      'longerthan' => function ($l, $r) {
        return strlen($l) > $r;
      },

But I suspect they're all throwing warnings.

$l and quite possibly $r need to be checked for null before being passed in. It's hard to tell without fully investigating due to the way it's written.

Steps to reproduce

Have a json source that has an endpoint which might not be present on some records... not just null within the endpoint, but the endpoint actually doesn't exist.

Proposed resolution

Check for null on line 381 of modules/contrib/views_json_source/src/Plugin/views/query/ViewsJsonQuery.php

🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇺🇸United States loopy1492

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