From 447f40d2e9f10d9f2b3ab64b63e19c2af82354e8 Mon Sep 17 00:00:00 2001 From: Ori Livneh Date: Sat, 31 Oct 2015 16:10:54 -0700 Subject: [PATCH] Move brace matching rules to Preprocessor class Instead of declaring the array of rules within both Preprocessor_DOM:: and Preprocessor_Hash::preprocessToXml(), declare it as a protected property of the parent Preprocessor class. Change-Id: I6193de66566c164fe85cdd6a88c04fa9c565f1a9 --- includes/parser/Preprocessor.php | 21 +++++++++++++++++++++ includes/parser/Preprocessor_DOM.php | 26 ++++---------------------- includes/parser/Preprocessor_Hash.php | 26 ++++---------------------- 3 files changed, 29 insertions(+), 44 deletions(-) diff --git a/includes/parser/Preprocessor.php b/includes/parser/Preprocessor.php index b1e49b2a6f..df5d4995b4 100644 --- a/includes/parser/Preprocessor.php +++ b/includes/parser/Preprocessor.php @@ -30,6 +30,27 @@ abstract class Preprocessor { const CACHE_VERSION = 1; + /** + * @var array Brace matching rules. + */ + protected $rules = array( + '{' => array( + 'end' => '}', + 'names' => array( + 2 => 'template', + 3 => 'tplarg', + ), + 'min' => 2, + 'max' => 3, + ), + '[' => array( + 'end' => ']', + 'names' => array( 2 => null ), + 'min' => 2, + 'max' => 2, + ) + ); + /** * Store a document tree in the cache. * diff --git a/includes/parser/Preprocessor_DOM.php b/includes/parser/Preprocessor_DOM.php index b71b9d242f..4ca3a878bd 100644 --- a/includes/parser/Preprocessor_DOM.php +++ b/includes/parser/Preprocessor_DOM.php @@ -193,24 +193,6 @@ class Preprocessor_DOM extends Preprocessor { * @return string */ public function preprocessToXml( $text, $flags = 0 ) { - $rules = array( - '{' => array( - 'end' => '}', - 'names' => array( - 2 => 'template', - 3 => 'tplarg', - ), - 'min' => 2, - 'max' => 3, - ), - '[' => array( - 'end' => ']', - 'names' => array( 2 => null ), - 'min' => 2, - 'max' => 2, - ) - ); - $forInclusion = $flags & Parser::PTD_FOR_INCLUSION; $xmlishElements = $this->parser->getStripList(); @@ -328,9 +310,9 @@ class Preprocessor_DOM extends Preprocessor { } } elseif ( $curChar == $currentClosing ) { $found = 'close'; - } elseif ( isset( $rules[$curChar] ) ) { + } elseif ( isset( $this->rules[$curChar] ) ) { $found = 'open'; - $rule = $rules[$curChar]; + $rule = $this->rules[$curChar]; } else { # Some versions of PHP have a strcspn which stops on null characters # Ignore and continue @@ -627,7 +609,7 @@ class Preprocessor_DOM extends Preprocessor { # check for maximum matching characters (if there are 5 closing # characters, we will probably need only 3 - depending on the rules) - $rule = $rules[$piece->open]; + $rule = $this->rules[$piece->open]; if ( $count > $rule['max'] ) { # The specified maximum exists in the callback array, unless the caller # has made an error @@ -696,7 +678,7 @@ class Preprocessor_DOM extends Preprocessor { $piece->parts = array( new PPDPart ); $piece->count -= $matchingCount; # do we still qualify for any callback with remaining count? - $min = $rules[$piece->open]['min']; + $min = $this->rules[$piece->open]['min']; if ( $piece->count >= $min ) { $stack->push( $piece ); $accum =& $stack->getAccum(); diff --git a/includes/parser/Preprocessor_Hash.php b/includes/parser/Preprocessor_Hash.php index 54824da69a..4f124147e2 100644 --- a/includes/parser/Preprocessor_Hash.php +++ b/includes/parser/Preprocessor_Hash.php @@ -118,24 +118,6 @@ class Preprocessor_Hash extends Preprocessor { return unserialize( $tree ); } - $rules = array( - '{' => array( - 'end' => '}', - 'names' => array( - 2 => 'template', - 3 => 'tplarg', - ), - 'min' => 2, - 'max' => 3, - ), - '[' => array( - 'end' => ']', - 'names' => array( 2 => null ), - 'min' => 2, - 'max' => 2, - ) - ); - $forInclusion = $flags & Parser::PTD_FOR_INCLUSION; $xmlishElements = $this->parser->getStripList(); @@ -252,9 +234,9 @@ class Preprocessor_Hash extends Preprocessor { } } elseif ( $curChar == $currentClosing ) { $found = 'close'; - } elseif ( isset( $rules[$curChar] ) ) { + } elseif ( isset( $this->rules[$curChar] ) ) { $found = 'open'; - $rule = $rules[$curChar]; + $rule = $this->rules[$curChar]; } else { # Some versions of PHP have a strcspn which stops on null characters # Ignore and continue @@ -557,7 +539,7 @@ class Preprocessor_Hash extends Preprocessor { # check for maximum matching characters (if there are 5 closing # characters, we will probably need only 3 - depending on the rules) - $rule = $rules[$piece->open]; + $rule = $this->rules[$piece->open]; if ( $count > $rule['max'] ) { # The specified maximum exists in the callback array, unless the caller # has made an error @@ -668,7 +650,7 @@ class Preprocessor_Hash extends Preprocessor { $piece->parts = array( new PPDPart_Hash ); $piece->count -= $matchingCount; # do we still qualify for any callback with remaining count? - $min = $rules[$piece->open]['min']; + $min = $this->rules[$piece->open]['min']; if ( $piece->count >= $min ) { $stack->push( $piece ); $accum =& $stack->getAccum(); -- 2.20.1