Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix switch formatting

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

Patch by @pscheit (PR #39).
  • Loading branch information...
commit df17d62b4035789bb0e9839194da50040ef405b1 1 parent ac6f221
@nikic authored
View
12 lib/PHPParser/PrettyPrinter/Zend.php
@@ -603,7 +603,12 @@ public function pStmt_Do(PHPParser_Node_Stmt_Do $node) {
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 @@ public function pStmt_Catch(PHPParser_Node_Stmt_Catch $node) {
. "\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
35 test/code/prettyPrinter/switch.test
@@ -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;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.