diff --git a/Library/Operators/Other/EmptyOperator.php b/Library/Operators/Other/EmptyOperator.php index 6980947cdf2..458090044a4 100644 --- a/Library/Operators/Other/EmptyOperator.php +++ b/Library/Operators/Other/EmptyOperator.php @@ -35,13 +35,7 @@ public function compile($expression, CompilationContext $compilationContext) { $compilationContext->headersManager->add('kernel/operators'); - if (isset($expression['left']['type']) - && $expression['left']['type'] == 'variable' - ) { - return $this->evaluateVariableExpression($expression, $compilationContext); - } - - return new CompiledExpression('int', '(0 == 0)', $expression); + return $this->evaluateVariableExpression($expression, $compilationContext); } /** @@ -61,10 +55,25 @@ protected function evaluateVariableExpression( CompilationContext $compilationContext ) { + switch (true) { + case isset($expression['left']['left']['value']) + && isset($expression['left']['left']['type']) + && $expression['left']['left']['type'] == 'variable': + + $name = $expression['left']['left']['value']; + + break; + + default: + throw new Exception( + 'Empty syntax not supported' + ); + } + $variable = $compilationContext->symbolTable->getVariableForWrite( - $expression['left']['value'], + $name, $compilationContext, - $expression['left'] + $expression['left']['left'] ); return new CompiledExpression( diff --git a/Library/SymbolTable.php b/Library/SymbolTable.php index bc37e12c3c6..35e8b0a3f32 100644 --- a/Library/SymbolTable.php +++ b/Library/SymbolTable.php @@ -189,11 +189,11 @@ public function getVariableForRead($name, CompilationContext $compilationContext /** * Return a variable in the symbol table, it will be used for a write operation - * Some variables aren't writables themselves but their members do + * Some variables aren't writable themselves but their members do * * @param string $name - * @param array $statement * @param CompilationContext $compilationContext + * @param array $statement * @return \Variable */ public function getVariableForWrite($name, $compilationContext, $statement=null) diff --git a/test/flow.zep b/test/flow.zep index a966cb06ada..959c94d1c09 100644 --- a/test/flow.zep +++ b/test/flow.zep @@ -119,15 +119,15 @@ class Flow if b { let c = 1; if c { - return 1; + return 654; } else { - return 0; + return -1; } } else { - return 0; + return -2; } } else { - return 0; + return -3; } } @@ -142,7 +142,7 @@ class Flow if b { let c = 1; if c { - return 1; + return 987; } else { return 0; } @@ -158,7 +158,7 @@ class Flow { int a, b; let a = 1, b = 2; - if a + b { return 1; } + if a + b { return -12; } return 0; } @@ -166,7 +166,7 @@ class Flow { var a, b; let a = 1, b = 2; - if a + b { return 1; } + if a + b { return 74; } return 0; } @@ -174,10 +174,16 @@ class Flow { var a, b, c; let a = 1, b = 2, c = 3; - if a + b + c { return 1; } + if a + b + c { return 89; } return 0; } + public function testIf16(var a) + { + if empty(a) { return true; } + return false; + } + public function testLoop1() { var a; diff --git a/unit-tests/flow.php b/unit-tests/flow.php index 8f805dbd6e8..42f099261cf 100644 --- a/unit-tests/flow.php +++ b/unit-tests/flow.php @@ -11,7 +11,15 @@ assert($t->testIf7() === 1); assert($t->testIf8() === 0); assert($t->testIf9() === 1); -assert($t->testIf10() === 1); +assert($t->testIf10() === 654); +assert($t->testIf12() === 987); +assert($t->testIf13() === -12); +assert($t->testIf14() === 74); +assert($t->testIf15() === 89); +assert($t->testIf16(array()) === false); +assert($t->testIf16('') === true); +assert($t->testIf16(null) === true); +assert($t->testIf16(' ') === false); assert($t->testLoop1() === true); assert($t->testLoop2() === 5);