1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-30 04:19:30 +01:00

Add Node::getComments() method

This commit is contained in:
Nikita Popov 2017-09-29 17:09:16 +02:00
parent df334eacaa
commit d8f9173390
6 changed files with 31 additions and 6 deletions

View File

@ -25,6 +25,15 @@ interface Node
*/
public function getLine() : int;
/**
* Gets all comments directly preceding this node.
*
* The comments are also available through the "comments" attribute.
*
* @return Comment[]
*/
public function getComments(): array;
/**
* Gets the doc comment of the node.
*

View File

@ -33,6 +33,17 @@ abstract class NodeAbstract implements Node, \JsonSerializable
return $this->getAttribute('startLine', -1);
}
/**
* Gets all comments directly preceding this node.
*
* The comments are also available through the "comments" attribute.
*
* @return Comment[]
*/
public function getComments(): array {
return $this->attributes['comments'] ?? [];
}
/**
* Gets the doc comment of the node.
*
@ -41,7 +52,7 @@ abstract class NodeAbstract implements Node, \JsonSerializable
* @return null|Comment\Doc Doc comment object or null
*/
public function getDocComment() {
$comments = $this->getAttribute('comments');
$comments = $this->getComments();
if (!$comments) {
return null;
}
@ -62,7 +73,7 @@ abstract class NodeAbstract implements Node, \JsonSerializable
* @param Comment\Doc $docComment Doc comment to set
*/
public function setDocComment(Comment\Doc $docComment) {
$comments = $this->getAttribute('comments', []);
$comments = $this->getComments();
$numComments = count($comments);
if ($numComments > 0 && $comments[$numComments - 1] instanceof Comment\Doc) {

View File

@ -78,7 +78,7 @@ class NodeDumper
}
}
if ($this->dumpComments && $comments = $node->getAttribute('comments')) {
if ($this->dumpComments && $comments = $node->getComments()) {
$r .= "\n comments: " . str_replace("\n", "\n ", $this->dumpRecursive($comments));
}
} elseif (is_array($node)) {

View File

@ -939,9 +939,13 @@ class Standard extends PrettyPrinterAbstract
}
}
/**
* @param Node[] $nodes
* @return bool
*/
private function hasNodeWithComments(array $nodes) {
foreach ($nodes as $node) {
if ($node && $node->getAttribute('comments')) {
if ($node && $node->getComments()) {
return true;
}
}

View File

@ -249,7 +249,7 @@ abstract class PrettyPrinterAbstract
$result = '';
foreach ($nodes as $node) {
$comments = $node->getAttribute('comments', []);
$comments = $node->getComments();
if ($comments) {
$result .= $this->nl . $this->pComments($comments);
if ($node instanceof Stmt\Nop) {
@ -388,7 +388,7 @@ abstract class PrettyPrinterAbstract
$lastIdx = count($nodes) - 1;
foreach ($nodes as $idx => $node) {
if ($node !== null) {
$comments = $node->getAttribute('comments', []);
$comments = $node->getComments();
if ($comments) {
$result .= $this->nl . $this->pComments($comments);
}

View File

@ -57,6 +57,7 @@ class NodeAbstractTest extends TestCase
$this->assertTrue(isset($node->subNode2));
$this->assertFalse(isset($node->subNode3));
$this->assertSame($attributes, $node->getAttributes());
$this->assertSame($attributes['comments'], $node->getComments());
return $node;
}