Pastakhov has uploaded a new change for review.

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


Change subject: add T_UNSET operator
......................................................................

add T_UNSET operator

Time: 426 ms, Memory: 25.25Mb
OK (451 tests, 457 assertions)

Change-Id: I0233d09eb7603b90781bf00bb7d46ccd9eeea891
---
M includes/Runtime.php
M includes/functions/var.php
M tests/phpunit/includes/RuntimeTest.php
3 files changed, 57 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Foxway 
refs/changes/35/93935/1

diff --git a/includes/Runtime.php b/includes/Runtime.php
index 5f73f50..fc0e618 100644
--- a/includes/Runtime.php
+++ b/includes/Runtime.php
@@ -957,6 +957,36 @@
                                                        }
                                                }
                                                break;
+                                       case T_UNSET:
+                                               
foreach($value[FOXWAY_STACK_PARAM] as $val) {
+                                                       if( 
$val[FOXWAY_STACK_COMMAND] != T_VARIABLE ) { // Example: isset($foo);
+                                                               throw new 
Exception; // @todo
+                                                       }
+                                                       $vn = 
$val[FOXWAY_STACK_PARAM]; // Variable Name
+                                                       if( 
isset($thisVariables[$vn]) ) { // defined variable
+                                                               if( 
isset($val[FOXWAY_STACK_ARRAY_INDEX]) ) { // There is array index. Example: 
unset($foo[0])
+                                                                       $ref = 
&$thisVariables[$vn];
+                                                                       $tmp = 
array_pop( $val[FOXWAY_STACK_ARRAY_INDEX] );
+                                                                       
foreach( $val[FOXWAY_STACK_ARRAY_INDEX] as $v ) {
+                                                                               
if( !isset($ref[$v]) ) { // undefined array index
+                                                                               
        // @todo PHP Notice:  Undefined variable:
+                                                                               
        continue 2;
+                                                                               
}
+                                                                               
$ref = &$ref[$v];
+                                                                       }
+                                                                       if( 
is_array($ref) ) {
+                                                                               
unset( $ref[$tmp] );
+                                                                       }else{
+                                                                               
// @todo PHP Fatal error:  Cannot unset string offsets
+                                                                       }
+                                                               }else{ // There 
is no array index. Example: unset($foo)
+                                                                       unset( 
$thisVariables[$vn] );
+                                                               }
+                                                       }elseif( 
isset($val[FOXWAY_STACK_ARRAY_INDEX]) ) { // undefined variable with array 
index. Example: unset($foo[1])
+                                                               // @todo PHP 
Notice:  Undefined variable:
+                                                       }
+                                               }
+                                               break;
                                        case T_ISSET:
                                                
foreach($value[FOXWAY_STACK_PARAM] as $val) {
                                                        if( 
$val[FOXWAY_STACK_COMMAND] != T_VARIABLE ) { // Example: isset($foo);
@@ -968,7 +998,7 @@
                                                        } // true, variable is 
defined
                                                        if( 
isset($val[FOXWAY_STACK_ARRAY_INDEX]) ) { // Example: isset($foo[1])
                                                                $ref = 
&$thisVariables[ $val[FOXWAY_STACK_PARAM] ];
-                                                               $vn = 
array_pop( $val[FOXWAY_STACK_ARRAY_INDEX] );
+                                                               $tmp = 
array_pop( $val[FOXWAY_STACK_ARRAY_INDEX] );
                                                                foreach( 
$val[FOXWAY_STACK_ARRAY_INDEX] as $v ) {
                                                                        if( 
!isset($ref[$v]) ) { // undefined array index
                                                                                
$value[FOXWAY_STACK_RESULT] = false;
@@ -977,7 +1007,7 @@
                                                                        $ref = 
&$ref[$v];
                                                                }
                                                                // @todo 
->>>>>>>>>>>> | ************************************************************* | 
<<<<< it only for compatible with PHP 5.4 if used PHP 5.3 @see 
http://www.php.net/manual/en/function.isset.php Example #2 isset() on String 
Offsets
-                                                               if( 
!isset($ref[$vn]) || (is_string($ref) && is_string($vn) && $vn != 
(string)(int)$vn) ) {
+                                                               if( 
!isset($ref[$tmp]) || (is_string($ref) && is_string($tmp) && $tmp != 
(string)(int)$tmp) ) {
                                                                        
$value[FOXWAY_STACK_RESULT] = false;
                                                                        break 2;
                                                                }
@@ -996,7 +1026,7 @@
                                                                $ref = 
&$val[FOXWAY_STACK_RESULT];
                                                        }
                                                        if( 
isset($val[FOXWAY_STACK_ARRAY_INDEX]) ) { // Example: empty($foo[1])
-                                                               $vn = 
array_pop( $val[FOXWAY_STACK_ARRAY_INDEX] );
+                                                               $tmp = 
array_pop( $val[FOXWAY_STACK_ARRAY_INDEX] );
                                                                foreach( 
$val[FOXWAY_STACK_ARRAY_INDEX] as $v ) {
                                                                        if( 
!isset($ref[$v]) ) { // undefined array index
                                                                                
continue 2;
@@ -1004,7 +1034,7 @@
                                                                        $ref = 
&$ref[$v];
                                                                }
                                                                // @todo 
->>>>>>>>>>>> | ************************************************************* | 
<<<<< it only for compatible with PHP 5.4 if used PHP 5.3 @see 
http://www.php.net/manual/en/function.empty.php Example #2 empty() on String 
Offsets
-                                                               if( 
!empty($ref[$vn]) && (is_array($ref) || !is_string($vn) || $vn == 
(string)(int)$vn) ) {
+                                                               if( 
!empty($ref[$tmp]) && (is_array($ref) || !is_string($tmp) || $tmp == 
(string)(int)$tmp) ) {
                                                                        
$value[FOXWAY_STACK_RESULT] = false;
                                                                        break 2;
                                                                }
diff --git a/includes/functions/var.php b/includes/functions/var.php
index bd31fa2..9f05252 100644
--- a/includes/functions/var.php
+++ b/includes/functions/var.php
@@ -13,7 +13,7 @@
        'boolval' => array( 1=>function($args) { return (bool)$args[0]; } ),
 // @todo debug_zval_dump()
        'doubleval' => array( 1=>function($args) { return doubleval($args[0]); 
} ),
-// @todo empty() in runtime.php
+       // empty() in runtime.php
        'floatval' => array( 1=>function($args) { return floatval($args[0]); } 
),
 // @todo get_defined_vars() in runtime.php
        // get_resource_type() it have not resources
@@ -38,7 +38,7 @@
        // is_resource it have not resources
        'is_scalar' => array( 1=>function($args) { return is_scalar($args[0]); 
} ),
        'is_string' => array( 1=>function($args) { return is_string($args[0]); 
} ),
-// @todo isset() in runtime.php
+       // isset() in runtime.php
        'print_r' => array(
                FOXWAY_DEFAULT_VALUES=>array( 1=>false ),
                2=>function($args) { $ret = print_r($args[0], true); return 
$args[1] ? $ret : new Foxway\outPrint(true, $ret); },
@@ -47,7 +47,7 @@
        'settype' => array( 2=>function($args) { return settype($args[0], 
$args[1]); } ),
        'strval' => array( 1=>function($args) { return strval($args[0]); } ),
 // @todo mixed unserialize ( string $str )
-// @todo unset() in runtime.php
+       // unset() in runtime.php
        'var_dump' => array(
                FOXWAY_MIN_VALUES=>1,
                ''=>function($args) { ob_start(); 
call_user_func_array('var_dump', $args); return new Foxway\outPrint( null, 
ob_get_clean() ); },
diff --git a/tests/phpunit/includes/RuntimeTest.php 
b/tests/phpunit/includes/RuntimeTest.php
index 5d02676..7d8796e 100644
--- a/tests/phpunit/includes/RuntimeTest.php
+++ b/tests/phpunit/includes/RuntimeTest.php
@@ -2975,4 +2975,24 @@
                                array('false')
                                );
        }
+
+       public function testRun_echo_unset_1() {
+               $this->assertEquals(
+                               Runtime::runSource('$var = "string"; echo 
isset($var) ? "true" : "false"; unset($var); echo isset($var) ? "true" : 
"false";'),
+                               array('true', 'false')
+                               );
+       }
+       public function testRun_echo_unset_2() {
+               $this->assertEquals(
+                               Runtime::runSource('$var = array("string"); 
echo isset($var[0]) ? "true" : "false"; unset($var[0]); echo isset($var[0]) ? 
"true" : "false";'),
+                               array('true', 'false')
+                               );
+       }
+       public function testRun_echo_unset_3() {
+               $this->assertEquals(
+                               Runtime::runSource('$var = array("foo" => 
"string"); echo isset($var["foo"]) ? "true" : "false"; unset($var["foo"]); echo 
isset($var["foo"]) ? "true" : "false";'),
+                               array('true', 'false')
+                               );
+       }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0233d09eb7603b90781bf00bb7d46ccd9eeea891
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Foxway
Gerrit-Branch: develop
Gerrit-Owner: Pastakhov <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to