Move brace matching rules to Preprocessor class
authorOri Livneh <ori@wikimedia.org>
Sat, 31 Oct 2015 23:10:54 +0000 (16:10 -0700)
committerKrinkle <krinklemail@gmail.com>
Tue, 3 Nov 2015 02:57:05 +0000 (02:57 +0000)
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
includes/parser/Preprocessor_DOM.php
includes/parser/Preprocessor_Hash.php

index b1e49b2..df5d499 100644 (file)
@@ -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.
         *
index b71b9d2..4ca3a87 100644 (file)
@@ -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();
index 54824da..4f12414 100644 (file)
@@ -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();