Skip to content

Commit ea47b6e

Browse files
committed
Add NodeAbstract::setDocComment()
1 parent 9e1c535 commit ea47b6e

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
Version 3.0.0-dev
22
-----------------
33

4-
Nothing yet.
4+
### Added
5+
6+
* Added `NodeAbstract::setDocComment()` method.
57

68
Version 3.0.0-beta1 (2016-09-16)
79
--------------------------------
10+
811
### Added
912

1013
* [7.1] Function/method and parameter builders now support PHP 7.1 type hints (void, iterable and
@@ -20,7 +23,7 @@ Version 3.0.0-beta1 (2016-09-16)
2023
The following changes are also part of PHP-Parser 2.1.1:
2124

2225
* The PHP 7 parser will now generate a parse error for `$var =& new Obj` assignments.
23-
* Comments on free-standing code blocks will no be retained as comments on the first statement in
26+
* Comments on free-standing code blocks will now be retained as comments on the first statement in
2427
the code block.
2528

2629
Version 3.0.0-alpha1 (2016-07-25)

lib/PhpParser/NodeAbstract.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ public function getDocComment() {
6363
return $lastComment;
6464
}
6565

66+
/**
67+
* Sets the doc comment of the node.
68+
*
69+
* This will either replace an existing doc comment or add it to the comments array.
70+
*
71+
* @param Comment\Doc $docComment Doc comment to set
72+
*/
73+
public function setDocComment(Comment\Doc $docComment) {
74+
$comments = $this->getAttribute('comments', []);
75+
76+
$numComments = count($comments);
77+
if ($numComments > 0 && $comments[$numComments - 1] instanceof Comment\Doc) {
78+
// Replace existing doc comment
79+
$comments[$numComments - 1] = $docComment;
80+
} else {
81+
// Append new comment
82+
$comments[] = $docComment;
83+
}
84+
85+
$this->setAttribute('comments', $comments);
86+
}
87+
6688
public function setAttribute($key, $value) {
6789
$this->attributes[$key] = $value;
6890
}

test/PhpParser/NodeAbstractTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@ public function testGetDocComment(array $attributes, Node $node) {
7070
$this->assertNull($node->getDocComment());
7171
}
7272

73+
public function testSetDocComment() {
74+
$node = new DummyNode(null, null, []);
75+
76+
// Add doc comment to node without comments
77+
$docComment = new Comment\Doc('/** doc */');
78+
$node->setDocComment($docComment);
79+
$this->assertSame($docComment, $node->getDocComment());
80+
81+
// Replace it
82+
$docComment = new Comment\Doc('/** doc 2 */');
83+
$node->setDocComment($docComment);
84+
$this->assertSame($docComment, $node->getDocComment());
85+
86+
// Add docmment to node with other comments
87+
$c1 = new Comment('/* foo */');
88+
$c2 = new Comment('/* bar */');
89+
$docComment = new Comment\Doc('/** baz */');
90+
$node->setAttribute('comments', [$c1, $c2]);
91+
$node->setDocComment($docComment);
92+
$this->assertSame([$c1, $c2, $docComment], $node->getAttribute('comments'));
93+
}
94+
7395
/**
7496
* @dataProvider provideNodes
7597
*/

0 commit comments

Comments
 (0)