Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
[Refactoring] Split BinaryOp\BooleanOr
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr committed Jul 9, 2015
1 parent 862b9cf commit bf401a0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 39 deletions.
41 changes: 2 additions & 39 deletions src/Visitor/Expression.php
Expand Up @@ -64,6 +64,8 @@ protected function factory($expr)
return new Expression\BinaryOp\Mul();
case 'PhpParser\Node\Expr\BinaryOp\BitwiseXor':
return new Expression\BinaryOp\BitwiseXor();
case 'PhpParser\Node\Expr\BinaryOp\BooleanOr':
return new Expression\BinaryOp\BooleanOr();
}

return false;
Expand All @@ -85,11 +87,6 @@ public function compile($expr)
return $this->passSymbol($expr);
case 'PhpParser\Node\Expr\Variable':
return $this->passExprVariable($expr);
/**
* Operators
*/
case 'PhpParser\Node\Expr\BinaryOp\BooleanOr':
return $this->passBinaryOpBooleanOr($expr);
/**
* Another
*/
Expand Down Expand Up @@ -430,40 +427,6 @@ protected function passUnaryMinus(Node\Expr\UnaryMinus $expr)
return new CompiledExpression();
}

/**
* {expr} || {expr}
*
* @param Node\Expr\BinaryOp\BooleanOr $expr
* @return CompiledExpression
*/
protected function passBinaryOpBooleanOr(Node\Expr\BinaryOp\BooleanOr $expr)
{
$expression = new Expression($this->context);
$left = $expression->compile($expr->left);

$expression = new Expression($this->context);
$right = $expression->compile($expr->right);

switch ($left->getType()) {
case CompiledExpression::LNUMBER:
case CompiledExpression::DNUMBER:
case CompiledExpression::STRING:
case CompiledExpression::BOOLEAN:
case CompiledExpression::NULL:
switch ($right->getType()) {
case CompiledExpression::LNUMBER:
case CompiledExpression::DNUMBER:
case CompiledExpression::STRING:
case CompiledExpression::BOOLEAN:
case CompiledExpression::NULL:
return CompiledExpression::fromZvalValue($left->getValue() || $right->getValue());
}
break;
}

return new CompiledExpression(CompiledExpression::UNKNOWN);
}

/**
* Convert lnumber scalar expr to CompiledExpression
*
Expand Down
48 changes: 48 additions & 0 deletions src/Visitor/Expression/BinaryOp/BooleanOr.php
@@ -0,0 +1,48 @@
<?php

namespace PHPSA\Visitor\Expression\BinaryOp;

use PHPSA\CompiledExpression;
use PHPSA\Context;
use PHPSA\Visitor\Expression;
use PHPSA\Visitor\Expression\AbstractExpressionCompiler;

class BooleanOr extends AbstractExpressionCompiler
{
protected $name = '\PhpParser\Node\Expr\BinaryOp\BooleanOr';

/**
* {expr} || {expr}
*
* @param \PhpParser\Node\Expr\BinaryOp\BooleanOr $expr
* @param Context $context
* @return CompiledExpression
*/
public function compile($expr, Context $context)
{
$expression = new Expression($context);
$left = $expression->compile($expr->left);

$expression = new Expression($context);
$right = $expression->compile($expr->right);

switch ($left->getType()) {
case CompiledExpression::LNUMBER:
case CompiledExpression::DNUMBER:
case CompiledExpression::STRING:
case CompiledExpression::BOOLEAN:
case CompiledExpression::NULL:
switch ($right->getType()) {
case CompiledExpression::LNUMBER:
case CompiledExpression::DNUMBER:
case CompiledExpression::STRING:
case CompiledExpression::BOOLEAN:
case CompiledExpression::NULL:
return CompiledExpression::fromZvalValue($left->getValue() || $right->getValue());
}
break;
}

return new CompiledExpression(CompiledExpression::UNKNOWN);
}
}

0 comments on commit bf401a0

Please sign in to comment.