Skip to content

Commit 41d9576

Browse files
committed
Implement more node types
1 parent 01ce411 commit 41d9576

File tree

5 files changed

+133
-2
lines changed

5 files changed

+133
-2
lines changed

src/ASTConverter/ASTConverter.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,17 @@ private static function _init_handle_map() : array {
466466
$startLine
467467
);
468468
},
469+
'PhpParser\Node\Scalar\Encapsed' => function(PhpParser\Node\Scalar\Encapsed $n, int $startLine) : \ast\Node {
470+
return astnode(
471+
\ast\AST_ENCAPS_LIST,
472+
0,
473+
array_map(function(PhpParser\Node $n) { return self::_phpparser_node_to_ast_node($n); }, $n->parts),
474+
$startLine
475+
);
476+
},
477+
'PhpParser\Node\Scalar\EncapsedStringPart' => function(PhpParser\Node\Scalar\EncapsedStringPart $n, int $startLine) : string {
478+
return $n->value;
479+
},
469480
'PhpParser\Node\Scalar\LNumber' => function(PhpParser\Node\Scalar\LNumber $n, int $startLine) : int {
470481
return (int)$n->value;
471482
},
@@ -501,7 +512,7 @@ private static function _init_handle_map() : array {
501512
},
502513
'PhpParser\Node\Stmt\Catch_' => function(PhpParser\Node\Stmt\Catch_ $n, int $startLine) : \ast\Node {
503514
return self::_ast_stmt_catch(
504-
self::_phpparser_catch_types_to_ast_catch_types($n->types, $startLine),
515+
self::_phpparser_name_list_to_ast_name_list($n->types, $startLine),
505516
$n->var,
506517
self::_phpparser_stmtlist_to_ast_node($n->stmts, $startLine),
507518
$startLine
@@ -663,6 +674,10 @@ private static function _init_handle_map() : array {
663674
$startLine
664675
);
665676
},
677+
'PhpParser\Node\Stmt\Nop' => function(PhpParser\Node\Stmt\Nop $n, int $startLine) : array {
678+
// `;;`
679+
return [];
680+
},
666681
'PhpParser\Node\Stmt\Property' => function(PhpParser\Node\Stmt\Property $n, int $startLine) : \ast\Node {
667682
return self::_phpparser_property_to_ast_node($n, $startLine);
668683
},
@@ -704,6 +719,36 @@ private static function _init_handle_map() : array {
704719
self::_extract_phpdoc_comment($n->getAttribute('comments'))
705720
);
706721
},
722+
'PhpParser\Node\Stmt\TraitUse' => function(PhpParser\Node\Stmt\TraitUse $n, int $startLine) : \ast\Node {
723+
if (\is_array($n->adaptations) && \count($n->adaptations) > 0) {
724+
$adaptations_inner = array_map(function(PhpParser\Node\Stmt\TraitUseAdaptation $n) : \ast\Node {
725+
return self::_phpparser_node_to_ast_node($n);
726+
}, $n->adaptations);
727+
$adaptations = astnode(\ast\AST_TRAIT_ADAPTATIONS, 0, $adaptations_inner, $adaptations_inner[0]->lineno ?: $startLine);
728+
} else {
729+
$adaptations = null;
730+
}
731+
return astnode(
732+
\ast\AST_USE_TRAIT,
733+
0,
734+
[
735+
'traits' => self::_phpparser_name_list_to_ast_name_list($n->traits, $startLine),
736+
'adaptations' => $adaptations,
737+
],
738+
$startLine
739+
);
740+
},
741+
'PhpParser\Node\Stmt\TraitUseAdaptation\Alias' => function(PhpParser\Node\Stmt\TraitUseAdaptation\Alias $n, int $startLine) : \ast\Node {
742+
$old_class = $n->trait !== null ? self::_phpparser_name_to_string($n->trait) : null;
743+
// TODO: flags for visibility
744+
return astnode(\ast\AST_TRAIT_ALIAS, self::_phpparser_visibility_to_ast_visibility($n->newModifier ?? 0), [
745+
'method' => astnode(\ast\AST_METHOD_REFERENCE, 0, [
746+
'class' => $old_class,
747+
'method' => $n->method,
748+
], $startLine),
749+
'alias' => $n->newName,
750+
], $startLine);
751+
},
707752
'PhpParser\Node\Stmt\TryCatch' => function(PhpParser\Node\Stmt\TryCatch $n, int $startLine) : \ast\Node {
708753
if (!is_array($n->catches)) {
709754
throw new \Error(sprintf("Unsupported type %s\n%s", get_class($n), var_export($n->catches, true)));
@@ -785,7 +830,7 @@ private static function _phpparser_catchlist_to_ast_catchlist(array $catches, in
785830
return $node;
786831
}
787832

788-
private static function _phpparser_catch_types_to_ast_catch_types(array $types, int $line) : \ast\Node {
833+
private static function _phpparser_name_list_to_ast_name_list(array $types, int $line) : \ast\Node {
789834
$astTypes = [];
790835
foreach ($types as $type) {
791836
$astTypes[] = self::_phpparser_node_to_ast_node($type);

test_files/src/0009_traits.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
trait A {
4+
}
5+
6+
trait B {
7+
}
8+
9+
class C {
10+
use \Foo\A;
11+
use B;
12+
}
13+
14+
class D {
15+
use A;
16+
}
17+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
class A {
4+
public static $alpha = 42;
5+
public $beta = 'string';
6+
const FOURTY_TWO = 42;
7+
}
8+
9+
class B extends A {
10+
public static $gamma = parent::$alpha; // FIXME: PHP Fatal error: Constant expression contains invalid operations, but this test expects no Issues.
11+
public $delta = parent::$beta; // Note: This is not a valid way to fetch a parent instance property. Emit an issue here as well.
12+
public $epsilon = parent::FOURTY_TWO;
13+
}
14+
// FIXME: this test should access parent properties within functions. Property declaration defaults expect constant expressions.

test_files/src/0026_dim_types.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
class A extends ArrayObject {}
5+
6+
$a = new A;
7+
$a[] = 5;
8+
$x = $a[0];
9+
10+
11+
$b = [1, 2, 3];
12+
$x = $b[1];
13+
print "$x\n";
14+
15+
16+
$c = 'string';
17+
$x = $c[2];
18+
print "$x\n";
19+
*/
20+
21+
class B {
22+
function f(int $a) : int {
23+
return $a;
24+
}
25+
26+
// @return int[]
27+
function g() {
28+
return [1, 2, 3];
29+
}
30+
31+
/**
32+
* @return string[]
33+
*/
34+
function h() {
35+
return ['a', 'b', 'c'];
36+
}
37+
}
38+
39+
$e = new B;
40+
/*
41+
$x = $e->f($e->g()[1]);
42+
print "$x\n";
43+
*/
44+
45+
// Should be of type string
46+
$f = $e->h()[2];
47+
$x = $e->f($f);
48+
print "$x\n";

test_files/src/use.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
class C {
4+
use T{
5+
f1 as private f2;
6+
}
7+
}

0 commit comments

Comments
 (0)