Skip to content

Commit 30d1ca0

Browse files
committed
Support more node types
1 parent 26db6e3 commit 30d1ca0

File tree

6 files changed

+80
-13
lines changed

6 files changed

+80
-13
lines changed

src/ASTConverter/ASTConverter.php

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ private static function _init_handle_map() : array {
293293
'args' => self::_phpparser_arg_list_to_ast_arg_list($n->args, $startLine),
294294
], $startLine);
295295
},
296+
'PhpParser\Node\Expr\Print_' => function(PhpParser\Node\Expr\Print_ $n, int $startLine) : \ast\Node {
297+
return astnode(
298+
\ast\AST_PRINT,
299+
0,
300+
['expr' => self::_phpparser_node_to_ast_node($n->expr)],
301+
$startLine
302+
);
303+
},
296304
'PhpParser\Node\Expr\PropertyFetch' => function(PhpParser\Node\Expr\PropertyFetch $n, int $startLine) : ?\ast\Node {
297305
return self::_phpparser_propertyfetch_to_ast_prop($n, $startLine);
298306
},
@@ -419,6 +427,29 @@ private static function _init_handle_map() : array {
419427
}
420428
return count($astEchos) === 1 ? $astEchos[0] : $astEchos;
421429
},
430+
'PhpParser\Node\Stmt\Foreach_' => function(PhpParser\Node\Stmt\Foreach_ $n, int $startLine) : \ast\Node {
431+
$value = self::_phpparser_node_to_ast_node($n->valueVar);
432+
if ($n->byRef) {
433+
$value = astnode(
434+
\ast\AST_REF,
435+
0,
436+
['var' => $value],
437+
$value->lineno ?? $startLine
438+
);
439+
}
440+
return astnode(
441+
\ast\AST_FOREACH,
442+
0,
443+
[
444+
'expr' => self::_phpparser_node_to_ast_node($n->expr),
445+
'value' => $value,
446+
'key' => $n->keyVar !== null ? self::_phpparser_node_to_ast_node($n->keyVar) : null,
447+
'stmts' => self::_phpparser_stmtlist_to_ast_node($n->stmts, $startLine),
448+
],
449+
$startLine
450+
);
451+
//return self::_phpparser_stmtlist_to_ast_node($n->stmts, $startLine);
452+
},
422453
'PhpParser\Node\Stmt\Finally_' => function(PhpParser\Node\Stmt\Finally_ $n, int $startLine) : \ast\Node {
423454
return self::_phpparser_stmtlist_to_ast_node($n->stmts, $startLine);
424455
},
@@ -443,6 +474,9 @@ private static function _init_handle_map() : array {
443474
'PhpParser\Node\Stmt\If_' => function(PhpParser\Node\Stmt\If_ $n, int $startLine) : \ast\Node {
444475
return self::_phpparser_if_stmt_to_ast_if_stmt($n);
445476
},
477+
'PhpParser\Node\Stmt\InlineHTML' => function(PhpParser\Node\Stmt\InlineHTML $n, int $startLine) : \ast\Node {
478+
return self::_ast_stmt_echo($n->value, $startLine);
479+
},
446480
'PhpParser\Node\Stmt\Interface_' => function(PhpParser\Node\Stmt\Interface_ $n, int $startLine) : \ast\Node {
447481
$endLine = $n->getAttribute('endLine') ?: $startLine;
448482
return self::_ast_stmt_class(
@@ -774,8 +808,13 @@ private static function _phpparser_params_to_ast_params(array $parserParams, int
774808
* @suppress PhanTypeMismatchProperty - Deliberately wrong type of kind
775809
*/
776810
private static function _ast_stub($parserNode) : \ast\Node{
811+
// Debugging code.
812+
if (getenv('AST_THROW_INVALID')) {
813+
throw new \Error("TODO:" . get_class($parserNode));
814+
}
815+
777816
$node = new \ast\Node();
778-
$node->kind = "TODO:" . get_class($parserNode) . json_encode(array_keys((array)$parserNode));
817+
$node->kind = "TODO:" . get_class($parserNode);
779818
$node->flags = 0;
780819
$node->lineno = $parserNode->getAttribute('startLine');
781820
$node->children = null;
@@ -1083,18 +1122,26 @@ private static function _phpparser_constelem_to_ast_constelem(PhpParser\Node\Con
10831122
return astnode(\ast\AST_CONST_ELEM, 0, $children, $startLine, self::_extract_phpdoc_comment($n->getAttribute('comments') ?? $docComment));
10841123
}
10851124
private static function _phpparser_visibility_to_ast_visibility(int $visibility) : int {
1086-
switch($visibility) {
1087-
case \PHPParser\Node\Stmt\Class_::MODIFIER_PUBLIC:
1088-
return \ast\flags\MODIFIER_PUBLIC;
1089-
case \PHPParser\Node\Stmt\Class_::MODIFIER_PROTECTED:
1090-
return \ast\flags\MODIFIER_PROTECTED;
1091-
case \PHPParser\Node\Stmt\Class_::MODIFIER_PRIVATE:
1092-
return \ast\flags\MODIFIER_PRIVATE;
1093-
case 0:
1094-
return \ast\flags\MODIFIER_PUBLIC; // FIXME?
1095-
default:
1096-
throw new \Error("Invalid phpparser visibility " . $visibility);
1125+
$ast_visibility = 0;
1126+
if ($visibility & \PHPParser\Node\Stmt\Class_::MODIFIER_PUBLIC) {
1127+
$ast_visibility |= \ast\flags\MODIFIER_PUBLIC;
1128+
} else if ($visibility & \PHPParser\Node\Stmt\Class_::MODIFIER_PROTECTED) {
1129+
$ast_visibility |= \ast\flags\MODIFIER_PROTECTED;
1130+
} else if ($visibility & \PHPParser\Node\Stmt\Class_::MODIFIER_PRIVATE) {
1131+
$ast_visibility |= \ast\flags\MODIFIER_PRIVATE;
1132+
} else {
1133+
$ast_visibility |= \ast\flags\MODIFIER_PUBLIC;
1134+
}
1135+
if ($visibility & \PHPParser\Node\Stmt\Class_::MODIFIER_STATIC) {
1136+
$ast_visibility |= \ast\flags\MODIFIER_STATIC;
1137+
}
1138+
if ($visibility & \PHPParser\Node\Stmt\Class_::MODIFIER_ABSTRACT) {
1139+
$ast_visibility |= \ast\flags\MODIFIER_ABSTRACT;
1140+
}
1141+
if ($visibility & \PHPParser\Node\Stmt\Class_::MODIFIER_FINAL) {
1142+
$ast_visibility |= \ast\flags\MODIFIER_FINAL;
10971143
}
1144+
return $ast_visibility;
10981145
}
10991146

11001147
private static function _phpparser_property_to_ast_node(PhpParser\Node $n, int $startLine) : \ast\Node {

test

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
#!/bin/sh
2-
vendor/bin/phpunit tests
2+
if php -m | grep '^ast'; then
3+
vendor/bin/phpunit tests
4+
else
5+
php -d extension=ast.so vendor/bin/phpunit tests
6+
fi
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$a = [1,2,3];
4+
5+
foreach ($a as $b) {
6+
print $b;
7+
}
8+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
foreach ([] as list(, $a));

test_files/src/foreachbyref.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
$x = [2];
3+
foreach ($x as $key => &$a) {}

test_files/src/script.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env php
2+
<?php require_once __DIR__ . '/src/phan.php';

0 commit comments

Comments
 (0)