1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-26 20:04:48 +01:00

Fix switch formatting

The switch cases were not indented and fall-through cases had an
unnecessary additional newline.

Patch by @pscheit (PR #39).
This commit is contained in:
nikic 2012-10-31 17:46:48 +01:00
parent ac6f221c50
commit df17d62b40
2 changed files with 41 additions and 6 deletions

View File

@ -603,7 +603,12 @@ class PHPParser_PrettyPrinter_Zend extends PHPParser_PrettyPrinterAbstract
public function pStmt_Switch(PHPParser_Node_Stmt_Switch $node) {
return 'switch (' . $this->p($node->cond) . ') {'
. "\n" . $this->pImplode($node->cases) . '}';
. "\n" . $this->pStmts($node->cases) . "\n" . '}';
}
public function pStmt_Case(PHPParser_Node_Stmt_Case $node) {
return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':'
. ($node->stmts ? "\n" . $this->pStmts($node->stmts) : '');
}
public function pStmt_TryCatch(PHPParser_Node_Stmt_TryCatch $node) {
@ -619,11 +624,6 @@ class PHPParser_PrettyPrinter_Zend extends PHPParser_PrettyPrinterAbstract
. "\n" . $this->pStmts($node->stmts) . "\n" . '}';
}
public function pStmt_Case(PHPParser_Node_Stmt_Case $node) {
return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':'
. "\n" . $this->pStmts($node->stmts) . "\n";
}
public function pStmt_Break(PHPParser_Node_Stmt_Break $node) {
return 'break' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
}

View File

@ -0,0 +1,35 @@
switch/case/default
-----
<?php
switch ($expr) {
case 0:
echo 'First case, with a break';
break;
case 1:
echo 'Second case, which falls through';
case 2:
case 3:
case 4:
echo 'Third case, return instead of break';
return;
default:
echo 'Default case';
break;
}
-----
switch ($expr) {
case 0:
echo 'First case, with a break';
break;
case 1:
echo 'Second case, which falls through';
case 2:
case 3:
case 4:
echo 'Third case, return instead of break';
return;
default:
echo 'Default case';
break;
}