Skip to content

Commit 9ca2eca

Browse files
committed
Parse __halt_compiler() (but don't compute offset yet)
1 parent e0e4ab7 commit 9ca2eca

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

src/ASTConverter/ASTConverter.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ private static function _init_handle_map() : array {
416416
return null;
417417
},
418418
'PhpParser\Node\Expr\Exit_' => function(PhpParser\Node\Expr\Exit_ $n, int $startLine) {
419-
return new ast\Node(ast\AST_EXIT, 0, ['expr' => self::_phpparser_node_to_ast_node($n->expr)], $startLine);
419+
return new ast\Node(ast\AST_EXIT, 0, ['expr' => $n->expr ? self::_phpparser_node_to_ast_node($n->expr) : null], $startLine);
420420
},
421421
'PhpParser\Node\Expr\FuncCall' => function(PhpParser\Node\Expr\FuncCall $n, int $startLine) : ast\Node {
422422
return self::_ast_node_call(
@@ -782,6 +782,14 @@ private static function _init_handle_map() : array {
782782
}
783783
return \count($globalNodes) === 1 ? $globalNodes[0] : $globalNodes;
784784
},
785+
'PhpParser\Node\Stmt\HaltCompiler' => function(PhpParser\Node\Stmt\HaltCompiler $n, int $startLine) : ast\Node {
786+
return new ast\Node(
787+
\ast\AST_HALT_COMPILER,
788+
0,
789+
['offset' => 'TODO compute halt compiler offset'], // FIXME implement
790+
$startLine
791+
);
792+
},
785793
'PhpParser\Node\Stmt\If_' => function(PhpParser\Node\Stmt\If_ $n, int $startLine) : ast\Node {
786794
return self::_phpparser_if_stmt_to_ast_if_stmt($n);
787795
},
@@ -1809,7 +1817,8 @@ private static function _phpparser_list_to_ast_list(PhpParser\Node\Expr\List_ $n
18091817
if ($item === null) {
18101818
$astItems[] = null;
18111819
} else {
1812-
$astItems[] = new ast\Node(ast\AST_ARRAY_ELEM, 0, [
1820+
$flags = $item->byRef ? \ast\flags\PARAM_REF : 0;
1821+
$astItems[] = new ast\Node(ast\AST_ARRAY_ELEM, $flags, [
18131822
'value' => self::_phpparser_node_to_ast_node($item->value),
18141823
'key' => $item->key !== null ? self::_phpparser_node_to_ast_node($item->key) : null,
18151824
], $item->getAttribute('startLine'));
@@ -1824,7 +1833,8 @@ private static function _phpparser_array_to_ast_array(PhpParser\Node\Expr\Array_
18241833
if ($item === null) {
18251834
$astItems[] = null;
18261835
} else {
1827-
$astItems[] = new ast\Node(ast\AST_ARRAY_ELEM, 0, [
1836+
$flags = $item->byRef ? \ast\flags\PARAM_REF : 0;
1837+
$astItems[] = new ast\Node(ast\AST_ARRAY_ELEM, $flags, [
18281838
'value' => self::_phpparser_node_to_ast_node($item->value),
18291839
'key' => $item->key !== null ? self::_phpparser_node_to_ast_node($item->key) : null,
18301840
], $item->getAttribute('startLine'));

test_files/src/arrayelementref.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$y = 2;
4+
$z = 3;
5+
$x = [&$y, $z];
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
ob_start();
4+
5+
function open($save_path, $session_name) {
6+
return true;
7+
}
8+
9+
function close() {
10+
echo "close: goodbye cruel world\n";
11+
exit;
12+
}
13+
14+
function read($id) {
15+
return '';
16+
}
17+
18+
function write($id, $session_data) {
19+
echo "write: goodbye cruel world\n";
20+
undefined_function();
21+
}
22+
23+
function destroy($id) {
24+
return true;
25+
}
26+
27+
function gc($maxlifetime) {
28+
return true;
29+
}
30+
31+
session_set_save_handler('open', 'close', 'read', 'write', 'destroy', 'gc');
32+
session_start();
33+
34+
?>

tests/ASTConverter/ConversionTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,13 @@ public function testFallbackFromParser(string $fileName, int $astVersion) {
102102
$ast = ast\parse_code($contents, $astVersion, $fileName);
103103
self::normalizeOriginalAST($ast);
104104
$this->assertInstanceOf('\ast\Node', $ast, 'Examples must be syntactically valid PHP parseable by php-ast');
105-
$fallback_ast = \ASTConverter\ASTConverter::ast_parse_code_fallback($contents, $astVersion);
105+
try {
106+
$fallback_ast = \ASTConverter\ASTConverter::ast_parse_code_fallback($contents, $astVersion);
107+
} catch (\Throwable $e) {
108+
throw new \RuntimeException("Error parsing $fileName with ast version $astVersion", $e->getCode(), $e);
109+
}
106110
$this->assertInstanceOf('\ast\Node', $fallback_ast, 'The fallback must also return a tree of php-ast nodes');
107-
if (stripos($fileName, 'phan_test_files') !== false) {
111+
if (stripos($fileName, 'phan_test_files') !== false || stripos($fileName, 'php-src_tests') !== false) {
108112
$fallback_ast = self::normalizeLineNumbers($fallback_ast);
109113
$ast = self::normalizeLineNumbers($ast);
110114
}

0 commit comments

Comments
 (0)