Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/parser/PrettyPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,44 @@

class PrettyPrinter extends Standard {

/**
* Pretty prints an array of nodes (statements) and indents them optionally.
*
* @param Node[] $nodes Array of nodes
* @param bool $indent Whether to indent the printed nodes
*
* @return string Pretty printed statements
*/
protected function pStmts(array $nodes, $indent = true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

travis says:

Suggested change
protected function pStmts(array $nodes, $indent = true) {
protected function pStmts(array $nodes, bool $indent = true): string {

$result = '';
$nodeBefore = NULL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$nodeBefore = NULL;
$prevNode = null;

foreach ($nodes as $node) {
$comments = $node->getAttribute('comments', array());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go modern 😉:

Suggested change
$comments = $node->getAttribute('comments', array());
$comments = $node->getAttribute('comments', []);

if ($comments) {
$result .= "\n" . $this->pComments($comments);
if ($node instanceof Stmt\Nop) {
continue;
}
}

if ($nodeBefore && $nodeBefore->getLine() && $node->getLine()) {
$diff = $node->getLine()- $nodeBefore->getLine();
if ($diff > 0) {
$result .= str_repeat("\n", $diff - 1);
}
}

$result .= "\n" . $this->p($node) . ($node instanceof Expr ? ';' : '');
$nodeBefore = $node;
}

if ($indent) {
return preg_replace('~\n(?!$|' . $this->noIndentToken . ')~', "\n ", $result);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use code profile for indentation character(s) here.

} else {
return $result;
}
}

public function pExpr_Array(Array_ $node) {
return '[' . $this->pCommaSeparated($node->items) . ']';
}
Expand Down