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

Created on 25 July 2023, over 1 year ago
Updated 14 May 2024, 6 months ago

Problem/Motivation

Deprecated function: mb_strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in search_api_stats_search_api_results_alter() (line 28 of search_api_stats.module

Steps to reproduce

Install and check the dblog

Proposed resolution

The deprecation warning you are seeing is related to the use of the mb_strtolower() function with a null value. The mb_strtolower() function is used to convert a string to lowercase using multi-byte characters, and passing a null value as its argument is deprecated.

To fix this issue, you should ensure that the $originalKeys variable is not null before calling the mb_strtolower() function. You can do this by adding a conditional check before the function call. Here's the updated code with the necessary check:

/**
 * Implements hook_search_api_results_alter().
 */
function search_api_stats_search_api_results_alter(ResultSetInterface &$results) {
  // Previous code...

  $originalKeys = $query->getOriginalKeys();

  // Ensure that $originalKeys is not null before using mb_strtolower().
  if ($originalKeys !== null) {
    $lowerOriginalKeys = mb_strtolower($originalKeys);
    $keywords = trim($lowerOriginalKeys);

    // Rest of the code...
  }

  // Rest of the code...
}

🐛 Bug report
Status

Fixed

Component

Code

Created by

🇮🇳India Manikandan Era

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

Comments & Activities

  • Issue created by @Manikandan Era
  • 🇮🇳India sidharth_soman Bangalore

    I might be mistaken, but wouldn't it be more concise if we used the null coalescing operator within mb_strtolower to avoid the passing of null as an argument?

    Perhaps something like this:

    $originalKeys = $query->getOriginalKeys();
    $lowerOriginalKeys = mb_strtolower($originalKeys ?? '');
    //Here, $lowerOriginalKeys should be an empty string if $originalKeys was null.
    $keywords = trim($lowerOriginalKeys); //still an empty string
    if (!empty($keywords)) {
    //if $keywords is an empty string, it will not hit this conditional and thus not add the fields.
    
  • First commit to issue fork.
  • @sakthi_dev opened merge request.
  • Status changed to Needs review over 1 year ago
  • 🇮🇳India sakthi_dev

    Created a MR with null coalescing operator as there is a if clause to verify $keywords. Not sure why we are getting NULL value while trying to retrieve originalKeys. MR might be a temporary solution but it would be better to have the condition for future and also root cause for getting NULL for originalKeys.

    • mandclu committed 59382608 on 8.x-1.x
      Issue #3376801 by sakthi_dev, sidharth_soman: Deprecated function:...
  • Status changed to Fixed 7 months ago
  • 🇨🇦Canada mandclu

    There's a chance this will have been resolved by the check added in 🐛 Empty search returns error Fixed but adding the empty string as a fallback still seems sensible. Merged in, and thanks for everyone's work here.

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

Production build 0.71.5 2024