@@ -466,6 +466,22 @@ private static function _init_handle_map() : array {
466466 $ startLine
467467 );
468468 },
469+ 'PhpParser\Node\Stmt\Namespace_ ' => function (PhpParser \Node \Stmt \Namespace_ $ n , int $ startLine ) : \ast \Node {
470+ $ nodeDumper = new \PhpParser \NodeDumper ([
471+ 'dumpComments ' => true ,
472+ 'dumpPositions ' => true ,
473+ ]);
474+ echo $ nodeDumper ->dump ($ n );
475+ return astnode (
476+ \ast \AST_NAMESPACE ,
477+ 0 ,
478+ [
479+ 'name ' => implode ('\\' , $ n ->name ->parts ?? []),
480+ 'stmts ' => isset ($ n ->stmts ) ? self ::_phpparser_stmtlist_to_ast_node ($ n ->stmts , $ startLine ) : null ,
481+ ],
482+ $ startLine
483+ );
484+ },
469485 'PhpParser\Node\Stmt\Property ' => function (PhpParser \Node \Stmt \Property $ n , int $ startLine ) : \ast \Node {
470486 return self ::_phpparser_property_to_ast_node ($ n , $ startLine );
471487 },
@@ -490,7 +506,7 @@ private static function _init_handle_map() : array {
490506 }
491507 return self ::_ast_node_try (
492508 self ::_phpparser_stmtlist_to_ast_node ($ n ->stmts , $ startLine ), // $n->try
493- self ::_phpparser_catchlist_to_ast_catchlist ($ n ->catches ),
509+ self ::_phpparser_catchlist_to_ast_catchlist ($ n ->catches , $ startLine ),
494510 isset ($ n ->finally ) ? self ::_phpparser_stmtlist_to_ast_node ($ n ->finally ->stmts , sl ($ n ->finally )) : null ,
495511 $ startLine
496512 );
@@ -552,15 +568,15 @@ private static function _ast_stmt_catch($types, string $var, $stmts, int $lineno
552568 return $ node ;
553569 }
554570
555- private static function _phpparser_catchlist_to_ast_catchlist (array $ catches ) : \ast \Node {
571+ private static function _phpparser_catchlist_to_ast_catchlist (array $ catches, int $ lineno ) : \ast \Node {
556572 $ node = new \ast \Node ();
557573 $ node ->kind = \ast \AST_CATCH_LIST ;
558- $ node ->lineno = 0 ;
559574 $ node ->flags = 0 ;
560575 $ children = [];
561576 foreach ($ catches as $ parserCatch ) {
562577 $ children [] = self ::_phpparser_node_to_ast_node ($ parserCatch );
563578 }
579+ $ node ->lineno = $ children [0 ]->lineno ?? $ lineno ;
564580 $ node ->children = $ children ;
565581 return $ node ;
566582 }
@@ -703,7 +719,7 @@ private static function _ast_node_nullable_type(\ast\Node $type, int $line) {
703719 $ node = new \ast \Node ;
704720 $ node ->kind = \ast \AST_NULLABLE_TYPE ;
705721 // FIXME: Why is this a special case in php-ast? (e.g. nullable int has no flags on the nullable node)
706- $ node ->flags = ( $ type -> kind === \ ast \ AST_TYPE && $ type -> flags === \ ast \ flags \ TYPE_ARRAY ) ? $ type -> flags : 0 ;
722+ $ node ->flags = 0 ;
707723 $ node ->lineno = $ line ;
708724 $ node ->children = ['type ' => $ type ];
709725 return $ node ;
@@ -759,7 +775,7 @@ private static function _phpparser_params_to_ast_params(array $parserParams, int
759775 */
760776 private static function _ast_stub ($ parserNode ) : \ast \Node {
761777 $ node = new \ast \Node ();
762- $ node ->kind = "TODO: " . get_class ($ parserNode );
778+ $ node ->kind = "TODO: " . get_class ($ parserNode ) . json_encode ( array_keys (( array ) $ parserNode )) ;
763779 $ node ->flags = 0 ;
764780 $ node ->lineno = $ parserNode ->getAttribute ('startLine ' );
765781 $ node ->children = null ;
@@ -769,11 +785,15 @@ private static function _ast_stub($parserNode) : \ast\Node{
769785 /**
770786 * @param PhpParser\Expr\ClosureUse[] $uses
771787 * @param int $line
788+ * @return ?\ast\Node
772789 */
773790 private static function _phpparser_closure_uses_to_ast_closure_uses (
774791 array $ uses ,
775792 int $ line
776- ) : \ast \Node {
793+ ) {
794+ if (count ($ uses ) === 0 ) {
795+ return null ;
796+ }
777797 $ astUses = [];
778798 foreach ($ uses as $ use ) {
779799 $ astUses [] = astnode (\ast \AST_CLOSURE_VAR , $ use ->byRef ? 1 : 0 , ['name ' => $ use ->var ], $ use ->getAttribute ('startLine ' ));
@@ -891,12 +911,12 @@ private static function _ast_stmt_class(
891911 private static function _phpparser_arg_list_to_ast_arg_list (array $ args , int $ line ) : \ast \Node {
892912 $ node = new \ast \Node ();
893913 $ node ->kind = \ast \AST_ARG_LIST ;
894- $ node ->lineno = $ line ;
895914 $ node ->flags = 0 ;
896915 $ astArgs = [];
897916 foreach ($ args as $ arg ) {
898917 $ astArgs [] = self ::_phpparser_node_to_ast_node ($ arg );
899918 }
919+ $ node ->lineno = $ astArgs [0 ]->lineno ?? $ line ;
900920 $ node ->children = $ astArgs ;
901921 return $ node ;
902922 }
@@ -1160,7 +1180,7 @@ private static function _extract_phpdoc_comment($comments) : ?string {
11601180 if ($ comments [$ i ] instanceof PhpParser \Comment \Doc) {
11611181 return $ comments [$ i ]->getText ();
11621182 } else {
1163- var_dump ( $ comments [ $ i ]);
1183+ // e.g. PhpParser\Comment; for a line comment
11641184 }
11651185 }
11661186 return null ;
0 commit comments