PHP 8.1 Deprecated function Notice when passing NULL to trim()

My Reports -> Recent Logs is inundated with Notices. Each row returned of a view I use for listing Events results in a Notice so it fills up pretty quickly.

Deprecated function: trim(): Passing null to parameter #1 ($string) of type string is deprecated in views_handler_field->render_text() (line 1195 of /views/handlers/views_handler_field.inc).

Steps to reproduce

Create a view for a content type and trim the whitespace of one or more fields. In my case, I have a field that I am using as a link. The link goes to the node ID and I am Trimming the Whitespace from the field.

Proposed resolution

I will attach a patch that casts the value as a string before it trims.

Remaining tasks

Please test and apply if it passes.

Cheers,
Andrew

Comments

awasson created an issue. See original summary.

awasson’s picture

Title: Deprecated function: trim(): Passing null to parameter #1 ($string) of type string is deprecated in views_handler_field->render_text() (line 1195 of /views/handlers/views_handler_field.inc). » Deprecated function: trim(): Passing null to parameter #1 ($string)
Issue summary: View changes
StatusFileSize
new514 bytes

Patch is attached that casts the value as a string before it trims.

awasson’s picture

Status: Active » Needs review
awasson’s picture

Version: 7.x-3.x-dev » 7.x-3.27
damienmckenna’s picture

Version: 7.x-3.27 » 7.x-3.x-dev
Assigned: awasson » Unassigned
Status: Needs review » Needs work
Parent issue: » #3311884: Plan for Views 7.x-3.28

I think it would be worth extending this to cover more situations where trim() is executed on variables that might not be strings.

damienmckenna’s picture

Issue tags: +PHP 8.1
awasson’s picture

@DamienMcKenna, how about using is_null()? is_null() only tests if the variable is null so empty variables may still show up as an empty string but that should be ok.

Original code:

    if (!empty($this->options['alter']['trim_whitespace'])) {
      $value = trim($value);
    }

Test for NULL:

    if (!is_null($value) && !empty($this->options['alter']['trim_whitespace'])) {
      $value = trim($value);
    }

If this is acceptable, I'll create a patch.

Cheers,
Andrew

awasson’s picture

Status: Needs work » Needs review
StatusFileSize
new544 bytes

Patch attached.

avpaderno’s picture

Title: Deprecated function: trim(): Passing null to parameter #1 ($string) » trim(): Passing null to parameter #1 ($string) of type string is deprecated
damienmckenna’s picture

Status: Needs review » Fixed

Committed. Thanks.

  • DamienMcKenna committed c3d49a2 on 7.x-3.x
    Issue #3313970 by awasson, DamienMcKenna:  trim(): Passing null to...

Status: Fixed » Closed (fixed)

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