Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7100868

Browse files
committed
Extractor: keeps the first comment in the method [Closes #119]
1 parent 01bb9ed commit 7100868

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
@@ -99,7 +99,7 @@ private function getReformattedContents(array $statements, int $level): string
9999

100100
private function prepareReplacements(array $statements): array
101101
{
102-
$start = $statements[0]->getStartFilePos();
102+
$start = $this->getNodeStartPos($statements[0]);
103103
$replacements = [];
104104
(new NodeFinder)->find($statements, function (Node $node) use (&$replacements, $start) {
105105
if ($node instanceof Node\Name\FullyQualified) {
@@ -430,7 +430,15 @@ private function toPhp($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)

AltStyle によって変換されたページ (->オリジナル) /