Fix PHP 8.1 "Passing null to parameter" deprecations in regex_replace modifier - #1209
Open
h-marumoto wants to merge 1 commit into
Open
h-marumoto wants to merge 1 commit into
h-marumoto wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
regex_replacemodifier triggersPassing null to parameterdeprecation errors on PHP >= 8.1 when any of its arguments isnull, e.g. when a template variable holdsnullor was never assigned:{$nullableVar|regex_replace:"/[\r\t\n]/":" "}The same applies to:
nullsearch pattern (strpos()andpreg_match()insideregex_replace_check(), andpreg_replace()'s$pattern)nullreplacement (preg_replace()'s$replacement)nulllimit (preg_replace()'s$limit)Fix
Follow the existing convention already used in
DefaultExtensionfornumber_format,explode,splitandimplode(see the "provide $string default to prevent deprecation errors in PHP >=8.1" comments): coalescenullarguments 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 ?? 0deliberately falls back to0(not the parameter default-1), because PHP's null-to-int coercion has always turned anulllimit into0(i.e. no replacements). Falling back to-1would 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_DEPRECATEDglobally (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 existingPluginModifiertests still pass unchanged.This is the same kind of fix as #834 / #838 / #1135.