@@ -166,7 +166,11 @@ private function startParsing(int $ast_version) {
166
166
self ::$ should_add_placeholders = $ this ->instance_should_add_placeholders ;
167
167
}
168
168
169
- private static function phpParserStmtlistToAstNode (array $ parser_nodes , ?int $ lineno ) : ast \Node {
169
+ /**
170
+ * @param array $parser_nodes
171
+ * @param ?int $lineno
172
+ */
173
+ private static function phpParserStmtlistToAstNode (array $ parser_nodes , $ lineno ) : ast \Node {
170
174
$ stmts = new ast \Node ();
171
175
$ stmts ->kind = ast \AST_STMT_LIST ;
172
176
$ stmts ->flags = 0 ;
@@ -307,15 +311,17 @@ private static function initHandleMap() : array {
307
311
'dim ' => $ n ->dim !== null ? self ::phpParserNodeToAstNode ($ n ->dim ) : null ,
308
312
], $ start_line );
309
313
},
310
- 'PhpParser\Node\Expr\Assign ' => function (PhpParser \Node \Expr \Assign $ n , int $ start_line ) : ?ast \Node {
314
+ /** @return ?ast\Node */
315
+ 'PhpParser\Node\Expr\Assign ' => function (PhpParser \Node \Expr \Assign $ n , int $ start_line ) {
311
316
return self ::astNodeAssign (
312
317
self ::phpParserNodeToAstNode ($ n ->var ),
313
318
self ::phpParserNodeToAstNode ($ n ->expr ),
314
319
$ start_line ,
315
320
false
316
321
);
317
322
},
318
- 'PhpParser\Node\Expr\AssignRef ' => function (PhpParser \Node \Expr \AssignRef $ n , int $ start_line ) : ?ast \Node {
323
+ /** @return ?ast\Node */
324
+ 'PhpParser\Node\Expr\AssignRef ' => function (PhpParser \Node \Expr \AssignRef $ n , int $ start_line ) {
319
325
return self ::astNodeAssign (
320
326
self ::phpParserNodeToAstNode ($ n ->var ),
321
327
self ::phpParserNodeToAstNode ($ n ->expr ),
@@ -485,7 +491,8 @@ private static function initHandleMap() : array {
485
491
);
486
492
// FIXME: add a test of ClassConstFetch to php-ast
487
493
},
488
- 'PhpParser\Node\Expr\ClassConstFetch ' => function (PhpParser \Node \Expr \ClassConstFetch $ n , int $ start_line ) : ?ast \Node {
494
+ /** @return ?ast\Node */
495
+ 'PhpParser\Node\Expr\ClassConstFetch ' => function (PhpParser \Node \Expr \ClassConstFetch $ n , int $ start_line ) {
489
496
return self ::phpParserClassconstfetchToAstClassconstfetch ($ n , $ start_line );
490
497
},
491
498
'PhpParser\Node\Expr\Clone_ ' => function (PhpParser \Node \Expr \Clone_ $ n , int $ start_line ) : ast \Node {
@@ -593,7 +600,8 @@ private static function initHandleMap() : array {
593
600
$ start_line
594
601
);
595
602
},
596
- 'PhpParser\Node\Expr\PropertyFetch ' => function (PhpParser \Node \Expr \PropertyFetch $ n , int $ start_line ) : ?ast \Node {
603
+ /** @return ?ast\Node */
604
+ 'PhpParser\Node\Expr\PropertyFetch ' => function (PhpParser \Node \Expr \PropertyFetch $ n , int $ start_line ) {
597
605
return self ::phpParserPropertyfetchToAstProp ($ n , $ start_line );
598
606
},
599
607
'PhpParser\Node\Expr\ShellExec ' => function (PhpParser \Node \Expr \ShellExec $ n , int $ start_line ) : ast \Node {
@@ -643,7 +651,8 @@ private static function initHandleMap() : array {
643
651
'PhpParser\Node\Expr\UnaryPlus ' => function (PhpParser \Node \Expr \UnaryPlus $ n , int $ start_line ) : ast \Node {
644
652
return self ::astNodeUnaryOp (ast \flags \UNARY_PLUS , self ::phpParserNodeToAstNode ($ n ->expr ), $ start_line );
645
653
},
646
- 'PhpParser\Node\Expr\Variable ' => function (PhpParser \Node \Expr \Variable $ n , int $ start_line ) : ?ast \Node {
654
+ /** @return ?ast\Node */
655
+ 'PhpParser\Node\Expr\Variable ' => function (PhpParser \Node \Expr \Variable $ n , int $ start_line ) {
647
656
return self ::astNodeVariable ($ n ->name , $ start_line );
648
657
},
649
658
'PhpParser\Node\Expr\Yield_ ' => function (PhpParser \Node \Expr \Yield_ $ n , int $ start_line ) : ast \Node {
@@ -1199,7 +1208,8 @@ private static function astNodeDoWhile($cond, $stmts, int $start_line) : ast\Nod
1199
1208
);
1200
1209
}
1201
1210
1202
- private static function astNodeAssign ($ var , $ expr , int $ line , bool $ ref ) : ?ast \Node {
1211
+ /** @return ?ast\Node */
1212
+ private static function astNodeAssign ($ var , $ expr , int $ line , bool $ ref ) {
1203
1213
if ($ expr === null ) {
1204
1214
if (self ::$ should_add_placeholders ) {
1205
1215
$ expr = '__INCOMPLETE_EXPR__ ' ;
@@ -1348,7 +1358,8 @@ private static function astNodeNullableType(ast\Node $type, int $line) {
1348
1358
return $ node ;
1349
1359
}
1350
1360
1351
- private static function astNodeVariable ($ expr , int $ line ) : ?ast \Node {
1361
+ /** @return ?ast\Node */
1362
+ private static function astNodeVariable ($ expr , int $ line ) {
1352
1363
// TODO: 2 different ways to handle an Error. 1. Add a placeholder. 2. remove all of the statements in that tree.
1353
1364
if ($ expr instanceof PhpParser \Node) {
1354
1365
$ expr = self ::phpParserNodeToAstNode ($ expr );
@@ -1422,6 +1433,9 @@ private static function phpParserClosureUsesToAstClosureUses(
1422
1433
1423
1434
}
1424
1435
1436
+ /**
1437
+ * @param ?string $doc_comment
1438
+ */
1425
1439
private static function astDeclClosure (
1426
1440
bool $ by_ref ,
1427
1441
bool $ static ,
@@ -1431,7 +1445,7 @@ private static function astDeclClosure(
1431
1445
$ return_type ,
1432
1446
int $ start_line ,
1433
1447
int $ end_line ,
1434
- ? string $ doc_comment
1448
+ $ doc_comment
1435
1449
) : ast \Node {
1436
1450
$ flags = 0 ;
1437
1451
if ($ by_ref ) {
@@ -1473,7 +1487,10 @@ private static function astDeclClosure(
1473
1487
ast \AST_YIELD_FROM => true ,
1474
1488
];
1475
1489
1476
- public static function functionBodyIsGenerator (?ast \Node $ stmts ) : bool {
1490
+ /**
1491
+ * @param ?ast\Node $stmts
1492
+ */
1493
+ public static function functionBodyIsGenerator ($ stmts ) : bool {
1477
1494
if (!$ stmts ) {
1478
1495
return false ;
1479
1496
}
@@ -1492,16 +1509,20 @@ public static function functionBodyIsGenerator(?ast\Node $stmts) : bool {
1492
1509
return false ;
1493
1510
}
1494
1511
1512
+ /**
1513
+ * @param ?array $uses
1514
+ * @param ?string $doc_comment
1515
+ */
1495
1516
private static function astDeclFunction (
1496
1517
bool $ by_ref ,
1497
1518
string $ name ,
1498
1519
ast \Node $ params ,
1499
- ? array $ uses ,
1520
+ $ uses ,
1500
1521
$ return_type ,
1501
1522
$ stmts ,
1502
1523
int $ line ,
1503
1524
int $ end_line ,
1504
- ? string $ doc_comment
1525
+ $ doc_comment
1505
1526
) : ast \Node {
1506
1527
$ flags = 0 ;
1507
1528
if ($ by_ref ) {
@@ -1548,16 +1569,17 @@ private static function phpParserClassFlagsToAstClassFlags(int $flags) {
1548
1569
* @param ?ast\Node $stmts
1549
1570
* @param int $line
1550
1571
* @param int $end_line
1572
+ * @param ?string $doc_comment
1551
1573
*/
1552
1574
private static function astStmtClass (
1553
1575
int $ flags ,
1554
- ? string $ name ,
1555
- ? ast \ Node $ extends ,
1556
- ? array $ implements ,
1557
- ? ast \ Node $ stmts ,
1576
+ $ name ,
1577
+ $ extends ,
1578
+ $ implements ,
1579
+ $ stmts ,
1558
1580
int $ line ,
1559
1581
int $ end_line ,
1560
- ? string $ doc_comment
1582
+ $ doc_comment
1561
1583
) : ast \Node {
1562
1584
if ($ name === null ) {
1563
1585
$ flags |= ast \flags \CLASS_ANONYMOUS ;
@@ -1602,9 +1624,10 @@ private static function phpParserArgListToAstArgList(array $args, int $line) : a
1602
1624
return $ node ;
1603
1625
}
1604
1626
1605
- private static function phpParserUseListToAstUseList (?array $ uses ) : array {
1627
+ /** @param ?array $uses */
1628
+ private static function phpParserUseListToAstUseList ($ uses ) : array {
1606
1629
$ ast_uses = [];
1607
- foreach ($ uses as $ use ) {
1630
+ foreach ($ uses ?? [] as $ use ) {
1608
1631
$ ast_use = new ast \Node ();
1609
1632
$ ast_use ->kind = ast \AST_USE_ELEM ;
1610
1633
$ ast_use ->flags = self ::phpParserUseTypeToAstFlags ($ use ->type ); // FIXME
@@ -1769,7 +1792,10 @@ private static function phpParserNodesToLeftRightChildren($left, $right) : array
1769
1792
];
1770
1793
}
1771
1794
1772
- private static function phpParserPropelemToAstPropelem (PhpParser \Node \Stmt \PropertyProperty $ n , ?string $ doc_comment ) : ast \Node {
1795
+ /**
1796
+ * @param ?string $doc_comment
1797
+ */
1798
+ private static function phpParserPropelemToAstPropelem (PhpParser \Node \Stmt \PropertyProperty $ n , $ doc_comment ) : ast \Node {
1773
1799
$ children = [
1774
1800
'name ' => $ n ->name ,
1775
1801
'default ' => $ n ->default ? self ::phpParserNodeToAstNode ($ n ->default ) : null ,
@@ -1780,7 +1806,10 @@ private static function phpParserPropelemToAstPropelem(PhpParser\Node\Stmt\Prope
1780
1806
return self ::newAstNode (ast \AST_PROP_ELEM , 0 , $ children , $ start_line , self ::extractPhpdocComment ($ n ->getAttribute ('comments ' ) ?? $ doc_comment ));
1781
1807
}
1782
1808
1783
- private static function phpParserConstelemToAstConstelem (PhpParser \Node \Const_ $ n , ?string $ doc_comment ) : ast \Node {
1809
+ /**
1810
+ * @param ?string $doc_comment
1811
+ */
1812
+ private static function phpParserConstelemToAstConstelem (PhpParser \Node \Const_ $ n , $ doc_comment ) : ast \Node {
1784
1813
$ children = [
1785
1814
'name ' => $ n ->name ,
1786
1815
'value ' => self ::phpParserNodeToAstNode ($ n ->value ),
@@ -1865,19 +1894,24 @@ private static function phpParserDeclareListToAstDeclares(array $declares, int $
1865
1894
$ doc_comment = self ::extractPhpdocComment ($ declare ->getAttribute ('comments ' )) ?? $ first_doc_comment ;
1866
1895
$ first_doc_comment = null ;
1867
1896
if (self ::$ ast_version >= 50 ) {
1868
- $ children ['docComment ' ] = $ doc_comment ;
1897
+ if (PHP_VERSION_ID >= 70100 ) {
1898
+ $ children ['docComment ' ] = $ doc_comment ;
1899
+ }
1869
1900
}
1870
1901
$ node = new ast \Node (ast \AST_CONST_ELEM , 0 , $ children , $ declare ->getAttribute ('startLine ' ));
1871
1902
if (self ::$ ast_version < 50 && is_string ($ doc_comment )) {
1872
- $ node ->docComment = $ doc_comment ;
1903
+ if (PHP_VERSION_ID >= 70100 ) {
1904
+ $ node ->docComment = $ doc_comment ;
1905
+ }
1873
1906
}
1874
1907
$ ast_declare_elements [] = $ node ;
1875
1908
}
1876
1909
return new ast \Node (ast \AST_CONST_DECL , 0 , $ ast_declare_elements , $ start_line );
1877
1910
1878
1911
}
1879
1912
1880
- private static function astStmtDeclare (ast \Node $ declares , ?ast \Node $ stmts , int $ start_line ) : ast \Node {
1913
+ /** @param ?ast\Node $stmts */
1914
+ private static function astStmtDeclare (ast \Node $ declares , $ stmts , int $ start_line ) : ast \Node {
1881
1915
$ children = [
1882
1916
'declares ' => $ declares ,
1883
1917
'stmts ' => $ stmts ,
@@ -1910,7 +1944,11 @@ private static function astNodeStaticCall($class, $method, ast\Node $args, int $
1910
1944
return new ast \Node (ast \AST_STATIC_CALL , 0 , ['class ' => $ class , 'method ' => $ method , 'args ' => $ args ], $ start_line );
1911
1945
}
1912
1946
1913
- private static function extractPhpdocComment ($ comments ) : ?string {
1947
+ /**
1948
+ * @param ?string|?array $comments
1949
+ * @return ?string
1950
+ */
1951
+ private static function extractPhpdocComment ($ comments ) {
1914
1952
if (\is_string ($ comments )) {
1915
1953
return $ comments ;
1916
1954
}
@@ -1947,7 +1985,7 @@ private static function phpParserListToAstList(PhpParser\Node\Expr\List_ $n, int
1947
1985
}
1948
1986
1949
1987
// convert list($x,) to list($x), list(,) to list(), etc.
1950
- if (\count ($ ast_items ) > 0 && end ($ ast_items ) === null ) {
1988
+ if (PHP_VERSION_ID >= 70100 && \count ($ ast_items ) > 0 && end ($ ast_items ) === null ) {
1951
1989
array_pop ($ ast_items );
1952
1990
}
1953
1991
return new ast \Node (ast \AST_ARRAY , ast \flags \ARRAY_SYNTAX_LIST , $ ast_items , $ start_line );
@@ -1966,11 +2004,16 @@ private static function phpParserArrayToAstArray(PhpParser\Node\Expr\Array_ $n,
1966
2004
], $ item ->getAttribute ('startLine ' ));
1967
2005
}
1968
2006
}
1969
- $ flags = $ n ->getAttribute ('kind ' ) === PhpParser \Node \Expr \Array_::KIND_LONG ? ast \flags \ARRAY_SYNTAX_LONG : ast \flags \ARRAY_SYNTAX_SHORT ;
2007
+ if (PHP_VERSION_ID < 70100 ) {
2008
+ $ flags = 0 ;
2009
+ } else {
2010
+ $ flags = ($ n ->getAttribute ('kind ' ) === PhpParser \Node \Expr \Array_::KIND_LONG ) ? ast \flags \ARRAY_SYNTAX_LONG : ast \flags \ARRAY_SYNTAX_SHORT ;
2011
+ }
1970
2012
return new ast \Node (ast \AST_ARRAY , $ flags , $ ast_items , $ start_line );
1971
2013
}
1972
2014
1973
- private static function phpParserPropertyfetchToAstProp (PhpParser \Node \Expr \PropertyFetch $ n , int $ start_line ) : ?ast \Node {
2015
+ /** @return ?ast\Node */
2016
+ private static function phpParserPropertyfetchToAstProp (PhpParser \Node \Expr \PropertyFetch $ n , int $ start_line ) {
1974
2017
$ name = $ n ->name ;
1975
2018
if (is_object ($ name )) {
1976
2019
$ name = self ::phpParserNodeToAstNode ($ name );
@@ -1988,7 +2031,8 @@ private static function phpParserPropertyfetchToAstProp(PhpParser\Node\Expr\Prop
1988
2031
], $ start_line );
1989
2032
}
1990
2033
1991
- private static function phpParserClassconstfetchToAstClassconstfetch (PhpParser \Node \Expr \ClassConstFetch $ n , int $ start_line ) : ?ast \Node {
2034
+ /** @return ?ast\Node */
2035
+ private static function phpParserClassconstfetchToAstClassconstfetch (PhpParser \Node \Expr \ClassConstFetch $ n , int $ start_line ) {
1992
2036
$ name = $ n ->name ;
1993
2037
if (is_object ($ name )) {
1994
2038
$ name = self ::phpParserNodeToAstNode ($ name );
@@ -2028,7 +2072,9 @@ private static function phpParserNameToString(PhpParser\Node\Name $name) : strin
2028
2072
private static function newAstNode (int $ kind , int $ flags , array $ children , int $ lineno , string $ doc_comment = null ) : ast \Node {
2029
2073
if (self ::$ ast_version >= 45 ) {
2030
2074
if (is_string ($ doc_comment ) || array_key_exists ($ kind , self ::_NODES_WITH_NULL_DOC_COMMENT )) {
2031
- $ children ['docComment ' ] = $ doc_comment ;
2075
+ if ($ kind !== \ast \AST_CONST_ELEM || PHP_VERSION_ID >= 70100 ) {
2076
+ $ children ['docComment ' ] = $ doc_comment ;
2077
+ }
2032
2078
}
2033
2079
return new ast \Node ($ kind , $ flags , $ children , $ lineno );
2034
2080
}
@@ -2079,14 +2125,16 @@ private static function nextDeclId() : int {
2079
2125
2080
2126
}
2081
2127
2082
- function sl ($ node ) : ?int {
2128
+ /** @return ?int */
2129
+ function sl ($ node ) {
2083
2130
if ($ node instanceof PhpParser \Node) {
2084
2131
return $ node ->getAttribute ('startLine ' );
2085
2132
}
2086
2133
return null ;
2087
2134
}
2088
2135
2089
- function el ($ node ) : ?int {
2136
+ /** @return ?int */
2137
+ function el ($ node ) {
2090
2138
if ($ node instanceof PhpParser \Node) {
2091
2139
return $ node ->getAttribute ('endLine ' );
2092
2140
}
0 commit comments