11<?php namespace lang \meta ;
22
3+ use PhpToken ;
34use lang \reflection \Annotation ;
45
56/**
@@ -61,43 +62,55 @@ public function ofParameter($method, $reflect) {
6162 * @return [:string]
6263 */
6364 public function imports ($ reflect ) {
64- static $ break = [T_CLASS => true , T_INTERFACE => true , T_TRAIT => true ];
65+ static $ break = [T_CLASS => true , T_INTERFACE => true , T_TRAIT => true , 372 /* T_ENUM */ => true ];
66+ static $ types = [T_WHITESPACE => true , 44 => true , 59 => true , 123 => true ];
6567
66- $ tokens = \ PhpToken::tokenize (file_get_contents ($ reflect ->getFileName ()));
68+ $ tokens = PhpToken::tokenize (file_get_contents ($ reflect ->getFileName ()));
6769 $ imports = [];
6870 for ($ i = 0 , $ s = sizeof ($ tokens ); $ i < $ s ; $ i ++) {
6971 if (isset ($ break [$ tokens [$ i ]->id ])) break ;
7072 if (T_USE !== $ tokens [$ i ]->id ) continue ;
7173
72- $ type = '' ;
73- for ($ i += 2 ; $ i < $ s , !(59 === $ tokens [$ i ]->id || 123 === $ tokens [$ i ]->id || T_WHITESPACE === $ tokens [$ i ]->id ); $ i ++) {
74- $ type .= $ tokens [$ i ]->text ;
75- }
74+ do {
75+ $ type = '' ;
76+ for ($ i += 2 ; $ i < $ s , !isset ($ types [$ tokens [$ i ]->id ]); $ i ++) {
77+ $ type .= $ tokens [$ i ]->text ;
78+ }
79+
80+ // Skip over whitespace
81+ if (T_WHITESPACE === $ tokens [$ i ]->id ) $ i ++;
7682
77- // use `lang\{Type, Primitive as P}` vs. `use lang\Primitive as P;` vs. `use lang\Primitive`
78- if (123 === $ tokens [$ i ]->id ) {
79- $ alias = null ;
80- $ group = '' ;
81- for ($ i += 1 ; $ i < $ s ; $ i ++) {
82- if (44 === $ tokens [$ i ]->id ) {
83- $ imports [$ alias ?? $ group ]= $ type .$ group ;
84- $ alias = null ;
85- $ group = '' ;
86- } else if (125 === $ tokens [$ i ]->id ) {
87- $ imports [$ alias ?? $ group ]= $ type .$ group ;
88- break ;
89- } else if (T_AS === $ tokens [$ i ]->id ) {
90- $ i += 2 ;
91- $ alias = $ tokens [$ i ][1 ];
92- } else if (T_WHITESPACE !== $ tokens [$ i ]->id ) {
93- $ group .= $ tokens [$ i ]->text ;
83+ // use `lang\{Type, Primitive as P}` vs. `use lang\Primitive as P;` vs. `use lang\Primitive`
84+ if (123 === $ tokens [$ i ]->id ) {
85+ $ alias = null ;
86+ $ group = '' ;
87+ for ($ i += 1 ; $ i < $ s ; $ i ++) {
88+ if (44 === $ tokens [$ i ]->id ) {
89+ $ imports [$ alias ?? $ group ]= $ type .$ group ;
90+ $ alias = null ;
91+ $ group = '' ;
92+ } else if (125 === $ tokens [$ i ]->id ) {
93+ $ imports [$ alias ?? $ group ]= $ type .$ group ;
94+ break ;
95+ } else if (T_AS === $ tokens [$ i ]->id ) {
96+ $ i += 2 ;
97+ $ alias = $ tokens [$ i ]->text ;
98+ } else if (T_WHITESPACE !== $ tokens [$ i ]->id ) {
99+ $ group .= $ tokens [$ i ]->text ;
100+ }
94101 }
102+ } else if (T_AS === $ tokens [$ i ]->id ) {
103+ $ i += 2 ;
104+ $ imports [$ tokens [$ i ]->text ]= $ type ;
105+ } else if (false === ($ p = strrpos ($ type , '\\' ))) {
106+ $ imports [$ type ]= null ;
107+ } else {
108+ $ imports [substr ($ type , strrpos ($ type , '\\' ) + 1 )]= $ type ;
95109 }
96- } else if (T_AS === $ tokens [++$ i ]->id ) {
97- $ imports [$ tokens [$ i + 2 ]->text ]= $ type ;
98- } else {
99- $ imports [substr ($ type , strrpos ($ type , '\\' ) + 1 )]= $ type ;
100- }
110+
111+ // Skip over whitespace
112+ if (T_WHITESPACE === $ tokens [$ i ]->id ) $ i ++;
113+ } while (44 === $ tokens [$ i ]->id );
101114 }
102115 return $ imports ;
103116 }
@@ -108,7 +121,7 @@ public function evaluate($reflect, $code) {
108121 $ header .= 'namespace ' .$ namespace .'; ' ;
109122 }
110123 foreach ($ this ->imports ($ reflect ) as $ import => $ type ) {
111- $ header .= ' use ' . $ type. ' as ' . $ import. ' ; ' ;
124+ $ header .= $ type ? " use { $ type} as { $ import} ; " : " use { $ import } ; " ;
112125 }
113126
114127 $ f = eval ($ header .' return static function() { return ' .$ code .'; }; ' );
0 commit comments