/**
  * @param Expr $expr
  *
  * @return array Returns an array of types, or an empty array if the expression doesn't return a value
  */
 public static function resolve(Expr $expr)
 {
     if (!$expr->getAttribute("scope") or !$expr->getAttribute("scopeInner")) {
         throw new \InvalidArgumentException("Expr must have its scope and scopeInner resolved");
     }
     switch (get_class($expr)) {
         default:
             return BasicExpressionReturnTypeResolver::resolve($expr);
     }
 }
Example #2
0
 /**
  * Constructs an array item node.
  *
  * @param Expr $value Value
  * @param null|Expr $key Key
  * @param bool $byRef Whether to assign by reference
  * @param array $attributes Additional attributes
  */
 public function __construct(Expr $value, Expr $key = null, $byRef = false, array $attributes = array())
 {
     parent::__construct($attributes);
     $this->key = $key;
     $this->value = $value;
     $this->byRef = $byRef;
 }
 protected function reportArgument(Expr $arg)
 {
     if ($arg instanceof BinaryOp) {
         $this->reportArgument($arg->left);
         $this->reportArgument($arg->right);
     } elseif ($arg instanceof Encapsed) {
         foreach ($arg->parts as $part) {
             if ($part instanceof Variable) {
                 $this->reportArgument($part);
             }
         }
     } elseif ($arg instanceof Variable) {
         $warn = "Translatable text contains variable '{$arg->name}' on line {$arg->getLine()}, consider using literal string instead.";
         $this->output->writeln("<comment>{$warn}</comment>");
     }
 }
Example #4
0
 /**
  * Constructs a static method call node.
  *
  * @param Node\Name|Expr $class Class name
  * @param string|Expr $name Method name
  * @param Node\Arg[] $args Arguments
  * @param array $attributes Additional attributes
  */
 public function __construct($class, $name, array $args = array(), array $attributes = array())
 {
     parent::__construct($attributes);
     $this->class = $class;
     $this->name = $name;
     $this->args = $args;
 }
Example #5
0
 /**
  * Constructs a function call node.
  *
  * @param Expr        $var        Variable holding object
  * @param string|Expr $name       Method name
  * @param Arg[]       $args       Arguments
  * @param array       $attributes Additional attributes
  */
 public function __construct(Expr $var, $name, array $args = array(), array $attributes = array())
 {
     parent::__construct(null, $attributes);
     $this->var = $var;
     $this->name = $name;
     $this->args = $args;
 }
Example #6
0
 /**
  * Constructs a ternary operator node.
  *
  * @param Expr      $cond       Condition
  * @param null|Expr $if         Expression for true
  * @param Expr      $else       Expression for false
  * @param array                    $attributes Additional attributes
  */
 public function __construct(Expr $cond, $if, Expr $else, array $attributes = array())
 {
     parent::__construct(null, $attributes);
     $this->cond = $cond;
     $this->if = $if;
     $this->else = $else;
 }
Example #7
0
 /**
  * Constructs a lambda function node.
  *
  * @param array $subNodes   Array of the following optional subnodes:
  *                          'static'     => false  : Whether the closure is static
  *                          'byRef'      => false  : Whether to return by reference
  *                          'params'     => array(): Parameters
  *                          'uses'       => array(): use()s
  *                          'returnType' => null   : Return type
  *                          'stmts'      => array(): Statements
  * @param array $attributes Additional attributes
  */
 public function __construct(array $subNodes = array(), array $attributes = array()) {
     parent::__construct($attributes);
     $this->static = isset($subNodes['static']) ? $subNodes['static'] : false;
     $this->byRef = isset($subNodes['byRef']) ? $subNodes['byRef'] : false;
     $this->params = isset($subNodes['params']) ? $subNodes['params'] : array();
     $this->uses = isset($subNodes['uses']) ? $subNodes['uses'] : array();
     $this->returnType = isset($subNodes['returnType']) ? $subNodes['returnType'] : null;
     $this->stmts = isset($subNodes['stmts']) ? $subNodes['stmts'] : array();
 }
Example #8
0
 /**
  * Constructs a bitwise and node.
  *
  * @param Expr $left The left hand side expression
  * @param Expr $right The right hand side expression
  * @param array $attributes Additional attributes
  */
 public function __construct(Expr $left, Expr $right, array $attributes = array())
 {
     parent::__construct($attributes);
     $this->left = $left;
     $this->right = $right;
 }
Example #9
0
 /**
  * Constructs an array item node.
  *
  * @param Expr      $value      Value
  * @param null|Expr $key        Key
  * @param bool      $byRef      Whether to assign by reference
  * @param array     $attributes Additional attributes
  */
 public function __construct(Expr $value, Expr $key = null, $byRef = false, array $attributes = array())
 {
     parent::__construct(array('key' => $key, 'value' => $value, 'byRef' => $byRef), $attributes);
 }
Example #10
0
 /**
  * Constructs an instanceof check node.
  *
  * @param Expr      $expr       Expression
  * @param Name|Expr $class      Class name
  * @param array     $attributes Additional attributes
  */
 public function __construct(Expr $expr, $class, array $attributes = array())
 {
     parent::__construct(null, $attributes);
     $this->expr = $expr;
     $this->class = $class;
 }
Example #11
0
 /**
  * Constructs a shell exec (backtick) node.
  *
  * @param array $parts      Encapsed string array
  * @param array $attributes Additional attributes
  */
 public function __construct($parts, array $attributes = array())
 {
     parent::__construct(array('parts' => $parts), $attributes);
 }
Example #12
0
 /**
  * Constructs an "yield from" node.
  *
  * @param Expr $expr Expression
  * @param array $attributes Additional attributes
  */
 public function __construct(Expr $expr, array $attributes = array())
 {
     parent::__construct($attributes);
     $this->expr = $expr;
 }
Example #13
0
 /**
  * Constructs an assignment node.
  *
  * @param Expr  $var        Variable
  * @param Expr  $expr       Expression
  * @param array $attributes Additional attributes
  */
 public function __construct(Expr $var, Expr $expr, array $attributes = array())
 {
     parent::__construct(array('var' => $var, 'expr' => $expr), $attributes);
 }
Example #14
0
 private function resolveExpressionValue(\PhpParser\Node\Expr $expr)
 {
     if ($expr instanceof \PhpParser\Node\Scalar\String_) {
         return array('type' => 'string', 'value' => $expr->getAttribute('originalValue'));
     }
     if ($expr instanceof \PhpParser\Node\Scalar\LNumber || $expr instanceof \PhpParser\Node\Expr\UnaryMinus || $expr instanceof \PhpParser\Node\Expr\UnaryPlus) {
         return array('type' => 'integer', 'value' => $expr->getAttribute('originalValue'));
     }
     if ($expr instanceof \PhpParser\Node\Scalar\DNumber) {
         return array('type' => 'float', 'value' => $expr->getAttribute('originalValue'));
     }
     if ($expr instanceof \PhpParser\Node\Expr\Array_) {
         return array('type' => 'array', 'value' => '');
     }
     if ($expr instanceof \PhpParser\Node\Expr\ClassConstFetch) {
         return array('type' => '{unknown}', 'value' => '', 'constant' => join('\\', $expr->class->parts) . '::' . $expr->name);
     }
     if ($expr instanceof \PhpParser\Node\Expr\ConstFetch) {
         $reference = join('\\', $expr->name->parts);
         if (in_array(strtolower($reference), array('true', 'false'))) {
             return array('type' => 'boolean', 'value' => $reference);
         }
         return array('type' => '{unknown}', 'value' => '', 'constant' => join('\\', $expr->name->parts));
     }
     if ($expr instanceof \PhpParser\Node\Scalar\MagicConst\Line) {
         return array('type' => 'integer', 'value' => '', 'constant' => $expr->getName());
     }
     if ($expr instanceof \PhpParser\Node\Scalar\MagicConst) {
         return array('type' => 'string', 'value' => '', 'constant' => $expr->getName());
     }
     $type = get_class($expr);
     $line = $expr->getLine();
     $file = $this->result->getFileName();
     throw new ParseErrorException("Unexpected expression type '{$type}' for value in line {$line} of file '{$file}'", ParseErrorException::UnexpectedExpr);
 }
 /**
  * Constructs an array node.
  *
  * @param ArrayItem[] $items      Items of the array
  * @param array       $attributes Additional attributes
  */
 public function __construct(array $items = array(), array $attributes = array())
 {
     parent::__construct(null, $attributes);
     $this->items = $items;
 }
 /**
  * Constructs an include node.
  *
  * @param Expr  $expr       Expression
  * @param int   $type       Type of include
  * @param array $attributes Additional attributes
  */
 public function __construct(Expr $expr, $type, array $attributes = array())
 {
     parent::__construct(null, $attributes);
     $this->expr = $expr;
     $this->type = $type;
 }
Example #17
0
 /**
  * Constructs an instanceof check node.
  *
  * @param Expr      $expr       Expression
  * @param Name|Expr $class      Class name
  * @param array     $attributes Additional attributes
  */
 public function __construct(Expr $expr, $class, array $attributes = array())
 {
     parent::__construct(array('expr' => $expr, 'class' => $class), $attributes);
 }
Example #18
0
 /**
  * Constructs an array node.
  *
  * @param Expr[] $vars       Variables
  * @param array  $attributes Additional attributes
  */
 public function __construct(array $vars, array $attributes = array())
 {
     parent::__construct(array('vars' => $vars), $attributes);
 }
Example #19
0
 /**
  * Constructs an include node.
  *
  * @param Expr  $expr       Expression
  * @param int   $type       Type of include
  * @param array $attributes Additional attributes
  */
 public function __construct(Expr $expr, $type, array $attributes = array())
 {
     parent::__construct(array('expr' => $expr, 'type' => $type), $attributes);
 }
Example #20
0
 /**
  * Constructs a yield expression node.
  *
  * @param null|Expr $value      Value expression
  * @param null|Expr $key        Key expression
  * @param array     $attributes Additional attributes
  */
 public function __construct(Expr $value = null, Expr $key = null, array $attributes = array())
 {
     parent::__construct(null, $attributes);
     $this->key = $key;
     $this->value = $value;
 }
Example #21
0
 /**
  * Constructs a shell exec (backtick) node.
  *
  * @param array $parts Encapsed string array
  * @param array $attributes Additional attributes
  */
 public function __construct(array $parts, array $attributes = array())
 {
     parent::__construct($attributes);
     $this->parts = $parts;
 }
Example #22
0
 /**
  * Constructs an array index fetch node.
  *
  * @param Expr      $var        Variable
  * @param null|Expr $dim        Array index / dim
  * @param array     $attributes Additional attributes
  */
 public function __construct(Expr $var, Expr $dim = null, array $attributes = array())
 {
     parent::__construct($attributes);
     $this->var = $var;
     $this->dim = $dim;
 }
        /**
         * @param AbstractVariableObject     $variable
         * @param \PhpParser\Node\Expr       $default
         * @return string
         */
        private function setVariableDefaultValue(AbstractVariableObject $variable, \PhpParser\Node\Expr $default = NULL) {
            if ($default === NULL) {
                return;
            }
            if ($default instanceof \PhpParser\Node\Scalar\String) {
                $variable->setDefault($default->getAttribute('originalValue'));
                if ($variable->getType() == '{unknown}') {
                    $variable->setType('string');
                }
                return;
            }
            if ($default instanceof \PhpParser\Node\Scalar\LNumber ||
                $default instanceof \PhpParser\Node\Expr\UnaryMinus ||
                $default instanceof \PhpParser\Node\Expr\UnaryPlus) {
                $variable->setDefault($default->getAttribute('originalValue'));
                if ($variable->getType() == '{unknown}') {
                    $variable->setType('integer');
                }
                return;
            }
            if ($default instanceof \PhpParser\Node\Scalar\DNumber) {
                $variable->setDefault($default->getAttribute('originalValue'));
                if ($variable->getType() == '{unknown}') {
                    $variable->setType('float');
                }
                return;
            }
            if ($default instanceof \PhpParser\Node\Expr\Array_) {
                //var_dump($default);
                //$parameter->setDefault(join('\\', $default->items));
                if ($variable->getType() == '{unknown}') {
                    $variable->setType('array');
                }
                return;
            }
            if ($default instanceof \PhpParser\Node\Expr\ClassConstFetch) {
                $variable->setDefault(join('\\', $default->class->parts) . '::' . $default->name);
                return;
            }
            if ($default instanceof \PhpParser\Node\Expr\ConstFetch) {
                $variable->setDefault(join('\\', $default->name->parts));
                return;
            }
            if ($default instanceof \PhpParser\Node\Scalar\MagicConst\Trait_) {
                $variable->setName('__TRAIT__');
                return;
            }
            if ($default instanceof \PhpParser\Node\Scalar\MagicConst\Class_) {
                $variable->setDefault('__CLASS__');
                return;
            }
            if ($default instanceof \PhpParser\Node\Scalar\MagicConst\Method) {
                $variable->setName('__METHOD__');
                return;
            }
            if ($default instanceof \PhpParser\Node\Scalar\MagicConst\Dir) {
                $variable->setName('__DIR__');
                return;
            }
            if ($default instanceof \PhpParser\Node\Scalar\MagicConst\File) {
                $variable->setName('__FILE__');
                return;
            }
            if ($default instanceof \PhpParser\Node\Scalar\MagicConst\Function_) {
                $variable->setName('__FUNC__');
                return;
            }
            if ($default instanceof \PhpParser\Node\Scalar\MagicConst\Line) {
                $variable->setName('__LINE__');
                return;
            }

            $type = get_class($default);
            $line = $default->startLine;
            $file = $this->result->getFileName();
            throw new ParseErrorException("Unexpected expression type '$type' for default value in line $line of file '$file'", ParseErrorException::UnexpectedExpr);
        }
Example #24
0
 /**
  * Constructs a function call node.
  *
  * @param Node\Name|Expr $class      Class name
  * @param Node\Arg[]     $args       Arguments
  * @param array          $attributes Additional attributes
  */
 public function __construct($class, array $args = array(), array $attributes = array())
 {
     parent::__construct(null, $attributes);
     $this->class = $class;
     $this->args = $args;
 }
Example #25
0
 /**
  * Constructs a variable node.
  *
  * @param string|Expr $name       Name
  * @param array                      $attributes Additional attributes
  */
 public function __construct($name, array $attributes = array())
 {
     parent::__construct(null, $attributes);
     $this->name = $name;
 }
 /**
  * Constructs a function call node.
  *
  * @param Expr        $var        Variable holding object
  * @param string|Expr $name       Property name
  * @param array       $attributes Additional attributes
  */
 public function __construct(Expr $var, $name, array $attributes = array())
 {
     parent::__construct(array('var' => $var, 'name' => $name), $attributes);
 }
Example #27
0
 /**
  * Constructs a post increment node.
  *
  * @param Expr $var
  *        	Variable
  * @param array $attributes
  *        	Additional attributes
  */
 public function __construct(Expr $var, array $attributes = array())
 {
     parent::__construct(null, $attributes);
     $this->var = $var;
 }
Example #28
0
 /**
  * Constructs a const fetch node.
  *
  * @param Name  $name       Constant name
  * @param array $attributes Additional attributes
  */
 public function __construct(Name $name, array $attributes = array())
 {
     parent::__construct(array('name' => $name), $attributes);
 }
Example #29
0
 /**
  * Constructs an array node.
  *
  * @param Expr[] $vars
  *        	Variables
  * @param array $attributes
  *        	Additional attributes
  */
 public function __construct(array $vars, array $attributes = array())
 {
     parent::__construct(null, $attributes);
     $this->vars = $vars;
 }
Example #30
0
 private function parseOperatorNode(Node\Expr $node)
 {
     $nodeType = str_replace('Expr_', '', $node->getType());
     switch (true) {
         case isset(self::$assignOperatorsMap[$nodeType]):
             return Expression::assign($this->parseNode($node->var), self::$assignOperatorsMap[$nodeType], $this->parseNode($node->expr));
         case $node instanceof Node\Expr\Instanceof_:
             return Expression::binaryOperation($this->parseNode($node->expr), Operators\Binary::IS_INSTANCE_OF, $this->parseNameNode($node->class));
         case isset(self::$binaryOperatorsMap[$nodeType]):
             return Expression::binaryOperation($this->parseNode($node->left), self::$binaryOperatorsMap[$nodeType], $this->parseNode($node->right));
         case isset(self::$unaryOperatorsMap[$nodeType]):
             return Expression::unaryOperation(self::$unaryOperatorsMap[$nodeType], $this->parseNode(isset($node->expr) ? $node->expr : $node->var));
         case isset(self::$castOperatorMap[$nodeType]):
             return Expression::cast(self::$castOperatorMap[$nodeType], $this->parseNode($node->expr));
         default:
             return null;
     }
 }