Skip to content

Commit

Permalink
Add dumpComments option to NodeDumper
Browse files Browse the repository at this point in the history
Adding this as an option to avoid breaking people's tests.

Some of the test results show pretty clearly that we are incorrectly
assigning the same comment multiple times for nested nodes (mentioned
in #36).
  • Loading branch information
nikic committed Mar 9, 2016
1 parent 06d9ba4 commit a0c216b
Show file tree
Hide file tree
Showing 21 changed files with 285 additions and 14 deletions.
4 changes: 2 additions & 2 deletions bin/php-parse
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ if (empty($files)) {
}

$lexer = new PhpParser\Lexer\Emulative(array('usedAttributes' => array(
'startLine', 'endLine', 'startFilePos', 'endFilePos'
'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments'
)));
$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7, $lexer);
$dumper = new PhpParser\NodeDumper;
$dumper = new PhpParser\NodeDumper(['dumpComments' => true]);
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
$serializer = new PhpParser\Serializer\XML;

Expand Down
18 changes: 18 additions & 0 deletions lib/PhpParser/NodeDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@

class NodeDumper
{
private $dumpComments;

/**
* Constructs a NodeDumper.
*
* @param array $options Boolean option 'dumpComments' controls whether comments should be
* dumped
*/
public function __construct(array $options = []) {
$this->dumpComments = !empty($options['dumpComments']);
}

/**
* Dumps a node or array.
*
Expand Down Expand Up @@ -31,6 +43,10 @@ public function dump($node) {
$r .= str_replace("\n", "\n ", $this->dump($value));
}
}

if ($this->dumpComments && $comments = $node->getAttribute('comments')) {
$r .= "\n comments: " . str_replace("\n", "\n ", $this->dump($comments));
}
} elseif (is_array($node)) {
$r = 'array(';

Expand All @@ -49,6 +65,8 @@ public function dump($node) {
$r .= str_replace("\n", "\n ", $this->dump($value));
}
}
} elseif ($node instanceof Comment) {
return $node->getReformattedText();
} else {
throw new \InvalidArgumentException('Can only dump nodes and arrays.');
}
Expand Down
4 changes: 2 additions & 2 deletions test/PhpParser/CodeParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CodeParsingTest extends CodeTestAbstract
*/
public function testParse($name, $code, $expected, $mode) {
$lexer = new Lexer\Emulative(array('usedAttributes' => array(
'startLine', 'endLine', 'startFilePos', 'endFilePos'
'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments'
)));
$parser5 = new Parser\Php5($lexer, array(
'throwOnError' => false,
Expand Down Expand Up @@ -47,7 +47,7 @@ private function getParseOutput(Parser $parser, $code) {
}

if (null !== $stmts) {
$dumper = new NodeDumper;
$dumper = new NodeDumper(['dumpComments' => true]);
$output .= $dumper->dump($stmts);
}

Expand Down
3 changes: 3 additions & 0 deletions test/code/parser/expr/arrayDef.test
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ array(
5: Expr_Array(
items: array(
)
comments: array(
0: // short array syntax
)
)
6: Expr_Array(
items: array(
Expand Down
33 changes: 33 additions & 0 deletions test/code/parser/expr/assign.test
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,30 @@ array(
0: Expr_Assign(
var: Expr_Variable(
name: a
comments: array(
0: // simple assign
)
)
expr: Expr_Variable(
name: b
)
comments: array(
0: // simple assign
)
)
1: Expr_AssignOp_BitwiseAnd(
var: Expr_Variable(
name: a
comments: array(
0: // combined assign
)
)
expr: Expr_Variable(
name: b
)
comments: array(
0: // combined assign
)
)
2: Expr_AssignOp_BitwiseOr(
var: Expr_Variable(
Expand Down Expand Up @@ -144,6 +156,9 @@ array(
13: Expr_Assign(
var: Expr_Variable(
name: a
comments: array(
0: // chained assign
)
)
expr: Expr_AssignOp_Mul(
var: Expr_Variable(
Expand All @@ -158,14 +173,23 @@ array(
)
)
)
comments: array(
0: // chained assign
)
)
14: Expr_AssignRef(
var: Expr_Variable(
name: a
comments: array(
0: // by ref assign
)
)
expr: Expr_Variable(
name: b
)
comments: array(
0: // by ref assign
)
)
15: Expr_AssignRef(
var: Expr_Variable(
Expand All @@ -188,10 +212,16 @@ array(
name: a
)
)
comments: array(
0: // list() assign
)
)
expr: Expr_Variable(
name: b
)
comments: array(
0: // list() assign
)
)
17: Expr_Assign(
var: Expr_List(
Expand Down Expand Up @@ -236,6 +266,9 @@ array(
var: Expr_Variable(
name: a
)
comments: array(
0: // inc/dec
)
)
20: Expr_PostInc(
var: Expr_Variable(
Expand Down
15 changes: 15 additions & 0 deletions test/code/parser/expr/fetchAndCall/funcCall.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ array(
parts: array(
0: a
)
comments: array(
0: // function name variations
)
)
args: array(
)
comments: array(
0: // function name variations
)
)
1: Expr_FuncCall(
name: Expr_Variable(
Expand Down Expand Up @@ -106,12 +112,21 @@ array(
parts: array(
0: a
)
comments: array(
0: // array dereferencing
)
)
args: array(
)
comments: array(
0: // array dereferencing
)
)
dim: Scalar_String(
value: b
)
comments: array(
0: // array dereferencing
)
)
)
23 changes: 22 additions & 1 deletion test/code/parser/expr/fetchAndCall/objectAccess.test
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ array(
0: Expr_PropertyFetch(
var: Expr_Variable(
name: a
comments: array(
0: // property fetch variations
)
)
name: b
comments: array(
0: // property fetch variations
)
)
1: Expr_ArrayDimFetch(
var: Expr_PropertyFetch(
Expand All @@ -50,10 +56,16 @@ array(
3: Expr_MethodCall(
var: Expr_Variable(
name: a
comments: array(
0: // method call variations
)
)
name: b
args: array(
)
comments: array(
0: // method call variations
)
)
4: Expr_MethodCall(
var: Expr_Variable(
Expand Down Expand Up @@ -94,14 +106,23 @@ array(
var: Expr_MethodCall(
var: Expr_Variable(
name: a
comments: array(
0: // array dereferencing
)
)
name: b
args: array(
)
comments: array(
0: // array dereferencing
)
)
dim: Scalar_String(
value: c
)
comments: array(
0: // array dereferencing
)
)
8: Expr_ArrayDimFetch(
var: Expr_MethodCall(
Expand All @@ -116,4 +137,4 @@ array(
value: c
)
)
)
)
23 changes: 22 additions & 1 deletion test/code/parser/expr/fetchAndCall/staticCall.test
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ array(
parts: array(
0: A
)
comments: array(
0: // method name variations
)
)
name: b
args: array(
)
comments: array(
0: // method name variations
)
)
1: Expr_StaticCall(
class: Name(
Expand Down Expand Up @@ -99,24 +105,39 @@ array(
parts: array(
0: A
)
comments: array(
0: // array dereferencing
)
)
name: b
args: array(
)
comments: array(
0: // array dereferencing
)
)
dim: Scalar_String(
value: c
)
comments: array(
0: // array dereferencing
)
)
6: Expr_StaticCall(
class: Name(
parts: array(
0: static
)
comments: array(
0: // class name variations
)
)
name: b
args: array(
)
comments: array(
0: // class name variations
)
)
7: Expr_StaticCall(
class: Expr_Variable(
Expand Down Expand Up @@ -149,4 +170,4 @@ array(
args: array(
)
)
)
)
15 changes: 15 additions & 0 deletions test/code/parser/expr/fetchAndCall/staticPropertyFetch.test
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ array(
parts: array(
0: A
)
comments: array(
0: // property name variations
)
)
name: b
comments: array(
0: // property name variations
)
)
1: Expr_StaticPropertyFetch(
class: Name(
Expand Down Expand Up @@ -48,12 +54,21 @@ array(
parts: array(
0: A
)
comments: array(
0: // array access
)
)
name: b
comments: array(
0: // array access
)
)
dim: Scalar_String(
value: c
)
comments: array(
0: // array access
)
)
4: Expr_ArrayDimFetch(
var: Expr_StaticPropertyFetch(
Expand Down
Loading

0 comments on commit a0c216b

Please sign in to comment.