Skip to content

Fix PHP 8.1 "Passing null to parameter" deprecations in regex_replace modifier - #1209

Open
h-marumoto wants to merge 1 commit into
smarty-php:masterfrom
h-marumoto:fix_regex_replace_null_deprecation
Open

h-marumoto wants to merge 1 commit into
smarty-php:masterfrom
h-marumoto:fix_regex_replace_null_deprecation

Conversation

@h-marumoto

@h-marumoto h-marumoto commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The regex_replace modifier triggers Passing null to parameter deprecation errors on PHP >= 8.1 when any of its arguments is null, e.g. when a template variable holds null or was never assigned:

{$nullableVar|regex_replace:"/[\r\t\n]/":" "}
Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

The same applies to:

  • a null search pattern (strpos() and preg_match() inside regex_replace_check(), and preg_replace()'s $pattern)
  • a null replacement (preg_replace()'s $replacement)
  • a null limit (preg_replace()'s $limit)

Fix

Follow the existing convention already used in DefaultExtension for number_format, explode, split and implode (see the "provide $string default to prevent deprecation errors in PHP >=8.1" comments): coalesce null arguments before passing them on.
Note on backwards compatibility: this is strictly a deprecation fix and does
not change any output.

  • $string ?? '' and $replace ?? '' produce exactly what PHP's implicit null-to-string coercion produced before.
  • $limit ?? 0 deliberately falls back to 0 (not the parameter default -1), because PHP's null-to-int coercion has always turned a null limit into 0 (i.e. no replacements). Falling back to -1 would silently change the output of existing templates that pass a null limit. A comment in the code documents this.

Tests

Added a test covering all three null cases. Since the test suite mutes E_DEPRECATED globally (PHPUnit_Smarty::setUp()), the test also collects deprecations via a temporary error handler and asserts that none are raised — running it without the fix fails with the three deprecations listed above, and with the fix all existing PluginModifier tests still pass unchanged.

This is the same kind of fix as #834 / #838 / #1135.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant