Skip to content

Commit 01ce411

Browse files
committed
Implement casts
1 parent 5b7346c commit 01ce411

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

src/ASTConverter/ASTConverter.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private static final function _phpparser_node_to_ast_node($n) {
106106
};
107107
}
108108
$callback = $callback_map[get_class($n)] ?? $fallback_closure;
109-
return $callback($n, $n->getAttribute('startLine'));
109+
return $callback($n, $n->getAttribute('startLine') ?: 0);
110110
}
111111

112112
/**
@@ -264,6 +264,27 @@ private static function _init_handle_map() : array {
264264
'PhpParser\Node\Expr\BooleanNot' => function(PhpParser\Node\Expr\BooleanNot $n, int $startLine) : \ast\Node {
265265
return self::_ast_node_unary_op(\ast\flags\UNARY_BOOL_NOT, self::_phpparser_node_to_ast_node($n->expr), $startLine);
266266
},
267+
'PhpParser\Node\Expr\Cast\Array_' => function(PhpParser\Node\Expr\Cast\Array_ $n, int $startLine) : \ast\Node {
268+
return self::_ast_node_cast(\ast\flags\TYPE_ARRAY, $n, $startLine);
269+
},
270+
'PhpParser\Node\Expr\Cast\Bool_' => function(PhpParser\Node\Expr\Cast\Bool_ $n, int $startLine) : \ast\Node {
271+
return self::_ast_node_cast(\ast\flags\TYPE_BOOL, $n, $startLine);
272+
},
273+
'PhpParser\Node\Expr\Cast\Double' => function(PhpParser\Node\Expr\Cast\Double $n, int $startLine) : \ast\Node {
274+
return self::_ast_node_cast(\ast\flags\TYPE_DOUBLE, $n, $startLine);
275+
},
276+
'PhpParser\Node\Expr\Cast\Int_' => function(PhpParser\Node\Expr\Cast\Int_ $n, int $startLine) : \ast\Node {
277+
return self::_ast_node_cast(\ast\flags\TYPE_LONG, $n, $startLine);
278+
},
279+
'PhpParser\Node\Expr\Cast\Object_' => function(PhpParser\Node\Expr\Cast\Object_ $n, int $startLine) : \ast\Node {
280+
return self::_ast_node_cast(\ast\flags\TYPE_OBJECT, $n, $startLine);
281+
},
282+
'PhpParser\Node\Expr\Cast\String_' => function(PhpParser\Node\Expr\Cast\String_ $n, int $startLine) : \ast\Node {
283+
return self::_ast_node_cast(\ast\flags\TYPE_STRING, $n, $startLine);
284+
},
285+
'PhpParser\Node\Expr\Cast\Unset_' => function(PhpParser\Node\Expr\Cast\Unset_ $n, int $startLine) : \ast\Node {
286+
return self::_ast_node_cast(\ast\flags\TYPE_NULL, $n, $startLine);
287+
},
267288
'PhpParser\Node\Expr\Closure' => function(PhpParser\Node\Expr\Closure $n, int $startLine) : \ast\Node {
268289
// TODO: is there a corresponding flag for $n->static? $n->byRef?
269290
return self::_ast_decl_closure(
@@ -295,9 +316,13 @@ private static function _init_handle_map() : array {
295316
);
296317
},
297318
'PhpParser\Node\Expr\Error' => function(PhpParser\Node\Expr\Error $n, int $startLine) {
319+
// This is where PhpParser couldn't parse a node.
298320
// TODO: handle this.
299321
return null;
300322
},
323+
'PhpParser\Node\Expr\Exit_' => function(PhpParser\Node\Expr\Exit_ $n, int $startLine) {
324+
return astnode(\ast\AST_EXIT, 0, ['expr' => self::_phpparser_node_to_ast_node($n->expr)], $startLine);
325+
},
301326
'PhpParser\Node\Expr\FuncCall' => function(PhpParser\Node\Expr\FuncCall $n, int $startLine) : \ast\Node {
302327
return self::_ast_node_call(
303328
self::_phpparser_node_to_ast_node($n->name),
@@ -584,6 +609,14 @@ private static function _init_handle_map() : array {
584609
self::_extract_phpdoc_comment($n->getAttribute('comments'))
585610
);
586611
},
612+
/** @return \ast\Node|\ast\Node[] */
613+
'PhpParser\Node\Stmt\Global_' => function(PhpParser\Node\Stmt\Global_ $n, int $startLine) {
614+
$globalNodes = [];
615+
foreach ($n->vars as $var) {
616+
$globalNodes[] = astnode(\ast\AST_GLOBAL, 0, ['var' => self::_phpparser_node_to_ast_node($var)], sl($var) ?: $startLine);
617+
}
618+
return \count($globalNodes) === 1 ? $globalNodes[0] : $globalNodes;
619+
},
587620
'PhpParser\Node\Stmt\If_' => function(PhpParser\Node\Stmt\If_ $n, int $startLine) : \ast\Node {
588621
return self::_phpparser_if_stmt_to_ast_if_stmt($n);
589622
},
@@ -636,6 +669,17 @@ private static function _init_handle_map() : array {
636669
'PhpParser\Node\Stmt\Return_' => function(PhpParser\Node\Stmt\Return_ $n, int $startLine) : \ast\Node {
637670
return self::_ast_stmt_return(self::_phpparser_node_to_ast_node($n->expr), $startLine);
638671
},
672+
/** @return \ast\Node|\ast\Node[] */
673+
'PhpParser\Node\Stmt\Static_' => function(PhpParser\Node\Stmt\Static_ $n, int $startLine) {
674+
$staticNodes = [];
675+
foreach ($n->vars as $var) {
676+
$staticNodes[] = astnode(\ast\AST_STATIC, 0, [
677+
'var' => astnode(\ast\AST_VAR, 0, ['name' => $var->name], sl($var) ?: $startLine),
678+
'default' => $var->default !== null ? self::_phpparser_node_to_ast_node($var->default) : null,
679+
], sl($var) ?: $startLine);
680+
}
681+
return \count($staticNodes) === 1 ? $staticNodes[0] : $staticNodes;
682+
},
639683
'PhpParser\Node\Stmt\Switch_' => function(PhpParser\Node\Stmt\Switch_ $n, int $startLine) : \ast\Node {
640684
return self::_phpparser_switch_list_to_ast_switch($n);
641685
},
@@ -784,6 +828,10 @@ private static function _ast_node_unary_op(int $flags, $expr, int $line) : \ast\
784828
return astnode(\ast\AST_UNARY_OP, $flags, ['expr' => $expr], $line);
785829
}
786830

831+
private static function _ast_node_cast(int $flags, PhpParser\Node\Expr\Cast $n, int $line) : \ast\Node {
832+
return astnode(\ast\AST_CAST, $flags, ['expr' => self::_phpparser_node_to_ast_node($n->expr)], sl($n) ?: $line);
833+
}
834+
787835
private static function _ast_node_eval($expr, int $line) : \ast\Node {
788836
return astnode(\ast\AST_INCLUDE_OR_EVAL, \ast\flags\EXEC_EVAL, ['expr' => $expr], $line);
789837
}

test_files/src/casts.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
(string)$x;
3+
(int)$x;
4+
(array)$x;
5+
(unset)$x;
6+
(object)$x;
7+
(float)$x;
8+
(bool)$x;

test_files/src/exit.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
die('message');
3+
exit(1);

test_files/src/static.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
function a() {
3+
static $a;
4+
static $b, $c;
5+
global $var1, $var2;
6+
global $var3;
7+
}

0 commit comments

Comments
 (0)