aboutsummaryrefslogtreecommitdiffstats
path: root/includes/libs/StringUtils.php
diff options
context:
space:
mode:
authorKevin Israel <pleasestand@live.com>2018-07-10 11:14:29 -0400
committerKunal Mehta <legoktm@member.fsf.org>2018-07-12 10:25:59 -0700
commit93fc424fc151865eb16ec3d29fd3224724ee2f21 (patch)
tree0b61b4592e9754af8924f6d44da26daebe1f2660 /includes/libs/StringUtils.php
parenta72fa4c99dd48dad8c05ca74cd007405c94a6d7a (diff)
downloadmediawikicore-93fc424fc151865eb16ec3d29fd3224724ee2f21.tar.gz
mediawikicore-93fc424fc151865eb16ec3d29fd3224724ee2f21.zip
StringUtils: Deprecate Replacer classes
The Replacer classes were added in 1.9, when MediaWiki supported PHP 5.0 and 5.1. They were designed to be used with preg_replace_callback() and StringUtils::delimiterReplaceCallback(). Now that Closures exist in PHP 5.3 and newer, there is no need to define a class for this purpose. All existing Replacer subclasses are simple enough that their few uses can easily be replaced with Closures, without making the code harder to understand. In fact, the code probably becomes easier to understand, as what each match is replaced with becomes more obvious -- no need to refer to a separate class. MediaWiki code search finds no uses in extensions. Thus, these classes are hard deprecated immediately. Change-Id: I441c21689909fb06a1ea07a305259eeb82cb2345
Diffstat (limited to 'includes/libs/StringUtils.php')
-rw-r--r--includes/libs/StringUtils.php29
1 files changed, 21 insertions, 8 deletions
diff --git a/includes/libs/StringUtils.php b/includes/libs/StringUtils.php
index d91ac85adb1a..51d108168af4 100644
--- a/includes/libs/StringUtils.php
+++ b/includes/libs/StringUtils.php
@@ -243,10 +243,13 @@ class StringUtils {
* @return string The string with the matches replaced
*/
static function delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags = '' ) {
- $replacer = new RegexlikeReplacer( $replace );
-
- return self::delimiterReplaceCallback( $startDelim, $endDelim,
- $replacer->cb(), $subject, $flags );
+ return self::delimiterReplaceCallback(
+ $startDelim, $endDelim,
+ function ( array $matches ) use ( $replace ) {
+ return strtr( $replace, [ '$0' => $matches[0], '$1' => $matches[1] ] );
+ },
+ $subject, $flags
+ );
}
/**
@@ -263,8 +266,13 @@ class StringUtils {
$text = str_replace( $placeholder, '', $text );
// Replace instances of the separator inside HTML-like tags with the placeholder
- $replacer = new DoubleReplacer( $separator, $placeholder );
- $cleaned = self::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text );
+ $cleaned = self::delimiterReplaceCallback(
+ '<', '>',
+ function ( array $matches ) use ( $separator, $placeholder ) {
+ return str_replace( $separator, $placeholder, $matches[0] );
+ },
+ $text
+ );
// Explode, then put the replaced separators back in
$items = explode( $separator, $cleaned );
@@ -290,8 +298,13 @@ class StringUtils {
$text = str_replace( $placeholder, '', $text );
// Replace instances of the separator inside HTML-like tags with the placeholder
- $replacer = new DoubleReplacer( $search, $placeholder );
- $cleaned = self::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text );
+ $cleaned = self::delimiterReplaceCallback(
+ '<', '>',
+ function ( array $matches ) use ( $search, $placeholder ) {
+ return str_replace( $search, $placeholder, $matches[0] );
+ },
+ $text
+ );
// Explode, then put the replaced separators back in
$cleaned = str_replace( $search, $replace, $cleaned );