From fc02062ecedcfb1f5eba50aab5964b87c22c7983 Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Fri, 12 Jun 2020 08:46:52 +0200 Subject: [PATCH] Inline preg_replace callback functions No need to have them on the class level. PHP supports closures for a long time now. Change-Id: I124f2b3f706e5f7266a8b3236dd5de9fef792db5 --- includes/Poem.php | 50 +++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/includes/Poem.php b/includes/Poem.php index 5e77cfe..04f2056 100644 --- a/includes/Poem.php +++ b/includes/Poem.php @@ -32,7 +32,21 @@ class Poem { $tag = $parser->insertStripItem( "
" ); // replace colons with indented spans - $text = preg_replace_callback( '/^(:++)(.+)$/m', [ self::class, 'indentVerse' ], $in ); + $text = preg_replace_callback( + '/^(:++)(.+)$/m', + function ( array $matches ) { + $indention = strlen( $matches[1] ); + return Html::rawElement( + 'span', + [ + 'class' => 'mw-poem-indented', + 'style' => 'display: inline-block; margin-left: ' . $indention . 'em;' + ], + $matches[2] + ); + }, + $in + ); // replace newlines with
tags unless they are at the beginning or end // of the poem, or would directly follow exactly 4 dashes. See Parser::internalParse() for @@ -44,7 +58,13 @@ class Poem { ); // replace spaces at the beginning of a line with non-breaking spaces - $text = preg_replace_callback( '/^ +/m', [ self::class, 'replaceSpaces' ], $text ); + $text = preg_replace_callback( + '/^ +/m', + function ( array $matches ) { + return str_repeat( ' ', strlen( $matches[0] ) ); + }, + $text + ); $text = $parser->recursiveTagParse( $text, $frame ); @@ -63,30 +83,4 @@ class Poem { return Html::rawElement( 'div', $attribs, $newline . trim( $text ) . $newline ); } - - /** - * Callback for preg_replace_callback() that replaces spaces with non-breaking spaces - * @param string[] $m Matches from the regular expression - * - $m[0] consists of 1 or more spaces - * @return string - */ - protected static function replaceSpaces( array $m ) { - return str_repeat( ' ', strlen( $m[0] ) ); - } - - /** - * Callback for preg_replace_callback() that wraps content in an indented span - * @param string[] $m Matches from the regular expression - * - $m[1] consists of 1 or more colons - * - $m[2] consists of the text after the colons - * @return string - */ - protected static function indentVerse( array $m ) { - $attribs = [ - 'class' => 'mw-poem-indented', - 'style' => 'display: inline-block; margin-left: ' . strlen( $m[1] ) . 'em;' - ]; - // @todo Should this really be raw? - return Html::rawElement( 'span', $attribs, $m[2] ); - } }