From df17d62b4035789bb0e9839194da50040ef405b1 Mon Sep 17 00:00:00 2001 From: nikic Date: Wed, 31 Oct 2012 17:46:48 +0100 Subject: [PATCH] Fix switch formatting The switch cases were not indented and fall-through cases had an unnecessary additional newline. Patch by @pscheit (PR #39). --- lib/PHPParser/PrettyPrinter/Zend.php | 12 +++++----- test/code/prettyPrinter/switch.test | 35 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 test/code/prettyPrinter/switch.test diff --git a/lib/PHPParser/PrettyPrinter/Zend.php b/lib/PHPParser/PrettyPrinter/Zend.php index aada95d..e5e72f6 100644 --- a/lib/PHPParser/PrettyPrinter/Zend.php +++ b/lib/PHPParser/PrettyPrinter/Zend.php @@ -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) : '') . ';'; } diff --git a/test/code/prettyPrinter/switch.test b/test/code/prettyPrinter/switch.test new file mode 100644 index 0000000..0733a48 --- /dev/null +++ b/test/code/prettyPrinter/switch.test @@ -0,0 +1,35 @@ +switch/case/default +----- +