From ea52f36afce11f4b069ff4933d0564262fa1a980 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Wed, 28 Feb 2018 11:22:43 +1100 Subject: [PATCH] In StripState use closures instead of temporary member variables The former convention was an awkward workaround for the lack of closures. Change-Id: I8722e168fb9b5e76cf6a937139be728bb3fc3e92 --- includes/parser/StripState.php | 85 ++++++++++++++-------------------- 1 file changed, 35 insertions(+), 50 deletions(-) diff --git a/includes/parser/StripState.php b/includes/parser/StripState.php index 298aad3313..5d6385e847 100644 --- a/includes/parser/StripState.php +++ b/includes/parser/StripState.php @@ -30,7 +30,6 @@ class StripState { protected $data; protected $regex; - protected $tempType, $tempMergePrefix; protected $circularRefGuard; protected $recursionLevel = 0; @@ -122,44 +121,37 @@ class StripState { return $text; } - $oldType = $this->tempType; - $this->tempType = $type; - $text = preg_replace_callback( $this->regex, [ $this, 'unstripCallback' ], $text ); - $this->tempType = $oldType; - return $text; - } - - /** - * @param array $m - * @return array - */ - protected function unstripCallback( $m ) { - $marker = $m[1]; - if ( isset( $this->data[$this->tempType][$marker] ) ) { - if ( isset( $this->circularRefGuard[$marker] ) ) { - return '' - . wfMessage( 'parser-unstrip-loop-warning' )->inContentLanguage()->text() - . ''; - } - if ( $this->recursionLevel >= self::UNSTRIP_RECURSION_LIMIT ) { - return '' . - wfMessage( 'parser-unstrip-recursion-limit' ) - ->numParams( self::UNSTRIP_RECURSION_LIMIT )->inContentLanguage()->text() . - ''; + $callback = function ( $m ) use ( $type ) { + $marker = $m[1]; + if ( isset( $this->data[$type][$marker] ) ) { + if ( isset( $this->circularRefGuard[$marker] ) ) { + return '' + . wfMessage( 'parser-unstrip-loop-warning' )->inContentLanguage()->text() + . ''; + } + if ( $this->recursionLevel >= self::UNSTRIP_RECURSION_LIMIT ) { + return '' . + wfMessage( 'parser-unstrip-recursion-limit' ) + ->numParams( self::UNSTRIP_RECURSION_LIMIT )->inContentLanguage()->text() . + ''; + } + $this->circularRefGuard[$marker] = true; + $this->recursionLevel++; + $value = $this->data[$type][$marker]; + if ( $value instanceof Closure ) { + $value = $value(); + } + $ret = $this->unstripType( $type, $value ); + $this->recursionLevel--; + unset( $this->circularRefGuard[$marker] ); + return $ret; + } else { + return $m[0]; } - $this->circularRefGuard[$marker] = true; - $this->recursionLevel++; - $value = $this->data[$this->tempType][$marker]; - if ( $value instanceof Closure ) { - $value = $value(); - } - $ret = $this->unstripType( $this->tempType, $value ); - $this->recursionLevel--; - unset( $this->circularRefGuard[$marker] ); - return $ret; - } else { - return $m[0]; - } + }; + + $text = preg_replace_callback( $this->regex, $callback, $text ); + return $text; } /** @@ -215,21 +207,14 @@ class StripState { } } - $this->tempMergePrefix = $mergePrefix; - $texts = preg_replace_callback( $otherState->regex, [ $this, 'mergeCallback' ], $texts ); - $this->tempMergePrefix = null; + $callback = function ( $m ) use ( $mergePrefix ) { + $key = $m[1]; + return Parser::MARKER_PREFIX . $mergePrefix . '-' . $key . Parser::MARKER_SUFFIX; + }; + $texts = preg_replace_callback( $otherState->regex, $callback, $texts ); return $texts; } - /** - * @param array $m - * @return string - */ - protected function mergeCallback( $m ) { - $key = $m[1]; - return Parser::MARKER_PREFIX . $this->tempMergePrefix . '-' . $key . Parser::MARKER_SUFFIX; - } - /** * Remove any strip markers found in the given text. * -- 2.20.1