mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-30 04:29:15 +01:00
Fix $node->getDocComment()
getDocComment() now returns the last comment (given that it is a doc comment). setDocComment() no longer exists, as it doesn't make sense with the comment objects anymore. getAttribute() now returns by reference, so it also works in reference contexts.
This commit is contained in:
parent
9d96dd1796
commit
a824a2aba7
@ -31,19 +31,14 @@ interface PHPParser_Node
|
||||
public function setLine($line);
|
||||
|
||||
/**
|
||||
* Gets the nearest doc comment.
|
||||
* Gets the doc comment of the node.
|
||||
*
|
||||
* @return null|string Nearest doc comment or null
|
||||
* The doc comment has to be the last comment associated with the node.
|
||||
*
|
||||
* @return null|PHPParser_Comment_Doc Doc comment object or null
|
||||
*/
|
||||
public function getDocComment();
|
||||
|
||||
/**
|
||||
* Sets the nearest doc comment.
|
||||
*
|
||||
* @param null|string $docComment Nearest doc comment or null
|
||||
*/
|
||||
public function setDocComment($docComment);
|
||||
|
||||
/**
|
||||
* Sets an attribute on a node.
|
||||
*
|
||||
|
@ -53,21 +53,24 @@ abstract class PHPParser_NodeAbstract implements PHPParser_Node, IteratorAggrega
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the nearest doc comment.
|
||||
* Gets the doc comment of the node.
|
||||
*
|
||||
* @return null|string Nearest doc comment or null
|
||||
* The doc comment has to be the last comment associated with the node.
|
||||
*
|
||||
* @return null|PHPParser_Comment_Doc Doc comment object or null
|
||||
*/
|
||||
public function getDocComment() {
|
||||
return $this->getAttribute('docComment');
|
||||
}
|
||||
$comments = $this->getAttribute('comments');
|
||||
if (!$comments) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the nearest doc comment.
|
||||
*
|
||||
* @param null|string $docComment Nearest doc comment or null
|
||||
*/
|
||||
public function setDocComment($docComment) {
|
||||
$this->setAttribute('docComment', $docComment);
|
||||
$lastComment = $comments[count($comments) - 1];
|
||||
if (!$lastComment instanceof PHPParser_Comment_Doc) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $lastComment;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -87,8 +90,12 @@ abstract class PHPParser_NodeAbstract implements PHPParser_Node, IteratorAggrega
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getAttribute($key, $default = null) {
|
||||
return array_key_exists($key, $this->attributes) ? $this->attributes[$key] : $default;
|
||||
public function &getAttribute($key, $default = null) {
|
||||
if (!array_key_exists($key, $this->attributes)) {
|
||||
return $default;
|
||||
} else {
|
||||
return $this->attributes[$key];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -4,8 +4,11 @@ class PHPParser_Tests_NodeAbstractTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testConstruct() {
|
||||
$attributes = array(
|
||||
'startLine' => 10,
|
||||
'docComment' => '/** doc comment */',
|
||||
'startLine' => 10,
|
||||
'comments' => array(
|
||||
new PHPParser_Comment('// Comment' . "\n"),
|
||||
new PHPParser_Comment_Doc('/** doc comment */'),
|
||||
),
|
||||
);
|
||||
|
||||
$node = $this->getMockForAbstractClass(
|
||||
@ -30,6 +33,17 @@ class PHPParser_Tests_NodeAbstractTest extends PHPUnit_Framework_TestCase
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testConstruct
|
||||
*/
|
||||
public function testGetDocComment(PHPParser_Node $node) {
|
||||
$this->assertEquals('/** doc comment */', $node->getDocComment());
|
||||
array_pop($node->getAttribute('comments')); // remove doc comment
|
||||
$this->assertNull($node->getDocComment());
|
||||
array_pop($node->getAttribute('comments')); // remove comment
|
||||
$this->assertNull($node->getDocComment());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testConstruct
|
||||
*/
|
||||
@ -38,10 +52,6 @@ class PHPParser_Tests_NodeAbstractTest extends PHPUnit_Framework_TestCase
|
||||
$node->setLine(15);
|
||||
$this->assertEquals(15, $node->getLine());
|
||||
|
||||
// change of doc comment
|
||||
$node->setDocComment('/** other doc comment */');
|
||||
$this->assertEquals('/** other doc comment */', $node->getDocComment());
|
||||
|
||||
// direct modification
|
||||
$node->subNode = 'newValue';
|
||||
$this->assertEquals('newValue', $node->subNode);
|
||||
|
Loading…
Reference in New Issue
Block a user