Skip to content

Commit 6436c65

Browse files
committed
Extractor: keeps the first comment in the method [Closes #119]
1 parent f5299fb commit 6436c65

File tree

7 files changed

+57
-2
lines changed

7 files changed

+57
-2
lines changed

src/PhpGenerator/Extractor.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private function getReformattedContents(array $nodes, int $level): string
108108
*/
109109
private function prepareReplacements(array $nodes): array
110110
{
111-
$start = $nodes[0]->getStartFilePos();
111+
$start = $this->getNodeStartPos($nodes[0]);
112112
$replacements = [];
113113
(new NodeFinder)->find($nodes, function (Node $node) use (&$replacements, $start) {
114114
if ($node instanceof Node\Name\FullyQualified) {
@@ -430,7 +430,15 @@ private function toPhp(mixed $value): string
430430

431431
private function getNodeContents(Node ...$nodes): string
432432
{
433-
$start = $nodes[0]->getStartFilePos();
433+
$start = $this->getNodeStartPos($nodes[0]);
434434
return substr($this->code, $start, end($nodes)->getEndFilePos() - $start + 1);
435435
}
436+
437+
438+
private function getNodeStartPos(Node $node): int
439+
{
440+
return ($comments = $node->getComments())
441+
? $comments[0]->getStartFilePos()
442+
: $node->getStartFilePos();
443+
}
436444
}

tests/PhpGenerator/expected/ClassType.from.bodies.expect

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ abstract class Class7
2727

2828
public function long()
2929
{
30+
// comment
3031
if ($member instanceof Method) {
3132
$s = [1, 2, 3];
3233
}
@@ -39,6 +40,7 @@ abstract class Class7
3940

4041
public function resolving($a = Abc\a\FOO, self $b = null, $c = self::FOO)
4142
{
43+
// constants
4244
echo FOO;
4345
echo \FOO;
4446
echo a\FOO;

tests/PhpGenerator/expected/Extractor.bodies.expect

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ abstract class Class7
3737

3838
function long()
3939
{
40+
// comment
4041
if ($member instanceof Method) {
4142
$s = [1, 2, 3];
4243
}
@@ -49,6 +50,7 @@ abstract class Class7
4950

5051
function resolving($a = a\FOO, self $b = null, $c = self::FOO)
5152
{
53+
// constants
5254
echo FOO;
5355
echo \FOO;
5456
echo a\FOO;

tests/PhpGenerator/expected/Extractor.bodies.resolving.expect

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ abstract class Class7
3232

3333
function long()
3434
{
35+
// comment
3536
if ($member instanceof \Abc\Method) {
3637
$s = [1, 2, 3];
3738
}
@@ -44,6 +45,7 @@ abstract class Class7
4445

4546
function resolving($a = \Abc\a\FOO, self $b = null, $c = self::FOO)
4647
{
48+
// constants
4749
echo FOO;
4850
echo \FOO;
4951
echo \Abc\a\FOO;

tests/PhpGenerator/expected/Extractor.bodies.unresolving.expect

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ abstract class Class7
3232

3333
function long()
3434
{
35+
// comment
3536
if ($member instanceof \Abc\Method) {
3637
$s = [1, 2, 3];
3738
}
@@ -44,6 +45,7 @@ abstract class Class7
4445

4546
function resolving($a = \Abc\a\FOO, self $b = null, $c = self::FOO)
4647
{
48+
// constants
4749
echo FOO;
4850
echo \FOO;
4951
echo \Abc\a\FOO;

tests/PhpGenerator/expected/Extractor.expect

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@ class Class1
99
}
1010
};
1111
}
12+
13+
14+
function comment1()
15+
{
16+
/** comment */
17+
$a = 10;
18+
}
19+
20+
21+
function comment2()
22+
{
23+
// comment
24+
"bar";
25+
}
26+
27+
28+
function comment3()
29+
{
30+
// comment
31+
Foo\Bar::XX;
32+
}
1233
}
1334

1435
/**

tests/PhpGenerator/fixtures/extractor.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@ function bar() {
88
}
99
};
1010
}
11+
12+
function comment1()
13+
{
14+
/** comment */
15+
$a = 10;
16+
}
17+
18+
function comment2()
19+
{
20+
// comment
21+
'bar';
22+
}
23+
24+
function comment3()
25+
{
26+
// comment
27+
Foo\Bar::XX;
28+
}
1129
}
1230

1331
function () {};

0 commit comments

Comments
 (0)