diff --git a/lib/PHPCfg/Op/Expr/ArrowFunction.php b/lib/PHPCfg/Op/Expr/ArrowFunction.php new file mode 100755 index 0000000..164fc59 --- /dev/null +++ b/lib/PHPCfg/Op/Expr/ArrowFunction.php @@ -0,0 +1,37 @@ +func = $func; + } + + public function getVariableNames(): array + { + return ['result']; + } + + public function getFunc(): Func + { + return $this->func; + } +} diff --git a/lib/PHPCfg/Parser.php b/lib/PHPCfg/Parser.php index ad71a9b..7c589fe 100755 --- a/lib/PHPCfg/Parser.php +++ b/lib/PHPCfg/Parser.php @@ -1060,6 +1060,29 @@ protected function parseExpr_Closure(Expr\Closure $expr) return $closure; } + protected function parseExpr_ArrowFunction(Expr\ArrowFunction $expr) + { + $flags = Func::FLAG_CLOSURE; + $flags |= $expr->byRef ? Func::FLAG_RETURNS_REF : 0; + $flags |= $expr->static ? Func::FLAG_STATIC : 0; + + $this->script->functions[] = $func = new Func( + '{anonymous}#' . ++$this->anonId, + $flags, + $this->parseTypeNode($expr->returnType), + null, + ); + $stmts = [ + new Stmt\Return_($expr->expr), + ]; + $this->parseFunc($func, $expr->params, $stmts); + + $closure = new Op\Expr\ArrowFunction($func, $this->mapAttributes($expr)); + $func->callableOp = $closure; + + return $closure; + } + protected function parseExpr_ClassConstFetch(Expr\ClassConstFetch $expr) { $c = $this->readVariable($this->parseExprNode($expr->class)); diff --git a/test/code/arrow_fn.test b/test/code/arrow_fn.test new file mode 100644 index 0000000..98706d6 --- /dev/null +++ b/test/code/arrow_fn.test @@ -0,0 +1,48 @@ + $b++ * $a; +$fn(3); +var_dump($b); +----- +Block#1 + Expr_Assign + var: Var#1<$b> + expr: LITERAL(1) + result: Var#2 + Expr_ArrowFunction<'{anonymous}#1'> + result: Var#3 + Expr_Assign + var: Var#4<$fn> + expr: Var#3 + result: Var#5 + Expr_FuncCall + name: Var#4<$fn> + args[0]: LITERAL(3) + result: Var#6 + Expr_FuncCall + name: LITERAL('var_dump') + args[0]: Var#1<$b> + result: Var#7 + Terminal_Return + +Function '{anonymous}#1': mixed +Block#1 + Expr_Param + declaredType: mixed + name: LITERAL('a') + result: Var#1<$a> + Expr_BinaryOp_Plus + left: Var#2<$b> + right: LITERAL(1) + result: Var#3 + Expr_Assign + var: Var#4<$b> + expr: Var#3 + result: Var#5 + Expr_BinaryOp_Mul + left: Var#4<$b> + right: Var#1<$a> + result: Var#6 + Terminal_Return + expr: Var#6 \ No newline at end of file