mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-11-27 04:14:44 +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);
|
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();
|
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.
|
* 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() {
|
public function getDocComment() {
|
||||||
return $this->getAttribute('docComment');
|
$comments = $this->getAttribute('comments');
|
||||||
}
|
if (!$comments) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
$lastComment = $comments[count($comments) - 1];
|
||||||
* Sets the nearest doc comment.
|
if (!$lastComment instanceof PHPParser_Comment_Doc) {
|
||||||
*
|
return null;
|
||||||
* @param null|string $docComment Nearest doc comment or null
|
}
|
||||||
*/
|
|
||||||
public function setDocComment($docComment) {
|
return $lastComment;
|
||||||
$this->setAttribute('docComment', $docComment);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -87,8 +90,12 @@ abstract class PHPParser_NodeAbstract implements PHPParser_Node, IteratorAggrega
|
|||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
public function getAttribute($key, $default = null) {
|
public function &getAttribute($key, $default = null) {
|
||||||
return array_key_exists($key, $this->attributes) ? $this->attributes[$key] : $default;
|
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() {
|
public function testConstruct() {
|
||||||
$attributes = array(
|
$attributes = array(
|
||||||
'startLine' => 10,
|
'startLine' => 10,
|
||||||
'docComment' => '/** doc comment */',
|
'comments' => array(
|
||||||
|
new PHPParser_Comment('// Comment' . "\n"),
|
||||||
|
new PHPParser_Comment_Doc('/** doc comment */'),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
$node = $this->getMockForAbstractClass(
|
$node = $this->getMockForAbstractClass(
|
||||||
@ -30,6 +33,17 @@ class PHPParser_Tests_NodeAbstractTest extends PHPUnit_Framework_TestCase
|
|||||||
return $node;
|
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
|
* @depends testConstruct
|
||||||
*/
|
*/
|
||||||
@ -38,10 +52,6 @@ class PHPParser_Tests_NodeAbstractTest extends PHPUnit_Framework_TestCase
|
|||||||
$node->setLine(15);
|
$node->setLine(15);
|
||||||
$this->assertEquals(15, $node->getLine());
|
$this->assertEquals(15, $node->getLine());
|
||||||
|
|
||||||
// change of doc comment
|
|
||||||
$node->setDocComment('/** other doc comment */');
|
|
||||||
$this->assertEquals('/** other doc comment */', $node->getDocComment());
|
|
||||||
|
|
||||||
// direct modification
|
// direct modification
|
||||||
$node->subNode = 'newValue';
|
$node->subNode = 'newValue';
|
||||||
$this->assertEquals('newValue', $node->subNode);
|
$this->assertEquals('newValue', $node->subNode);
|
||||||
|
Loading…
Reference in New Issue
Block a user