From e587e3f4c660cfaef9ed4c0a3e29e2d006206f00 Mon Sep 17 00:00:00 2001 From: nikic Date: Sun, 6 May 2012 17:49:04 +0200 Subject: [PATCH] Collect normal comments too, not only doc comments Comments and doc comments are now saved in the 'comments' attribute, as an array. The are instances of PHPParser_Comment[_Doc]. --- lib/PHPParser/Comment.php | 42 +++++++++++++++++++ lib/PHPParser/Comment/Doc.php | 5 +++ lib/PHPParser/Lexer.php | 8 ++-- lib/PHPParser/Serializer/XML.php | 5 +++ test/PHPParser/Tests/LexerTest.php | 25 ++++++++--- test/PHPParser/Tests/Serializer/XMLTest.php | 39 +++++++++-------- test/PHPParser/Tests/Unserializer/XMLTest.php | 2 - 7 files changed, 98 insertions(+), 28 deletions(-) create mode 100644 lib/PHPParser/Comment.php create mode 100644 lib/PHPParser/Comment/Doc.php diff --git a/lib/PHPParser/Comment.php b/lib/PHPParser/Comment.php new file mode 100644 index 0000000..01c1bdf --- /dev/null +++ b/lib/PHPParser/Comment.php @@ -0,0 +1,42 @@ +text = $text; + } + + /** + * Gets the comment text. + * + * @return string The comment text (including comment delimiters like /*) + */ + public function getText() { + return $this->text; + } + + /** + * Sets the comment text. + * + * @param string $text The comment text (including comment delimiters like /*) + */ + public function setText($text) { + $this->text = $text; + } + + /** + * Gets the comment text. + * + * @return string The comment text (including comment delimiters like /*) + */ + public function __toString() { + return $this->text; + } +} \ No newline at end of file diff --git a/lib/PHPParser/Comment/Doc.php b/lib/PHPParser/Comment/Doc.php new file mode 100644 index 0000000..95e4bc9 --- /dev/null +++ b/lib/PHPParser/Comment/Doc.php @@ -0,0 +1,5 @@ +dropTokens = array_fill_keys(array(T_WHITESPACE, T_COMMENT, T_OPEN_TAG), 1); + $this->dropTokens = array_fill_keys(array(T_WHITESPACE, T_OPEN_TAG), 1); } /** @@ -101,8 +101,10 @@ class PHPParser_Lexer } else { $this->line += substr_count($token[1], "\n"); - if (T_DOC_COMMENT === $token[0]) { - $startAttributes['docComment'] = $token[1]; + if (T_COMMENT === $token[0]) { + $startAttributes['comments'][] = new PHPParser_Comment($token[1]); + } elseif (T_DOC_COMMENT === $token[0]) { + $startAttributes['comments'][] = new PHPParser_Comment_Doc($token[1]); } elseif (!isset($this->dropTokens[$token[0]])) { $value = $token[1]; $startAttributes['startLine'] = $token[2]; diff --git a/lib/PHPParser/Serializer/XML.php b/lib/PHPParser/Serializer/XML.php index 1529e7f..548c6d7 100644 --- a/lib/PHPParser/Serializer/XML.php +++ b/lib/PHPParser/Serializer/XML.php @@ -46,6 +46,11 @@ class PHPParser_Serializer_XML implements PHPParser_Serializer $this->writer->endElement(); } + $this->writer->endElement(); + } elseif ($node instanceof PHPParser_Comment) { + $this->writer->startElement('comment'); + $this->writer->writeAttribute('isDocComment', $node instanceof PHPParser_Comment_Doc ? 'true' : 'false'); + $this->writer->text($node->getText()); $this->writer->endElement(); } elseif (is_array($node)) { $this->writer->startElement('scalar:array'); diff --git a/test/PHPParser/Tests/LexerTest.php b/test/PHPParser/Tests/LexerTest.php index 1e99333..aac158f 100644 --- a/test/PHPParser/Tests/LexerTest.php +++ b/test/PHPParser/Tests/LexerTest.php @@ -49,9 +49,9 @@ class PHPParser_Tests_LexerTest extends PHPUnit_Framework_TestCase public function provideTestLex() { return array( - // tests conversion of closing PHP tag and drop of whitespace, comments and opening tags + // tests conversion of closing PHP tag and drop of whitespace and opening tags array( - 'plaintext', + 'plaintext', array( array( PHPParser_Parser::T_STRING, 'tokens', @@ -81,17 +81,30 @@ class PHPParser_Tests_LexerTest extends PHPUnit_Framework_TestCase ), array( ord('$'), '$', - array('startLine' => 3, 'docComment' => '/** doc' . "\n" . 'comment */'), array('endLine' => 3) + array( + 'startLine' => 3, + 'comments' => array(new PHPParser_Comment_Doc('/** doc' . "\n" . 'comment */')) + ), + array('endLine' => 3) ), ) ), - // tests doccomment extraction + // tests comment extraction array( - ' 1, 'docComment' => '/** docComment 2 */'), array('endLine' => 1) + array( + 'startLine' => 2, + 'comments' => array( + new PHPParser_Comment('/* comment */'), + new PHPParser_Comment('// comment' . "\n"), + new PHPParser_Comment_Doc('/** docComment 1 */'), + new PHPParser_Comment_Doc('/** docComment 2 */'), + ), + ), + array('endLine' => 2) ), ) ), diff --git a/test/PHPParser/Tests/Serializer/XMLTest.php b/test/PHPParser/Tests/Serializer/XMLTest.php index c05d71a..3ae6cf0 100644 --- a/test/PHPParser/Tests/Serializer/XMLTest.php +++ b/test/PHPParser/Tests/Serializer/XMLTest.php @@ -8,6 +8,7 @@ class PHPParser_Tests_Serializer_XMLTest extends PHPUnit_Framework_TestCase public function testSerialize() { $code = << - - /** doc comment */ - + + + // comment + + /** doc comment */ + + - 3 + 4 - 5 + 6 @@ -34,10 +39,10 @@ CODE; - 3 + 4 - 3 + 4 a @@ -45,10 +50,10 @@ CODE; - 3 + 4 - 3 + 4 0 @@ -64,10 +69,10 @@ CODE; - 3 + 4 - 3 + 4 b @@ -75,10 +80,10 @@ CODE; - 3 + 4 - 3 + 4 1 @@ -98,19 +103,19 @@ CODE; - 4 + 5 - 4 + 5 - 4 + 5 - 4 + 5 Foo diff --git a/test/PHPParser/Tests/Unserializer/XMLTest.php b/test/PHPParser/Tests/Unserializer/XMLTest.php index 05c5c03..7efe4fb 100644 --- a/test/PHPParser/Tests/Unserializer/XMLTest.php +++ b/test/PHPParser/Tests/Unserializer/XMLTest.php @@ -39,8 +39,6 @@ XML; XML; $unserializer = new PHPParser_Unserializer_XML; - var_dump($unserializer->unserialize($xml)); - $this->assertEquals( new PHPParser_Node_Scalar_ClassConst,