This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Function, class, and control statement braces now PSR compliant (#10)

  • Loading branch information...
1 parent ab028f8 commit 087203c4d21cdcbc7da721d9dc682324bb5fcbe2 @jmalloc jmalloc committed Jan 14, 2013
Showing with 58 additions and 57 deletions.
  1. +38 −22 lib/Icecave/Rasta/Renderer.php
  2. +20 −35 test/suite/Icecave/Rasta/RendererTest.php
@@ -205,9 +205,9 @@ public function visitContinueStatement(ContinueStatement $node)
public function visitDoWhileStatement(DoWhileStatement $node)
{
- $code = $this->indent('do' . PHP_EOL);
+ $code = $this->indent('do ');
$code .= $this->generateStatementCode($node->statement());
- $code .= $this->indent('while (' . $node->conditionExpression()->accept($this) . ');' . PHP_EOL);
+ $code .= ' while (' . $node->conditionExpression()->accept($this) . ');' . PHP_EOL;
return $code;
}
@@ -224,8 +224,9 @@ public function visitForeachStatement(ForeachStatement $node)
} else {
$unpack = $node->valueParameter()->accept($this);
}
- $code = $this->indent('foreach (' . $node->sequence()->accept($this) . ' as ' . $unpack . ')' . PHP_EOL);
+ $code = $this->indent('foreach (' . $node->sequence()->accept($this) . ' as ' . $unpack . ') ');
$code .= $this->generateStatementCode($node->statement());
+ $code .= PHP_EOL;
return $code;
}
@@ -248,24 +249,28 @@ public function visitForStatement(ForStatement $node)
$iter = ' ' . $expression->accept($this);
}
- $code = $this->indent('for (' . $init . ';' . $cond . ';' . $iter . ')' . PHP_EOL);
+ $code = $this->indent('for (' . $init . ';' . $cond . ';' . $iter . ') ');
$code .= $this->generateStatementCode($node->statement());
+ $code .= PHP_EOL;
return $code;
}
public function visitIfStatement(IfStatement $node)
{
- $code = $this->indent('if (' . $node->condition()->accept($this) . ')' . PHP_EOL);
+ $code = $this->indent('if (' . $node->condition()->accept($this) . ') ');
$code .= $this->generateStatementCode($node->trueBranch());
$statement = $node->falseBranch();
if ($statement instanceof IfStatement) {
- $code .= $this->indent('else');
+ $code .= ' else';
$code .= ltrim($statement->accept($this));
} elseif ($statement) {
- $code .= $this->indent('else' . PHP_EOL);
+ $code .= ' else ';
$code .= $this->generateStatementCode($statement);
+ $code .= PHP_EOL;
+ } else {
+ $code .= PHP_EOL;
}
return $code;
@@ -286,13 +291,25 @@ public function visitReturnStatement(ReturnStatement $node)
return $this->indent('return' . $expression . ';' . PHP_EOL);
}
- public function visitStatementBlock(StatementBlock $node)
+ public function visitStatementBlock(StatementBlock $node, $isStandalone = true)
{
- $code = $this->indent('{' . PHP_EOL);
- $this->startIndent();
- $code .= $this->visitChildren($node);
- $this->endIndent();
- $code .= $this->indent('}' . PHP_EOL);
+ if ($isStandalone) {
+ $code = $this->indent('{' . PHP_EOL);
+ } else {
+ $code = '{' . PHP_EOL;
+ }
+
+ if ($node->children()) {
+ $this->startIndent();
+ $code .= $this->visitChildren($node);
+ $this->endIndent();
+ }
+
+ if ($isStandalone) {
+ $code .= $this->indent('}' . PHP_EOL);
+ } else {
+ $code .= $this->indent('}');
+ }
return $code;
}
@@ -315,8 +332,9 @@ public function visitUseStatement(UseStatement $node)
public function visitWhileStatement(WhileStatement $node)
{
- $code = $this->indent('while (' . $node->conditionExpression()->accept($this) . ')' . PHP_EOL);
+ $code = $this->indent('while (' . $node->conditionExpression()->accept($this) . ') ');
$code .= $this->generateStatementCode($node->statement());
+ $code .= PHP_EOL;
return $code;
}
@@ -881,17 +899,15 @@ protected function generatePolyadicOperatorCode($operator, PolyadicOperator $nod
return '(' . implode(' ' . $operator . ' ', $expressions) . ')';
}
- protected function generateStatementCode(IStatement $node)
+ protected function generateStatementCode(IStatement $node, $addBraces = true)
{
- if ($node instanceof StatementBlock) {
- return $node->accept($this);
+ if ($addBraces && !$node instanceof StatementBlock) {
+ $n = new StatementBlock;
+ $n->add($node);
+ $node = $n;
}
- $this->startIndent();
- $code = $node->accept($this);
- $this->endIndent();
-
- return $code;
+ return $this->visitStatementBlock($node, false);
}
const MAXIMUM_INLINE_ARITY = 3;
@@ -171,10 +171,8 @@ public function testVisitDoWhileStatement()
);
$code = $node->accept($this->_renderer);
- $expected = 'do' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
- $expected .= '}' . PHP_EOL;
- $expected .= 'while (true);' . PHP_EOL;
+ $expected = 'do {' . PHP_EOL;
+ $expected .= '} while (true);' . PHP_EOL;
$this->assertSame($expected, $code);
}
@@ -195,8 +193,7 @@ public function testVisitForeachStatement()
);
$code = $node->accept($this->_renderer);
- $expected = 'foreach ($thing as $theKey => $theValue)' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
+ $expected = 'foreach ($thing as $theKey => $theValue) {' . PHP_EOL;
$expected .= '}' . PHP_EOL;
$this->assertSame($expected, $code);
}
@@ -211,8 +208,7 @@ public function testVisitForeachStatementWithoutKey()
);
$code = $node->accept($this->_renderer);
- $expected = 'foreach ($thing as $theValue)' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
+ $expected = 'foreach ($thing as $theValue) {' . PHP_EOL;
$expected .= '}' . PHP_EOL;
$this->assertSame($expected, $code);
}
@@ -227,8 +223,7 @@ public function testVisitForStatement()
);
$code = $node->accept($this->_renderer);
- $expected = 'for (1; 2; 3)' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
+ $expected = 'for (1; 2; 3) {' . PHP_EOL;
$expected .= '}' . PHP_EOL;
$this->assertSame($expected, $code);
}
@@ -244,8 +239,7 @@ public function testVisitForStatementWithOnlyInitializeExpression()
);
$code = $node->accept($this->_renderer);
- $expected = 'for (1;;)' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
+ $expected = 'for (1;;) {' . PHP_EOL;
$expected .= '}' . PHP_EOL;
$this->assertSame($expected, $code);
}
@@ -261,8 +255,7 @@ public function testVisitForStatementWithOnlyConditionExpression()
);
$code = $node->accept($this->_renderer);
- $expected = 'for (; 2;)' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
+ $expected = 'for (; 2;) {' . PHP_EOL;
$expected .= '}' . PHP_EOL;
$this->assertSame($expected, $code);
}
@@ -278,8 +271,7 @@ public function testVisitForStatementWithOnlyIterationExpression()
);
$code = $node->accept($this->_renderer);
- $expected = 'for (;; 3)' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
+ $expected = 'for (;; 3) {' . PHP_EOL;
$expected .= '}' . PHP_EOL;
$this->assertSame($expected, $code);
}
@@ -295,8 +287,7 @@ public function testVisitForStatementWithNoExpressions()
);
$code = $node->accept($this->_renderer);
- $expected = 'for (;;)' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
+ $expected = 'for (;;) {' . PHP_EOL;
$expected .= '}' . PHP_EOL;
$this->assertSame($expected, $code);
}
@@ -306,8 +297,7 @@ public function testVisitIfStatement()
$node = new IfStatement(new Literal(true));
$code = $node->accept($this->_renderer);
- $expected = 'if (true)' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
+ $expected = 'if (true) {' . PHP_EOL;
$expected .= '}' . PHP_EOL;
$this->assertSame($expected, $code);
}
@@ -321,11 +311,8 @@ public function testVisitIfStatementElse()
);
$code = $node->accept($this->_renderer);
- $expected = 'if (true)' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
- $expected .= '}' . PHP_EOL;
- $expected .= 'else' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
+ $expected = 'if (true) {' . PHP_EOL;
+ $expected .= '} else {' . PHP_EOL;
$expected .= '}' . PHP_EOL;
$this->assertSame($expected, $code);
}
@@ -339,11 +326,8 @@ public function testVisitIfStatementElseIf()
);
$code = $node->accept($this->_renderer);
- $expected = 'if (true)' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
- $expected .= '}' . PHP_EOL;
- $expected .= 'elseif (false)' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
+ $expected = 'if (true) {' . PHP_EOL;
+ $expected .= '} elseif (false) {' . PHP_EOL;
$expected .= '}' . PHP_EOL;
$this->assertSame($expected, $code);
}
@@ -356,8 +340,9 @@ public function testVisitIfStatementSingleStatement()
);
$code = $node->accept($this->_renderer);
- $expected = 'if (true)' . PHP_EOL;
+ $expected = 'if (true) {' . PHP_EOL;
$expected .= ' foo();' . PHP_EOL;
+ $expected .= '}' . PHP_EOL;
$this->assertSame($expected, $code);
}
@@ -370,10 +355,11 @@ public function testVisitIfStatementElseSingleStatement()
);
$code = $node->accept($this->_renderer);
- $expected = 'if (true)' . PHP_EOL;
+ $expected = 'if (true) {' . PHP_EOL;
$expected .= ' foo();' . PHP_EOL;
- $expected .= 'else' . PHP_EOL;
+ $expected .= '} else {' . PHP_EOL;
$expected .= ' bar();' . PHP_EOL;
+ $expected .= '}' . PHP_EOL;
$this->assertSame($expected, $code);
}
@@ -464,8 +450,7 @@ public function testVisitWhileStatement()
);
$code = $node->accept($this->_renderer);
- $expected = 'while (true)' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
+ $expected = 'while (true) {' . PHP_EOL;
$expected .= '}' . PHP_EOL;
$this->assertSame($expected, $code);
}

0 comments on commit 087203c

Please sign in to comment.