@@ -173,6 +173,11 @@ public function parseCodeAsPHPAST(string $file_contents, int $version, array &$e
173
173
*/
174
174
public static function phpParserParse (string $ file_contents , array &$ errors = null ) : PhpParser \Node
175
175
{
176
+ if (PHP_VERSION_ID >= 70300 ) {
177
+ // TODO: Remove after upgrading to tolerant-php-parser 0.0.13 (https://github.com/Microsoft/tolerant-php-parser/pull/250)
178
+ @class_exists (\Microsoft \PhpParser \PhpTokenizer::class);
179
+ @class_exists (\Microsoft \PhpParser \Parser::class);
180
+ }
176
181
$ parser = new Parser (); // TODO: In php 7.3, we might need to provide a version, due to small changes in lexing?
177
182
$ result = $ parser ->parseSourceFile ($ file_contents );
178
183
$ errors = DiagnosticsProvider::getDiagnostics ($ result );
@@ -270,7 +275,7 @@ private static function phpParserStmtlistToAstNode($parser_nodes, $lineno, bool
270
275
foreach ($ parser_nodes as $ parser_node ) {
271
276
try {
272
277
$ child_node = static ::phpParserNodeToAstNode ($ parser_node );
273
- } catch (InvalidNodeException $ e ) {
278
+ } catch (InvalidNodeException $ _ ) {
274
279
continue ;
275
280
}
276
281
if (\is_array ($ child_node )) {
@@ -372,7 +377,7 @@ protected static function phpParserNodeToAstNodeOrPlaceholderExpr($n)
372
377
373
378
/**
374
379
* @param PhpParser\Node|Token $n - The node from PHP-Parser
375
- * @return ast\Node|ast\Node[]|string|int|float|bool| null - whatever ast\parse_code would return as the equivalent.
380
+ * @return ast\Node|ast\Node[]|string|int|float|null - whatever ast\parse_code would return as the equivalent.
376
381
*/
377
382
protected static function phpParserNodeToAstNode ($ n )
378
383
{
@@ -461,7 +466,7 @@ protected static function initHandleMap() : array
461
466
'Microsoft\PhpParser\Node\Expression\AssignmentExpression ' => function (PhpParser \Node \Expression \AssignmentExpression $ n , int $ start_line ) {
462
467
try {
463
468
$ var_node = static ::phpParserNodeToAstNode ($ n ->leftOperand );
464
- } catch (InvalidNodeException $ e ) {
469
+ } catch (InvalidNodeException $ _ ) {
465
470
if (self ::$ should_add_placeholders ) {
466
471
$ var_node = new ast \Node (ast \AST_VAR , 0 , ['name ' => '__INCOMPLETE_VARIABLE__ ' ], $ start_line );
467
472
} else {
@@ -886,13 +891,14 @@ protected static function initHandleMap() : array
886
891
},
887
892
'Microsoft\PhpParser\Node\Parameter ' => function (PhpParser \Node \Parameter $ n , int $ start_line ) : ast \Node {
888
893
$ type_line = static ::getEndLine ($ n ->typeDeclaration ) ?: $ start_line ;
894
+ $ default_node = $ n ->default !== null ? static ::phpParserNodeToAstNode ($ n ->default ) : null ;
889
895
return static ::astNodeParam (
890
896
$ n ->questionToken !== null ,
891
897
$ n ->byRefToken !== null ,
892
898
$ n ->dotDotDotToken !== null ,
893
899
static ::phpParserTypeToAstNode ($ n ->typeDeclaration , $ type_line ),
894
900
static ::variableTokenToString ($ n ->variableName ),
895
- $ n -> default !== null ? static :: phpParserNodeToAstNode ( $ n -> default ) : null ,
901
+ $ default_node ,
896
902
$ start_line
897
903
);
898
904
},
@@ -932,7 +938,7 @@ protected static function initHandleMap() : array
932
938
$ raw_string = static ::tokenToRawString ($ part );
933
939
934
940
// Pass in '"\\n"' and get "\n" (somewhat inefficient)
935
- $ represented_string = String_ ::parse ($ start_quote_text . $ raw_string . $ end_quote_text );
941
+ $ represented_string = StringUtil ::parse ($ start_quote_text . $ raw_string . $ end_quote_text );
936
942
$ inner_node_parts [] = $ represented_string ;
937
943
}
938
944
}
@@ -976,14 +982,14 @@ protected static function initHandleMap() : array
976
982
'Microsoft\PhpParser\Node\Statement\BreakOrContinueStatement ' => function (PhpParser \Node \Statement \BreakOrContinueStatement $ n , int $ start_line ) : ast \Node {
977
983
$ kind = $ n ->breakOrContinueKeyword ->kind === TokenKind::ContinueKeyword ? ast \AST_CONTINUE : ast \AST_BREAK ;
978
984
if ($ n ->breakoutLevel !== null ) {
979
- $ breakoutLevel = static ::phpParserNodeToAstNode ($ n ->breakoutLevel );
980
- if (!\is_int ($ breakoutLevel )) {
981
- $ breakoutLevel = null ;
985
+ $ breakout_level = static ::phpParserNodeToAstNode ($ n ->breakoutLevel );
986
+ if (!\is_int ($ breakout_level )) {
987
+ $ breakout_level = null ;
982
988
}
983
989
} else {
984
- $ breakoutLevel = null ;
990
+ $ breakout_level = null ;
985
991
}
986
- return new ast \Node ($ kind , 0 , ['depth ' => $ breakoutLevel ], $ start_line );
992
+ return new ast \Node ($ kind , 0 , ['depth ' => $ breakout_level ], $ start_line );
987
993
},
988
994
'Microsoft\PhpParser\Node\CatchClause ' => function (PhpParser \Node \CatchClause $ n , int $ start_line ) : ast \Node {
989
995
$ qualified_name = $ n ->qualifiedName ;
@@ -1054,12 +1060,22 @@ protected static function initHandleMap() : array
1054
1060
// This node type is generated for something that isn't a function/constant/property. e.g. "public example();"
1055
1061
return null ;
1056
1062
},
1063
+ /**
1064
+ * @throws InvalidNodeException
1065
+ */
1057
1066
'Microsoft\PhpParser\Node\MethodDeclaration ' => function (PhpParser \Node \MethodDeclaration $ n , int $ start_line ) : ast \Node {
1058
1067
$ statements = $ n ->compoundStatementOrSemicolon ;
1059
1068
$ ast_return_type = static ::phpParserTypeToAstNode ($ n ->returnType , static ::getEndLine ($ n ->returnType ) ?: $ start_line );
1060
1069
if (($ ast_return_type ->children ['name ' ] ?? null ) === '' ) {
1061
1070
$ ast_return_type = null ;
1062
1071
}
1072
+ $ original_method_name = $ n ->name ;
1073
+ if (($ original_method_name ->kind ?? null ) === TokenKind::Name) {
1074
+ $ method_name = static ::tokenToString ($ original_method_name );
1075
+ } else {
1076
+ $ method_name = 'placeholder_ ' . $ original_method_name ->fullStart ;
1077
+ }
1078
+
1063
1079
if ($ n ->questionToken !== null && $ ast_return_type !== null ) {
1064
1080
$ ast_return_type = new ast \Node (ast \AST_NULLABLE_TYPE , 0 , ['type ' => $ ast_return_type ], $ start_line );
1065
1081
}
@@ -1074,7 +1090,7 @@ protected static function initHandleMap() : array
1074
1090
],
1075
1091
$ start_line ,
1076
1092
$ n ->getDocCommentText (),
1077
- static :: variableTokenToString ( $ n -> name ) ,
1093
+ $ method_name ,
1078
1094
static ::getEndLine ($ n ),
1079
1095
self ::nextDeclId ()
1080
1096
);
@@ -2157,7 +2173,7 @@ private static function astNodeBinaryop(int $flags, PhpParser\Node\Expression\Bi
2157
2173
{
2158
2174
try {
2159
2175
$ left_node = static ::phpParserNodeToAstNode ($ n ->leftOperand );
2160
- } catch (InvalidNodeException $ e ) {
2176
+ } catch (InvalidNodeException $ _ ) {
2161
2177
if (self ::$ should_add_placeholders ) {
2162
2178
$ left_node = static ::newPlaceholderExpression ($ n ->leftOperand );
2163
2179
} else {
@@ -2167,7 +2183,7 @@ private static function astNodeBinaryop(int $flags, PhpParser\Node\Expression\Bi
2167
2183
}
2168
2184
try {
2169
2185
$ right_node = static ::phpParserNodeToAstNode ($ n ->rightOperand );
2170
- } catch (InvalidNodeException $ e ) {
2186
+ } catch (InvalidNodeException $ _ ) {
2171
2187
if (self ::$ should_add_placeholders ) {
2172
2188
$ right_node = static ::newPlaceholderExpression ($ n ->rightOperand );
2173
2189
} else {
@@ -2192,7 +2208,7 @@ private static function astNodeAssignop(int $flags, PhpParser\Node\Expression\Bi
2192
2208
{
2193
2209
try {
2194
2210
$ var_node = static ::phpParserNodeToAstNode ($ n ->leftOperand );
2195
- } catch (InvalidNodeException $ e ) {
2211
+ } catch (InvalidNodeException $ _ ) {
2196
2212
if (self ::$ should_add_placeholders ) {
2197
2213
$ var_node = new ast \Node (ast \AST_VAR , 0 , ['name ' => '__INCOMPLETE_VARIABLE__ ' ], $ start_line );
2198
2214
} else {
@@ -2546,14 +2562,14 @@ private static function tokenToScalar(Token $n)
2546
2562
return $ float ;
2547
2563
}
2548
2564
2549
- return String_ ::parse ($ str );
2565
+ return StringUtil ::parse ($ str );
2550
2566
}
2551
2567
2552
2568
private static function parseQuotedString (PhpParser \Node \StringLiteral $ n ) : string
2553
2569
{
2554
2570
$ start = $ n ->getStart ();
2555
2571
$ text = \substr (self ::$ file_contents , $ start , $ n ->getEndPosition () - $ start );
2556
- return String_ ::parse ($ text );
2572
+ return StringUtil ::parse ($ text );
2557
2573
}
2558
2574
2559
2575
/**
@@ -2615,10 +2631,10 @@ private static function phpParserClassconstfetchToAstClassconstfetch($scope_reso
2615
2631
*/
2616
2632
private static function phpParserNameToString (PhpParser \Node \QualifiedName $ name ) : string
2617
2633
{
2618
- $ nameParts = $ name ->nameParts ;
2634
+ $ name_parts = $ name ->nameParts ;
2619
2635
// TODO: Handle error case (can there be missing parts?)
2620
2636
$ result = '' ;
2621
- foreach ($ nameParts as $ part ) {
2637
+ foreach ($ name_parts as $ part ) {
2622
2638
$ part_as_string = static ::tokenToString ($ part );
2623
2639
if ($ part_as_string !== '' ) {
2624
2640
$ result .= \trim ($ part_as_string );
@@ -2639,14 +2655,14 @@ private static function phpParserNameToString(PhpParser\Node\QualifiedName $name
2639
2655
*/
2640
2656
private static function newAstDecl (int $ kind , int $ flags , array $ children , int $ lineno , string $ doc_comment = null , string $ name = null , int $ end_lineno = 0 , int $ decl_id = -1 ) : ast \Node
2641
2657
{
2642
- $ children50 = [];
2643
- $ children50 ['name ' ] = $ name ;
2644
- $ children50 ['docComment ' ] = $ doc_comment ;
2645
- $ children50 += $ children ;
2658
+ $ decl_children = [];
2659
+ $ decl_children ['name ' ] = $ name ;
2660
+ $ decl_children ['docComment ' ] = $ doc_comment ;
2661
+ $ decl_children += $ children ;
2646
2662
if ($ decl_id >= 0 ) {
2647
- $ children50 ['__declId ' ] = $ decl_id ;
2663
+ $ decl_children ['__declId ' ] = $ decl_id ;
2648
2664
}
2649
- $ node = new ast \Node ($ kind , $ flags , $ children50 , $ lineno );
2665
+ $ node = new ast \Node ($ kind , $ flags , $ decl_children , $ lineno );
2650
2666
if (\is_int ($ end_lineno )) {
2651
2667
$ node ->endLineno = $ end_lineno ;
2652
2668
}
0 commit comments