Permalink
Browse files

Separated cast operators to allow for PSR compliant output

  • Loading branch information...
1 parent a2b7f43 commit 8e767e39594ef0d4291698a6041e40159cc3d6a8 @jmalloc jmalloc committed Jan 14, 2013
Showing with 42 additions and 12 deletions.
  1. +12 −6 lib/Icecave/Rasta/Renderer.php
  2. +30 −6 test/suite/Icecave/Rasta/RendererTest.php
@@ -574,32 +574,32 @@ public function visitBitwiseOrAssign(BitwiseOrAssign $node)
public function visitCastArray(CastArray $node)
{
- return $this->generateUnaryOperatorCode('(array)', $node);
+ return $this->generateCastOperatorCode('array', $node);
}
public function visitCastBoolean(CastBoolean $node)
{
- return $this->generateUnaryOperatorCode('(boolean)', $node);
+ return $this->generateCastOperatorCode('boolean', $node);
}
public function visitCastFloat(CastFloat $node)
{
- return $this->generateUnaryOperatorCode('(float)', $node);
+ return $this->generateCastOperatorCode('float', $node);
}
public function visitCastInteger(CastInteger $node)
{
- return $this->generateUnaryOperatorCode('(integer)', $node);
+ return $this->generateCastOperatorCode('integer', $node);
}
public function visitCastObject(CastObject $node)
{
- return $this->generateUnaryOperatorCode('(object)', $node);
+ return $this->generateCastOperatorCode('object', $node);
}
public function visitCastString(CastString $node)
{
- return $this->generateUnaryOperatorCode('(string)', $node);
+ return $this->generateCastOperatorCode('string', $node);
}
public function visitCloneOperator(CloneOperator $node)
@@ -837,6 +837,11 @@ protected function visitChildren(Node $node)
return $code;
}
+ protected function generateCastOperatorCode($type, UnaryOperator $node)
+ {
+ return '(' . $type . ') ' . $node->expression()->accept($this);
+ }
+
protected function generateUnaryOperatorCode($operator, UnaryOperator $node, $prefix = true)
{
if ($prefix) {
@@ -845,6 +850,7 @@ protected function generateUnaryOperatorCode($operator, UnaryOperator $node, $pr
return '(' . $node->expression()->accept($this) . $operator . ')';
}
+
protected function generateBinaryOperatorCode($operator, BinaryOperator $node)
{
return '(' . $node->leftExpression()->accept($this) . ' ' . $operator . ' ' . $node->rightExpression()->accept($this) . ')';
@@ -870,6 +870,24 @@ public function testBinaryOperators($operator, $symbol)
}
/**
+ * @dataProvider getCastOperatorData
+ */
+ public function testCastOperators($operator, $type)
+ {
+ $reflector = new ReflectionClass('Icecave\Pasta\AST\Expr\\' . $operator);
+ $node = $reflector->newInstance(
+ new Variable(new Identifier('value'))
+ );
+
+ $this->assertInstanceOf('Icecave\Pasta\AST\Expr\UnaryOperator', $node);
+
+ $code = $node->accept($this->_renderer);
+
+ $expected = '(' . $type . ') $value';
+ $this->assertSame($expected, $code);
+ }
+
+ /**
* @dataProvider getPrefixUnaryOperatorData
*/
public function testPrefixUnaryOperators($operator, $symbol)
@@ -980,16 +998,22 @@ public function getBinaryOperatorData()
);
}
+ public function getCastOperatorData()
+ {
+ return array(
+ array('CastArray', 'array'),
+ array('CastBoolean', 'boolean'),
+ array('CastFloat', 'float'),
+ array('CastInteger', 'integer'),
+ array('CastObject', 'object'),
+ array('CastString', 'string'),
+ );
+ }
+
public function getPrefixUnaryOperatorData()
{
return array(
array('BitwiseNot', '~'),
- array('CastArray', '(array)'),
- array('CastBoolean', '(boolean)'),
- array('CastFloat', '(float)'),
- array('CastInteger', '(integer)'),
- array('CastObject', '(object)'),
- array('CastString', '(string)'),
array('CloneOperator', 'clone '),
array('LogicalNot', '!'),
array('NewOperator', 'new '),

0 comments on commit 8e767e3

Please sign in to comment.