Skip to content

Commit

Permalink
Extractor: keeps the first comment in the method [Closes #119]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 2, 2022
1 parent f5299fb commit 6436c65
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/PhpGenerator/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private function getReformattedContents(array $nodes, int $level): string
*/
private function prepareReplacements(array $nodes): array
{
$start = $nodes[0]->getStartFilePos();
$start = $this->getNodeStartPos($nodes[0]);
$replacements = [];
(new NodeFinder)->find($nodes, function (Node $node) use (&$replacements, $start) {
if ($node instanceof Node\Name\FullyQualified) {
Expand Down Expand Up @@ -430,7 +430,15 @@ private function toPhp(mixed $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();
}
}
2 changes: 2 additions & 0 deletions tests/PhpGenerator/expected/ClassType.from.bodies.expect
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ abstract class Class7

public function long()
{
// comment
if ($member instanceof Method) {
$s = [1, 2, 3];
}
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions tests/PhpGenerator/expected/Extractor.bodies.expect
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ abstract class Class7

function long()
{
// comment
if ($member instanceof Method) {
$s = [1, 2, 3];
}
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions tests/PhpGenerator/expected/Extractor.bodies.resolving.expect
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ abstract class Class7

function long()
{
// comment
if ($member instanceof \Abc\Method) {
$s = [1, 2, 3];
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ abstract class Class7

function long()
{
// comment
if ($member instanceof \Abc\Method) {
$s = [1, 2, 3];
}
Expand All @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions tests/PhpGenerator/expected/Extractor.expect
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ class Class1
}
};
}


function comment1()
{
/** comment */
$a = 10;
}


function comment2()
{
// comment
"bar";
}


function comment3()
{
// comment
Foo\Bar::XX;
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/PhpGenerator/fixtures/extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ function bar() {
}
};
}

function comment1()
{
/** comment */
$a = 10;
}

function comment2()
{
// comment
'bar';
}

function comment3()
{
// comment
Foo\Bar::XX;
}
}

function () {};
Expand Down

0 comments on commit 6436c65

Please sign in to comment.