Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PrettyPrinter.php #62

Merged
merged 1 commit into from
May 18, 2019
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