10
10
namespace Nette \PhpGenerator ;
11
11
12
12
use Nette ;
13
+ use Nette \PhpGenerator \Traits \CommentAware ;
13
14
use Nette \Utils \Strings ;
14
15
15
16
@@ -48,7 +49,7 @@ public function printFunction(GlobalFunction $function, ?PhpNamespace $namespace
48
49
$ body = Helpers::simplifyTaggedNames ($ function ->getBody (), $ this ->namespace );
49
50
$ body = ltrim (rtrim (Strings::normalize ($ body )) . "\n" );
50
51
51
- return Helpers:: formatDocComment ( $ function -> getComment () . "\n" )
52
+ return $ this -> printDocComment ( $ function )
52
53
. self ::printAttributes ($ function ->getAttributes ())
53
54
. $ line
54
55
. $ this ->printParameters ($ function , strlen ($ line ) + strlen ($ returnType ) + 2 ) // 2 = parentheses
@@ -119,7 +120,7 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo
119
120
$ body = ltrim (rtrim (Strings::normalize ($ body )) . "\n" );
120
121
$ braceOnNextLine = $ this ->bracesOnNextLine && !str_contains ($ params , "\n" );
121
122
122
- return Helpers:: formatDocComment ( $ method -> getComment () . "\n" )
123
+ return $ this -> printDocComment ( $ method )
123
124
. self ::printAttributes ($ method ->getAttributes ())
124
125
. $ line
125
126
. $ params
@@ -144,7 +145,7 @@ public function printClass(
144
145
if ($ class instanceof ClassType || $ class instanceof TraitType || $ class instanceof EnumType) {
145
146
foreach ($ class ->getTraits () as $ trait ) {
146
147
$ resolutions = $ trait ->getResolutions ();
147
- $ traits [] = Helpers:: formatDocComment (( string ) $ trait -> getComment () )
148
+ $ traits [] = $ this -> printDocComment ( $ trait )
148
149
. 'use ' . $ resolver ($ trait ->getName ())
149
150
. ($ resolutions
150
151
? " { \n" . $ this ->indentation . implode ("; \n" . $ this ->indentation , $ resolutions ) . "; \n} \n"
@@ -158,7 +159,7 @@ public function printClass(
158
159
$ enumType = $ class ->getType ();
159
160
foreach ($ class ->getCases () as $ case ) {
160
161
$ enumType ??= is_scalar ($ case ->getValue ()) ? get_debug_type ($ case ->getValue ()) : null ;
161
- $ cases [] = Helpers:: formatDocComment (( string ) $ case -> getComment () )
162
+ $ cases [] = $ this -> printDocComment ( $ case )
162
163
. self ::printAttributes ($ case ->getAttributes ())
163
164
. 'case ' . $ case ->getName ()
164
165
. ($ case ->getValue () === null ? '' : ' = ' . $ this ->dump ($ case ->getValue ()))
@@ -173,7 +174,7 @@ public function printClass(
173
174
. ($ const ->getVisibility () ? $ const ->getVisibility () . ' ' : '' )
174
175
. 'const ' . $ const ->getName () . ' = ' ;
175
176
176
- $ consts [] = Helpers:: formatDocComment (( string ) $ const -> getComment () )
177
+ $ consts [] = $ this -> printDocComment ( $ const )
177
178
. self ::printAttributes ($ const ->getAttributes ())
178
179
. $ def
179
180
. $ this ->dump ($ const ->getValue (), strlen ($ def )) . "; \n" ;
@@ -204,7 +205,7 @@ public function printClass(
204
205
. ltrim ($ this ->printType ($ type , $ property ->isNullable ()) . ' ' )
205
206
. '$ ' . $ property ->getName ());
206
207
207
- $ properties [] = Helpers:: formatDocComment (( string ) $ property -> getComment () )
208
+ $ properties [] = $ this -> printDocComment ( $ property )
208
209
. self ::printAttributes ($ property ->getAttributes ())
209
210
. $ def
210
211
. ($ property ->getValue () === null && !$ property ->isInitialized ()
@@ -242,7 +243,7 @@ public function printClass(
242
243
: null ;
243
244
$ line [] = $ class ->getName () ? null : '{ ' ;
244
245
245
- return Helpers:: formatDocComment ( $ class -> getComment () . "\n" )
246
+ return $ this -> printDocComment ( $ class )
246
247
. self ::printAttributes ($ class ->getAttributes ())
247
248
. implode (' ' , array_filter ($ line ))
248
249
. ($ class ->getName () ? "\n{ \n" : "\n" )
@@ -295,7 +296,7 @@ public function printFile(PhpFile $file): string
295
296
}
296
297
297
298
return "<?php \n"
298
- . ($ file ->getComment () ? "\n" . Helpers:: formatDocComment ( $ file -> getComment () . "\n" ) : '' )
299
+ . ($ file ->getComment () ? "\n" . $ this -> printDocComment ( $ file ) : '' )
299
300
. "\n"
300
301
. ($ file ->hasStrictTypes () ? "declare(strict_types=1); \n\n" : '' )
301
302
. implode ("\n\n" , $ namespaces );
@@ -332,7 +333,7 @@ protected function printParameters(Closure|GlobalFunction|Method $function, int
332
333
$ type = $ param ->getType ();
333
334
$ promoted = $ param instanceof PromotedParameter ? $ param : null ;
334
335
$ params [] =
335
- ($ promoted ? Helpers:: formatDocComment (( string ) $ promoted -> getComment () ) : '' )
336
+ ($ promoted ? $ this -> printDocComment ( $ promoted , false ) : '' )
336
337
. ($ attrs = self ::printAttributes ($ param ->getAttributes (), inline: true ))
337
338
. ($ promoted ?
338
339
($ promoted ->getVisibility () ?: 'public ' )
@@ -374,6 +375,11 @@ protected function printType(?string $type, bool $nullable): string
374
375
return $ type ;
375
376
}
376
377
378
+ /** @param CommentAware $commentable */
379
+ protected function printDocComment ($ commentable , bool $ newline = true ): string
380
+ {
381
+ return ($ commentable instanceof PhpFile && $ commentable ->getComment () ? "\n" : '' ) . Helpers::formatDocComment ($ commentable ->getComment () . ($ commentable ->getComment () && $ newline ? "\n" : '' ));
382
+ }
377
383
378
384
private function printReturnType (Closure |GlobalFunction |Method $ function ): string
379
385
{
0 commit comments