fix(data): guard pods_trim() against null input for PHP 8.1+ - #7580
fix(data): guard pods_trim() against null input for PHP 8.1+#7580faisalahammad wants to merge 2 commits into
Conversation
A PHP 8.1+ deprecation was emitted when pods_trim() reached trim()/ltrim()/rtrim() via call_user_func_array with a null value. The function had no early guard, relying on a (string) cast in the scalar branch to coerce null to '', and array/object recursion routes never reached that cast cleanly. Added a 3-line guard at the top of pods_trim() matching the exact pods_sanitize() idiom (line 27). One change covers the only entry point of the trim dispatcher, so every caller (top-level and recursive) is protected. null is now returned unchanged instead of producing a deprecation notice. Non-null inputs return identically. Fixes pods-framework#7262
PR Summary
|
|
AI disclosure: this PR was written with Claude Opus 5 assistance, reviewed and tested by me. |
Review: this looks already fixed on the base branchI verified $args = [
(string) $input,
];Every leaf — including the recursive array and object branches above it — is cast before The guard also changes return typesThe new early return alters behaviour for existing callers, in ways the added test locks in rather than catches:
The docblock still says The test isn't hollow — it does fail on base — but it fails because it asserts the new types, not because a deprecation is being triggered. SuggestionClose as already-fixed against the existing if ( null === $input ) {
return '';
}That guards the null case without changing the return type for any other input. Disclosure: this review was produced with AI assistance and verified against the branch code before posting. |
Description
A PHP 8.1+
Deprecated: trim(): Passing null to parameter #1 ($string)notice was emitted fromincludes/data.phpwheneverpods_trim()reachedtrim()/ltrim()/rtrim()viacall_user_func_arraywith a null value. The function had no early guard and relied on a(string)cast only in its scalar branch, leaving array and object recursion paths exposed.Added a 3-line early-return guard at the top of
pods_trim(), matching the existingpods_sanitize()idiom (line 27). One change covers the only entry point of the trim dispatcher, so every caller (top-level and recursive) is protected.nullis now returned unchanged instead of producing a deprecation notice. All non-null behavior is unchanged.function pods_trim( $input, $charlist = " \t\n\r\0\x0B", $lr = null ) { + if ( '' === $input || is_int( $input ) || is_float( $input ) || empty( $input ) ) { + return $input; + } + if ( is_object( $input ) ) {Related GitHub issue(s)
Fixes #7262
Testing instructions
WP_DEBUG=true.pods_trim()internally (saving a Pod, registering a Pod with a custom REST namespace, etc.).wp-content/debug.logno longer contains entries likeDeprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /wp-content/plugins/pods/includes/data.php on line [298-299].slic run wpunit --ext DotReporter tests/codeception/wpunit/functions/DataTest.phpruns the newtest_pods_trim_null()method, covering scalar null with/without charlist/direction, empty string,'0', integer0, float0.0, null leaf inside an array, and null leaf inside an object.Screenshots / screencast
Not applicable.
Changelog text for these changes
Bug: pods_trim() no longer triggers a PHP 8.1+ trim() deprecation notice when called with null values. #7262 (@faisalahammad)
PR checklist