@@ -39,6 +39,7 @@ class Printer
3939 public function printFunction (GlobalFunction $ function , PhpNamespace $ namespace = null ): string
4040 {
4141 return Helpers::formatDocComment ($ function ->getComment () . "\n" )
42+ . self ::printAttributes ($ function ->getAttributes (), $ namespace )
4243 . 'function '
4344 . ($ function ->getReturnReference () ? '& ' : '' )
4445 . $ function ->getName ()
@@ -58,7 +59,8 @@ public function printClosure(Closure $closure): string
5859 ? "\n" . $ this ->indentation . implode (", \n" . $ this ->indentation , $ uses ) . "\n"
5960 : $ tmp ;
6061
61- return 'function '
62+ return self ::printAttributes ($ closure ->getAttributes (), null , true )
63+ . 'function '
6264 . ($ closure ->getReturnReference () ? '& ' : '' )
6365 . $ this ->printParameters ($ closure , null )
6466 . ($ uses ? " use ( $ useStr) " : '' )
@@ -75,7 +77,8 @@ public function printArrowFunction(Closure $closure): string
7577 }
7678 }
7779
78- return 'fn '
80+ return self ::printAttributes ($ closure ->getAttributes (), null )
81+ . 'fn '
7982 . ($ closure ->getReturnReference () ? '& ' : '' )
8083 . $ this ->printParameters ($ closure , null )
8184 . $ this ->printReturnType ($ closure , null )
@@ -87,6 +90,7 @@ public function printMethod(Method $method, PhpNamespace $namespace = null): str
8790 {
8891 $ method ->validate ();
8992 return Helpers::formatDocComment ($ method ->getComment () . "\n" )
93+ . self ::printAttributes ($ method ->getAttributes (), $ namespace )
9094 . ($ method ->isAbstract () ? 'abstract ' : '' )
9195 . ($ method ->isFinal () ? 'final ' : '' )
9296 . ($ method ->getVisibility () ? $ method ->getVisibility () . ' ' : '' )
@@ -122,6 +126,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
122126 foreach ($ class ->getConstants () as $ const ) {
123127 $ def = ($ const ->getVisibility () ? $ const ->getVisibility () . ' ' : '' ) . 'const ' . $ const ->getName () . ' = ' ;
124128 $ consts [] = Helpers::formatDocComment ((string ) $ const ->getComment ())
129+ . self ::printAttributes ($ const ->getAttributes (), $ namespace )
125130 . $ def
126131 . $ this ->dump ($ const ->getValue (), strlen ($ def )) . "; \n" ;
127132 }
@@ -134,6 +139,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
134139 . '$ ' . $ property ->getName ());
135140
136141 $ properties [] = Helpers::formatDocComment ((string ) $ property ->getComment ())
142+ . self ::printAttributes ($ property ->getAttributes (), $ namespace )
137143 . $ def
138144 . ($ property ->getValue () === null && !$ property ->isInitialized () ? '' : ' = ' . $ this ->dump ($ property ->getValue (), strlen ($ def ) + 3 )) // 3 = ' = '
139145 . "; \n" ;
@@ -154,6 +160,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
154160
155161 return Strings::normalize (
156162 Helpers::formatDocComment ($ class ->getComment () . "\n" )
163+ . self ::printAttributes ($ class ->getAttributes (), $ namespace )
157164 . ($ class ->isAbstract () ? 'abstract ' : '' )
158165 . ($ class ->isFinal () ? 'final ' : '' )
159166 . ($ class ->getName () ? $ class ->getType () . ' ' . $ class ->getName () . ' ' : '' )
@@ -259,14 +266,15 @@ public function printParameters($function, PhpNamespace $namespace = null): stri
259266 $ promoted = $ param instanceof PromotedParameter ? $ param : null ;
260267 $ params [] =
261268 ($ promoted ? Helpers::formatDocComment ((string ) $ promoted ->getComment ()) : '' )
262- . ($ promoted ? ($ promoted ->getVisibility () ?: 'public ' ) . ' ' : '' )
269+ . ($ attrs = self ::printAttributes ($ param ->getAttributes (), $ namespace , true ))
270+ . ($ promoted ? ($ param ->getVisibility () ?: 'public ' ) . ' ' : '' )
263271 . ltrim ($ this ->printType ($ type , $ param ->isNullable (), $ namespace ) . ' ' )
264272 . ($ param ->isReference () ? '& ' : '' )
265273 . ($ variadic ? '... ' : '' )
266274 . '$ ' . $ param ->getName ()
267275 . ($ param ->hasDefaultValue () && !$ variadic ? ' = ' . $ this ->dump ($ param ->getDefaultValue ()) : '' );
268276
269- $ special = $ special || $ promoted ;
277+ $ special = $ special || $ promoted || $ attrs ;
270278 }
271279
272280 $ line = implode (', ' , $ params );
@@ -296,6 +304,22 @@ private function printReturnType($function, ?PhpNamespace $namespace): string
296304 }
297305
298306
307+ private function printAttributes (array $ attrs , ?PhpNamespace $ namespace , bool $ inline = false ): string
308+ {
309+ if (!$ attrs ) {
310+ return '' ;
311+ }
312+ $ items = [];
313+ foreach ($ attrs as $ attr ) {
314+ $ args = (new Dumper )->format ('...? ' , $ attr ->getArguments ());
315+ $ items [] = $ this ->printType ($ attr ->getName (), false , $ namespace ) . ($ args ? "( $ args) " : '' );
316+ }
317+ return $ inline
318+ ? '#[ ' . implode (', ' , $ items ) . '] '
319+ : '#[ ' . implode ("] \n#[ " , $ items ) . "] \n" ;
320+ }
321+
322+
299323 private function joinProperties (array $ props )
300324 {
301325 return $ this ->linesBetweenProperties
0 commit comments