diff --git a/lib/PHPParser/Node.php b/lib/PHPParser/Node.php index c86a200..a2c8c48 100644 --- a/lib/PHPParser/Node.php +++ b/lib/PHPParser/Node.php @@ -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. * diff --git a/lib/PHPParser/NodeAbstract.php b/lib/PHPParser/NodeAbstract.php index c0db699..e7d0456 100644 --- a/lib/PHPParser/NodeAbstract.php +++ b/lib/PHPParser/NodeAbstract.php @@ -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]; + } } /** diff --git a/test/PHPParser/Tests/NodeAbstractTest.php b/test/PHPParser/Tests/NodeAbstractTest.php index f19ce08..767340e 100644 --- a/test/PHPParser/Tests/NodeAbstractTest.php +++ b/test/PHPParser/Tests/NodeAbstractTest.php @@ -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);