@@ -34,6 +34,7 @@ public function __construct(string $code)
34
34
if (!class_exists (ParserFactory::class)) {
35
35
throw new Nette \NotSupportedException ("PHP-Parser is required to load method bodies, install package 'nikic/php-parser' 4.7 or newer. " );
36
36
}
37
+
37
38
$ this ->printer = new PhpParser \PrettyPrinter \Standard ;
38
39
$ this ->parseCode ($ code );
39
40
}
@@ -44,6 +45,7 @@ private function parseCode(string $code): void
44
45
if (substr ($ code , 0 , 5 ) !== '<?php ' ) {
45
46
throw new Nette \InvalidStateException ('The input string is not a PHP code. ' );
46
47
}
48
+
47
49
$ this ->code = str_replace ("\r\n" , "\n" , $ code );
48
50
$ lexer = new PhpParser \Lexer \Emulative (['usedAttributes ' => ['startFilePos ' , 'endFilePos ' , 'comments ' ]]);
49
51
$ parser = (new ParserFactory )->create (ParserFactory::ONLY_PHP7 , $ lexer );
@@ -71,6 +73,7 @@ public function extractMethodBodies(string $className): array
71
73
$ res [$ methodNode ->name ->toString ()] = $ this ->getReformattedContents ($ methodNode ->stmts , 2 );
72
74
}
73
75
}
76
+
74
77
return $ res ;
75
78
}
76
79
@@ -111,7 +114,6 @@ private function prepareReplacements(array $statements): array
111
114
Helpers::tagName ($ node ->toCodeString (), $ of ),
112
115
];
113
116
}
114
-
115
117
} elseif ($ node instanceof Node \Scalar \String_ || $ node instanceof Node \Scalar \EncapsedStringPart) {
116
118
// multi-line strings => singleline
117
119
$ token = $ this ->getNodeContents ($ node );
@@ -123,7 +125,6 @@ private function prepareReplacements(array $statements): array
123
125
$ quote . addcslashes ($ node ->value , "\x00.. \x1F" ) . $ quote ,
124
126
];
125
127
}
126
-
127
128
} elseif ($ node instanceof Node \Scalar \Encapsed) {
128
129
// HEREDOC => "string"
129
130
if ($ node ->getAttribute ('kind ' ) === Node \Scalar \String_::KIND_HEREDOC ) {
@@ -153,6 +154,7 @@ private function performReplacements(string $s, array $replacements): string
153
154
foreach ($ replacements as [$ start , $ end , $ replacement ]) {
154
155
$ s = substr_replace ($ s , $ replacement , $ start , $ end - $ start + 1 );
155
156
}
157
+
156
158
return $ s ;
157
159
}
158
160
@@ -179,6 +181,7 @@ public function enterNode(Node $node)
179
181
if (!$ node ->name ) {
180
182
return PhpParser \NodeTraverser::DONT_TRAVERSE_CHILDREN ;
181
183
}
184
+
182
185
$ class = $ this ->addClassToFile ($ phpFile , $ node );
183
186
} elseif ($ node instanceof Node \Stmt \Interface_) {
184
187
$ class = $ this ->addInterfaceToFile ($ phpFile , $ node );
@@ -199,6 +202,7 @@ public function enterNode(Node $node)
199
202
} elseif ($ node instanceof Node \Stmt \EnumCase) {
200
203
$ this ->addEnumCaseToClass ($ class , $ node );
201
204
}
205
+
202
206
if ($ node instanceof Node \FunctionLike) {
203
207
return PhpParser \NodeTraverser::DONT_TRAVERSE_CHILDREN ;
204
208
}
@@ -230,9 +234,11 @@ private function addClassToFile(PhpFile $phpFile, Node\Stmt\Class_ $node): Class
230
234
if ($ node ->extends ) {
231
235
$ class ->setExtends ($ node ->extends ->toString ());
232
236
}
237
+
233
238
foreach ($ node ->implements as $ item ) {
234
239
$ class ->addImplement ($ item ->toString ());
235
240
}
241
+
236
242
$ class ->setFinal ($ node ->isFinal ());
237
243
$ class ->setAbstract ($ node ->isAbstract ());
238
244
$ this ->addCommentAndAttributes ($ class , $ node );
@@ -246,6 +252,7 @@ private function addInterfaceToFile(PhpFile $phpFile, Node\Stmt\Interface_ $node
246
252
foreach ($ node ->extends as $ item ) {
247
253
$ class ->addExtend ($ item ->toString ());
248
254
}
255
+
249
256
$ this ->addCommentAndAttributes ($ class , $ node );
250
257
return $ class ;
251
258
}
@@ -265,6 +272,7 @@ private function addEnumToFile(PhpFile $phpFile, Node\Stmt\Enum_ $node): ClassTy
265
272
foreach ($ node ->implements as $ item ) {
266
273
$ class ->addImplement ($ item ->toString ());
267
274
}
275
+
268
276
$ this ->addCommentAndAttributes ($ class , $ node );
269
277
return $ class ;
270
278
}
@@ -282,9 +290,11 @@ private function addTraitToClass(ClassType $class, Node\Stmt\TraitUse $node): vo
282
290
foreach ($ node ->traits as $ item ) {
283
291
$ trait = $ class ->addTrait ($ item ->toString (), true );
284
292
}
293
+
285
294
foreach ($ node ->adaptations as $ item ) {
286
295
$ trait ->addResolution (trim ($ this ->toPhp ($ item ), '; ' ));
287
296
}
297
+
288
298
$ this ->addCommentAndAttributes ($ trait , $ node );
289
299
}
290
300
@@ -299,10 +309,12 @@ private function addPropertyToClass(ClassType $class, Node\Stmt\Property $node):
299
309
} elseif ($ node ->isProtected ()) {
300
310
$ prop ->setProtected ();
301
311
}
312
+
302
313
$ prop ->setType ($ node ->type ? $ this ->toPhp ($ node ->type ) : null );
303
314
if ($ item ->default ) {
304
315
$ prop ->setValue (new Literal ($ this ->getReformattedContents ([$ item ->default ], 1 )));
305
316
}
317
+
306
318
$ prop ->setReadOnly (method_exists ($ node , 'isReadonly ' ) && $ node ->isReadonly ());
307
319
$ this ->addCommentAndAttributes ($ prop , $ node );
308
320
}
@@ -320,6 +332,7 @@ private function addMethodToClass(ClassType $class, Node\Stmt\ClassMethod $node)
320
332
} elseif ($ node ->isProtected ()) {
321
333
$ method ->setProtected ();
322
334
}
335
+
323
336
$ this ->setupFunction ($ method , $ node );
324
337
}
325
338
@@ -334,6 +347,7 @@ private function addConstantToClass(ClassType $class, Node\Stmt\ClassConst $node
334
347
} elseif ($ node ->isProtected ()) {
335
348
$ const ->setProtected ();
336
349
}
350
+
337
351
$ const ->setFinal (method_exists ($ node , 'isFinal ' ) && $ node ->isFinal ());
338
352
$ this ->addCommentAndAttributes ($ const , $ node );
339
353
}
@@ -366,6 +380,7 @@ private function addCommentAndAttributes($element, Node $node): void
366
380
$ args [] = $ value ;
367
381
}
368
382
}
383
+
369
384
$ element ->addAttribute ($ attribute ->name ->toString (), $ args );
370
385
}
371
386
}
@@ -387,8 +402,10 @@ private function setupFunction($function, Node\FunctionLike $node): void
387
402
if ($ item ->default ) {
388
403
$ param ->setDefaultValue (new Literal ($ this ->getReformattedContents ([$ item ->default ], 2 )));
389
404
}
405
+
390
406
$ this ->addCommentAndAttributes ($ param , $ item );
391
407
}
408
+
392
409
$ this ->addCommentAndAttributes ($ function , $ node );
393
410
if ($ node ->stmts ) {
394
411
$ function ->setBody ($ this ->getReformattedContents ($ node ->stmts , 2 ));
0 commit comments