Pastakhov has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/224050

Change subject: fix foreach operator (v 5.1.4)
......................................................................

fix foreach operator (v 5.1.4)

PHPTAGS_RUNTIME_RELEASE = 5
* public method \PhpTags\Renderer::getScopeID()

Change-Id: I221ea5bf664323a7f0b56ff202c07062bb20271b
---
M PhpTags.php
M includes/Compiler.php
M includes/Renderer.php
M includes/Runtime.php
M tests/phpunit/includes/RuntimeTest.php
5 files changed, 28 insertions(+), 17 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/PhpTags 
refs/changes/50/224050/1

diff --git a/PhpTags.php b/PhpTags.php
index 50a5ebb..7fb22d3 100644
--- a/PhpTags.php
+++ b/PhpTags.php
@@ -17,11 +17,11 @@
 
 const PHPTAGS_MAJOR_VERSION = 5;
 const PHPTAGS_MINOR_VERSION = 1;
-const PHPTAGS_RELEASE_VERSION = 3;
+const PHPTAGS_RELEASE_VERSION = 4;
 define( 'PHPTAGS_VERSION', PHPTAGS_MAJOR_VERSION . '.' . PHPTAGS_MINOR_VERSION 
. '.' . PHPTAGS_RELEASE_VERSION );
 
 const PHPTAGS_HOOK_RELEASE = 8;
-const PHPTAGS_RUNTIME_RELEASE = 4;
+const PHPTAGS_RUNTIME_RELEASE = 5;
 const PHPTAGS_JSONLOADER_RELEASE = 3;
 
 // Register this extension on Special:Version
diff --git a/includes/Compiler.php b/includes/Compiler.php
index 1b755f1..de8aa66 100644
--- a/includes/Compiler.php
+++ b/includes/Compiler.php
@@ -1555,7 +1555,6 @@
 
                $this->stack_push_memory();
                $asExpression = $arrayExpression;
-               $this->addValueIntoStack( $asExpression, $t_as, 
PHPTAGS_STACK_RESULT );
                $this->stack[] =& $t_as;
 
                if ( $this->id === '{' ) {
@@ -1569,13 +1568,13 @@
                $this->stack[] = array( 
PHPTAGS_STACK_COMMAND=>PHPTAGS_T_CONTINUE, PHPTAGS_STACK_RESULT=>1, 
PHPTAGS_STACK_TOKEN_LINE=>$tokenLine ); // Add operator T_CONTINUE to the end 
of the cycle
                $foreach = array(
                        PHPTAGS_STACK_COMMAND => PHPTAGS_T_FOREACH,
-                       PHPTAGS_STACK_PARAM => null,
+                       PHPTAGS_STACK_PARAM => &$t_as,
                        PHPTAGS_STACK_DO_TRUE => $this->stack,
                        PHPTAGS_STACK_TOKEN_LINE => $tokenLine,
                        PHPTAGS_STACK_DEBUG => $text,
                );
                $this->stack_pop_memory();
-               $this->addValueIntoStack( $arrayExpression, $foreach, 
PHPTAGS_STACK_PARAM );
+               $this->addValueIntoStack( $asExpression, $t_as, 
PHPTAGS_STACK_RESULT );
                $this->stack[] =& $foreach;
                return true;
        }
diff --git a/includes/Renderer.php b/includes/Renderer.php
index 4a32c0a..7d3d519 100644
--- a/includes/Renderer.php
+++ b/includes/Renderer.php
@@ -18,6 +18,8 @@
 
        static $globalVariablesScript = array();
 
+       private static $scopes = array();
+       private static $nextScopeID = 0;
        /**
         * Array of PPFrame
         * @var array
@@ -211,6 +213,8 @@
                self::$globalVariablesScript = array();
                self::$parserCacheDisabled = false;
                self::$errorCategoryAdded = false;
+               self::$scopes = array();
+               self::$nextScopeID = 0;
        }
 
        /**
@@ -223,16 +227,14 @@
                return $parser->insertStripItem( $text );
        }
 
-       private static function getScopeID( \PPFrame $frame ) {
-               static $frames = array(), $scope = 0;
-
-               foreach ( $frames as $value ) {
+       public static function getScopeID( \PPFrame $frame ) {
+               foreach ( self::$scopes as $value ) {
                        if ( $value[0] === $frame ) {
                                return $value[1];
                        }
                }
-               $frames[] = array( $frame, $scope );
-               return $scope++;
+               self::$scopes[] = array( $frame, self::$nextScopeID );
+               return self::$nextScopeID++;
        }
 
        public static function writeLimitReport() {
@@ -241,12 +243,12 @@
                $time = Timer::getRunTime();
                $compileTime = Timer::getCompileTime();
                $wgPhpTagsLimitReport = sprintf(
-                               '
+                               '-------------------- PhpTags Extension 
--------------------
 PhpTags usage count: %d
 Runtime : %.3f sec
 Compiler: %.3f sec ( usage: %d, cache: %d, memory: %d )
 Total   : %.3f sec
-
+-----------------------------------------------------------
 ',
                                $wgPhpTagsCounter,
                                $time - $compileTime,
diff --git a/includes/Runtime.php b/includes/Runtime.php
index 8d428ed..b858258 100644
--- a/includes/Runtime.php
+++ b/includes/Runtime.php
@@ -729,11 +729,15 @@
         * @param array $value
         */
        private static function doForeach ( &$value ) {
-               if ( !is_array($value[PHPTAGS_STACK_PARAM]) ) {
+               $t_as =& $value[PHPTAGS_STACK_PARAM];
+               $clone = $t_as[PHPTAGS_STACK_RESULT];
+               if ( !is_array( $clone ) ) {
                        self::pushException( new PhpTagsException( 
PhpTagsException::WARNING_INVALID_ARGUMENT_FOR_FOREACH, null ) );
                        return;
                }
-               reset( $value[PHPTAGS_STACK_PARAM] );
+               unset( $t_as[PHPTAGS_STACK_RESULT] );
+               reset( $clone );
+               $t_as[PHPTAGS_STACK_RESULT] = $clone;
                $null = null;
                self::pushDown( $value[PHPTAGS_STACK_DO_TRUE], T_WHILE, $null );
        }
@@ -753,8 +757,8 @@
         */
        private static function doAs ( &$value ) {
                // $value[PHPTAGS_STACK_RESULT] is always array, checked in 
self::doForeach()
-               $tmp = each( $value[PHPTAGS_STACK_RESULT] );
-               if ( $tmp === false ) { // it is last element
+               $tmp = each( $value[PHPTAGS_STACK_RESULT] ); // 'each' can 
return false and null
+               if ( ! $tmp ) { // it is last element
                        self::popUp();
                }
 
diff --git a/tests/phpunit/includes/RuntimeTest.php 
b/tests/phpunit/includes/RuntimeTest.php
index 24b6d2a..ea914f1 100644
--- a/tests/phpunit/includes/RuntimeTest.php
+++ b/tests/phpunit/includes/RuntimeTest.php
@@ -2951,6 +2951,12 @@
                                array( (string)new PhpTagsException( 
PhpTagsException::WARNING_INVALID_ARGUMENT_FOR_FOREACH, null, 1, 'test' ) )
                        );
        }
+       public function testRun_foreach_14() {
+               $this->assertEquals(
+                               Runtime::runSource( '$a = ["one", "two"]; 
foreach ($a as $v) { echo "-$v-"; $a = false; } echo $a === false ? "false" : 
"not false";', array('test'), 1 ),
+                               array( '-one-', '-two-', 'false' )
+                       );
+       }
 
        public function testRun_constant_version() {
                $this->assertEquals(

-- 
To view, visit https://gerrit.wikimedia.org/r/224050
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I221ea5bf664323a7f0b56ff202c07062bb20271b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/PhpTags
Gerrit-Branch: master
Gerrit-Owner: Pastakhov <pastak...@yandex.ru>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to