From 7100868cb18f296b8cbfbe4ad40212678b87f497 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 4 Oct 2022 13:22:53 +0200 Subject: [PATCH] Extractor: keeps the first comment in the method [Closes #119] --- src/PhpGenerator/Extractor.php | 12 +++++++++-- .../expected/ClassType.from.bodies.expect | 2 ++ .../expected/Extractor.bodies.expect | 2 ++ .../Extractor.bodies.resolving.expect | 2 ++ .../Extractor.bodies.unresolving.expect | 2 ++ tests/PhpGenerator/expected/Extractor.expect | 21 +++++++++++++++++++ tests/PhpGenerator/fixtures/extractor.php | 18 ++++++++++++++++ 7 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/PhpGenerator/Extractor.php b/src/PhpGenerator/Extractor.php index c3b0daa5..d7eba32f 100644 --- a/src/PhpGenerator/Extractor.php +++ b/src/PhpGenerator/Extractor.php @@ -99,7 +99,7 @@ private function getReformattedContents(array $statements, int $level): string private function prepareReplacements(array $statements): array { - $start = $statements[0]->getStartFilePos(); + $start = $this->getNodeStartPos($statements[0]); $replacements = []; (new NodeFinder)->find($statements, function (Node $node) use (&$replacements, $start) { if ($node instanceof Node\Name\FullyQualified) { @@ -430,7 +430,15 @@ private function toPhp($value): string private function getNodeContents(Node ...$nodes): string { - $start = $nodes[0]->getStartFilePos(); + $start = $this->getNodeStartPos($nodes[0]); return substr($this->code, $start, end($nodes)->getEndFilePos() - $start + 1); } + + + private function getNodeStartPos(Node $node): int + { + return ($comments = $node->getComments()) + ? $comments[0]->getStartFilePos() + : $node->getStartFilePos(); + } } diff --git a/tests/PhpGenerator/expected/ClassType.from.bodies.expect b/tests/PhpGenerator/expected/ClassType.from.bodies.expect index 1ce8fe8f..a251549e 100644 --- a/tests/PhpGenerator/expected/ClassType.from.bodies.expect +++ b/tests/PhpGenerator/expected/ClassType.from.bodies.expect @@ -27,6 +27,7 @@ abstract class Class7 public function long() { + // comment if ($member instanceof Method) { $s = [1, 2, 3]; } @@ -39,6 +40,7 @@ abstract class Class7 public function resolving($a = Abc\a\FOO, self $b = null, $c = self::FOO) { + // constants echo FOO; echo \FOO; echo a\FOO; diff --git a/tests/PhpGenerator/expected/Extractor.bodies.expect b/tests/PhpGenerator/expected/Extractor.bodies.expect index 21d22313..6e321ade 100644 --- a/tests/PhpGenerator/expected/Extractor.bodies.expect +++ b/tests/PhpGenerator/expected/Extractor.bodies.expect @@ -37,6 +37,7 @@ abstract class Class7 function long() { + // comment if ($member instanceof Method) { $s = [1, 2, 3]; } @@ -49,6 +50,7 @@ abstract class Class7 function resolving($a = a\FOO, self $b = null, $c = self::FOO) { + // constants echo FOO; echo \FOO; echo a\FOO; diff --git a/tests/PhpGenerator/expected/Extractor.bodies.resolving.expect b/tests/PhpGenerator/expected/Extractor.bodies.resolving.expect index efe51b99..cb64aca7 100644 --- a/tests/PhpGenerator/expected/Extractor.bodies.resolving.expect +++ b/tests/PhpGenerator/expected/Extractor.bodies.resolving.expect @@ -32,6 +32,7 @@ abstract class Class7 function long() { + // comment if ($member instanceof \Abc\Method) { $s = [1, 2, 3]; } @@ -44,6 +45,7 @@ abstract class Class7 function resolving($a = \Abc\a\FOO, self $b = null, $c = self::FOO) { + // constants echo FOO; echo \FOO; echo \Abc\a\FOO; diff --git a/tests/PhpGenerator/expected/Extractor.bodies.unresolving.expect b/tests/PhpGenerator/expected/Extractor.bodies.unresolving.expect index 8d8a774c..6ae6f85c 100644 --- a/tests/PhpGenerator/expected/Extractor.bodies.unresolving.expect +++ b/tests/PhpGenerator/expected/Extractor.bodies.unresolving.expect @@ -32,6 +32,7 @@ abstract class Class7 function long() { + // comment if ($member instanceof \Abc\Method) { $s = [1, 2, 3]; } @@ -44,6 +45,7 @@ abstract class Class7 function resolving($a = \Abc\a\FOO, self $b = null, $c = self::FOO) { + // constants echo FOO; echo \FOO; echo \Abc\a\FOO; diff --git a/tests/PhpGenerator/expected/Extractor.expect b/tests/PhpGenerator/expected/Extractor.expect index dc56a7e3..8b3afc30 100644 --- a/tests/PhpGenerator/expected/Extractor.expect +++ b/tests/PhpGenerator/expected/Extractor.expect @@ -9,6 +9,27 @@ class Class1 } }; } + + + function comment1() + { + /** comment */ + $a = 10; + } + + + function comment2() + { + // comment + "bar"; + } + + + function comment3() + { + // comment + Foo\Bar::XX; + } } /** diff --git a/tests/PhpGenerator/fixtures/extractor.php b/tests/PhpGenerator/fixtures/extractor.php index 2566fac7..fafe8004 100644 --- a/tests/PhpGenerator/fixtures/extractor.php +++ b/tests/PhpGenerator/fixtures/extractor.php @@ -8,6 +8,24 @@ function bar() { } }; } + + function comment1() + { + /** comment */ + $a = 10; + } + + function comment2() + { + // comment + 'bar'; + } + + function comment3() + { + // comment + Foo\Bar::XX; + } } function () {};