@@ -34,6 +34,7 @@ public function __construct(string $code)
3434 if (!class_exists (ParserFactory::class)) {
3535 throw new Nette \NotSupportedException ("PHP-Parser is required to load method bodies, install package 'nikic/php-parser' 4.7 or newer. " );
3636 }
37+
3738 $ this ->printer = new PhpParser \PrettyPrinter \Standard ;
3839 $ this ->parseCode ($ code );
3940 }
@@ -44,6 +45,7 @@ private function parseCode(string $code): void
4445 if (substr ($ code , 0 , 5 ) !== '<?php ' ) {
4546 throw new Nette \InvalidStateException ('The input string is not a PHP code. ' );
4647 }
48+
4749 $ this ->code = str_replace ("\r\n" , "\n" , $ code );
4850 $ lexer = new PhpParser \Lexer \Emulative (['usedAttributes ' => ['startFilePos ' , 'endFilePos ' , 'comments ' ]]);
4951 $ parser = (new ParserFactory )->create (ParserFactory::ONLY_PHP7 , $ lexer );
@@ -71,6 +73,7 @@ public function extractMethodBodies(string $className): array
7173 $ res [$ methodNode ->name ->toString ()] = $ this ->getReformattedContents ($ methodNode ->stmts , 2 );
7274 }
7375 }
76+
7477 return $ res ;
7578 }
7679
@@ -111,7 +114,6 @@ private function prepareReplacements(array $statements): array
111114 Helpers::tagName ($ node ->toCodeString (), $ of ),
112115 ];
113116 }
114-
115117 } elseif ($ node instanceof Node \Scalar \String_ || $ node instanceof Node \Scalar \EncapsedStringPart) {
116118 // multi-line strings => singleline
117119 $ token = $ this ->getNodeContents ($ node );
@@ -123,7 +125,6 @@ private function prepareReplacements(array $statements): array
123125 $ quote . addcslashes ($ node ->value , "\x00.. \x1F" ) . $ quote ,
124126 ];
125127 }
126-
127128 } elseif ($ node instanceof Node \Scalar \Encapsed) {
128129 // HEREDOC => "string"
129130 if ($ node ->getAttribute ('kind ' ) === Node \Scalar \String_::KIND_HEREDOC ) {
@@ -153,6 +154,7 @@ private function performReplacements(string $s, array $replacements): string
153154 foreach ($ replacements as [$ start , $ end , $ replacement ]) {
154155 $ s = substr_replace ($ s , $ replacement , $ start , $ end - $ start + 1 );
155156 }
157+
156158 return $ s ;
157159 }
158160
@@ -179,6 +181,7 @@ public function enterNode(Node $node)
179181 if (!$ node ->name ) {
180182 return PhpParser \NodeTraverser::DONT_TRAVERSE_CHILDREN ;
181183 }
184+
182185 $ class = $ this ->addClassToFile ($ phpFile , $ node );
183186 } elseif ($ node instanceof Node \Stmt \Interface_) {
184187 $ class = $ this ->addInterfaceToFile ($ phpFile , $ node );
@@ -199,6 +202,7 @@ public function enterNode(Node $node)
199202 } elseif ($ node instanceof Node \Stmt \EnumCase) {
200203 $ this ->addEnumCaseToClass ($ class , $ node );
201204 }
205+
202206 if ($ node instanceof Node \FunctionLike) {
203207 return PhpParser \NodeTraverser::DONT_TRAVERSE_CHILDREN ;
204208 }
@@ -230,9 +234,11 @@ private function addClassToFile(PhpFile $phpFile, Node\Stmt\Class_ $node): Class
230234 if ($ node ->extends ) {
231235 $ class ->setExtends ($ node ->extends ->toString ());
232236 }
237+
233238 foreach ($ node ->implements as $ item ) {
234239 $ class ->addImplement ($ item ->toString ());
235240 }
241+
236242 $ class ->setFinal ($ node ->isFinal ());
237243 $ class ->setAbstract ($ node ->isAbstract ());
238244 $ this ->addCommentAndAttributes ($ class , $ node );
@@ -246,6 +252,7 @@ private function addInterfaceToFile(PhpFile $phpFile, Node\Stmt\Interface_ $node
246252 foreach ($ node ->extends as $ item ) {
247253 $ class ->addExtend ($ item ->toString ());
248254 }
255+
249256 $ this ->addCommentAndAttributes ($ class , $ node );
250257 return $ class ;
251258 }
@@ -265,6 +272,7 @@ private function addEnumToFile(PhpFile $phpFile, Node\Stmt\Enum_ $node): ClassTy
265272 foreach ($ node ->implements as $ item ) {
266273 $ class ->addImplement ($ item ->toString ());
267274 }
275+
268276 $ this ->addCommentAndAttributes ($ class , $ node );
269277 return $ class ;
270278 }
@@ -282,9 +290,11 @@ private function addTraitToClass(ClassType $class, Node\Stmt\TraitUse $node): vo
282290 foreach ($ node ->traits as $ item ) {
283291 $ trait = $ class ->addTrait ($ item ->toString (), true );
284292 }
293+
285294 foreach ($ node ->adaptations as $ item ) {
286295 $ trait ->addResolution (trim ($ this ->toPhp ($ item ), '; ' ));
287296 }
297+
288298 $ this ->addCommentAndAttributes ($ trait , $ node );
289299 }
290300
@@ -299,10 +309,12 @@ private function addPropertyToClass(ClassType $class, Node\Stmt\Property $node):
299309 } elseif ($ node ->isProtected ()) {
300310 $ prop ->setProtected ();
301311 }
312+
302313 $ prop ->setType ($ node ->type ? $ this ->toPhp ($ node ->type ) : null );
303314 if ($ item ->default ) {
304315 $ prop ->setValue (new Literal ($ this ->getReformattedContents ([$ item ->default ], 1 )));
305316 }
317+
306318 $ prop ->setReadOnly (method_exists ($ node , 'isReadonly ' ) && $ node ->isReadonly ());
307319 $ this ->addCommentAndAttributes ($ prop , $ node );
308320 }
@@ -320,6 +332,7 @@ private function addMethodToClass(ClassType $class, Node\Stmt\ClassMethod $node)
320332 } elseif ($ node ->isProtected ()) {
321333 $ method ->setProtected ();
322334 }
335+
323336 $ this ->setupFunction ($ method , $ node );
324337 }
325338
@@ -334,6 +347,7 @@ private function addConstantToClass(ClassType $class, Node\Stmt\ClassConst $node
334347 } elseif ($ node ->isProtected ()) {
335348 $ const ->setProtected ();
336349 }
350+
337351 $ const ->setFinal (method_exists ($ node , 'isFinal ' ) && $ node ->isFinal ());
338352 $ this ->addCommentAndAttributes ($ const , $ node );
339353 }
@@ -366,6 +380,7 @@ private function addCommentAndAttributes($element, Node $node): void
366380 $ args [] = $ value ;
367381 }
368382 }
383+
369384 $ element ->addAttribute ($ attribute ->name ->toString (), $ args );
370385 }
371386 }
@@ -387,8 +402,10 @@ private function setupFunction($function, Node\FunctionLike $node): void
387402 if ($ item ->default ) {
388403 $ param ->setDefaultValue (new Literal ($ this ->getReformattedContents ([$ item ->default ], 2 )));
389404 }
405+
390406 $ this ->addCommentAndAttributes ($ param , $ item );
391407 }
408+
392409 $ this ->addCommentAndAttributes ($ function , $ node );
393410 if ($ node ->stmts ) {
394411 $ function ->setBody ($ this ->getReformattedContents ($ node ->stmts , 2 ));
0 commit comments