Permalink
Browse files

Improved inlining of function call parameters.

  • Loading branch information...
1 parent 42cf0ad commit 009cc966975f938fa265a9da3a29cc77ad8d6c75 @jmalloc jmalloc committed Aug 20, 2012
Showing with 50 additions and 37 deletions.
  1. +20 −11 lib/Icecave/Rasta/Renderer.php
  2. +30 −26 test/suite/Icecave/Rasta/RendererTest.php
@@ -372,20 +372,27 @@ public function visitCall(Call $node)
{
$arguments = $node->children();
$arity = count($arguments);
+ $inline = $arity <= self::MAXIMUM_INLINE_ARITY;
- if ($arity === 0) {
- $code = $node->name()->accept($this) . '()';
- } else {
- $argumentCode = '';
- for ($index = 0; $index < $arity; ++$index) {
- $argument = $arguments[$index]->accept($this);
- if ($index < $arity - 1) {
- $argument .= ',';
- }
- $argumentCode .= $argument . PHP_EOL;
+ $renderedArguments = array();
+ foreach ($arguments as $argument) {
+ $code = $argument->accept($this);
+
+ // Switch to multiline if any of the arguments contain multiple lines ...
+ if ($inline && false !== strpos($code, PHP_EOL)) {
+ $inline = false;
}
- $code = $node->name()->accept($this) . '(' . PHP_EOL;
+ $renderedArguments[] = $code;
+ }
+
+ $name = $node->name()->accept($this);
+
+ if ($inline) {
+ $code = $name . '(' . implode(', ', $renderedArguments) . ')';
+ } else {
+ $argumentCode = implode(',' . PHP_EOL, $renderedArguments) . PHP_EOL;
+ $code = $name . '(' . PHP_EOL;
$code .= $this->indent($argumentCode, 1);
$code .= ')';
}
@@ -756,6 +763,8 @@ protected function generateStatementCode(IStatement $node)
return $code;
}
+ const MAXIMUM_INLINE_ARITY = 3;
+
private $indentString;
private $indentLevel;
private $isInterfaceScope;
@@ -558,59 +558,63 @@ public function testVisitInterfaceDefinitionWithParent()
$this->assertSame($expected, $code);
}
- public function testVisitFunctionCall()
+ public function testVisitCall()
{
$node = new Call(QualifiedIdentifier::fromString('foo'));
$code = $node->accept($this->_renderer);
$this->assertSame('foo()', $code);
}
- public function testVisitFunctionCallWithParameters()
+ public function testVisitCallInline()
{
$node = new Call(QualifiedIdentifier::fromString('foo'));
$node->add(new Literal(1));
$node->add(new Literal(2));
$node->add(new Literal(3));
$code = $node->accept($this->_renderer);
+ $expected = 'foo(1, 2, 3)';
+ $this->assertSame($expected, $code);
+ }
+
+ public function testVisitCallMultilineDueToArity()
+ {
+ $node = new Call(QualifiedIdentifier::fromString('foo'));
+ $node->add(new Literal(1));
+ $node->add(new Literal(2));
+ $node->add(new Literal(3));
+ $node->add(new Literal(4));
+ $code = $node->accept($this->_renderer);
+
$expected = 'foo(' . PHP_EOL;
$expected .= ' 1,' . PHP_EOL;
$expected .= ' 2,' . PHP_EOL;
- $expected .= ' 3' . PHP_EOL;
+ $expected .= ' 3,' . PHP_EOL;
+ $expected .= ' 4' . PHP_EOL;
$expected .= ')';
$this->assertSame($expected, $code);
}
- public function testVisitFunctionCallNestedCall()
+ public function testVisitCallMultilineDueToNestedMultiline()
{
- $nestedNode2 = new Call(QualifiedIdentifier::fromString('spam'));
- $nestedNode2->add(new Literal('one'));
- $nestedNode2->add(new Literal('two'));
- $nestedNode2->add(new Literal('three'));
-
- $nestedNode1 = new Call(QualifiedIdentifier::fromString('bar'));
- $nestedNode1->add(new Literal('before'));
- $nestedNode1->add($nestedNode2);
- $nestedNode1->add(new Literal('after'));
+ $innerNode = new Call(QualifiedIdentifier::fromString('bar'));
+ $innerNode->add(new Literal(1));
+ $innerNode->add(new Literal(2));
+ $innerNode->add(new Literal(3));
+ $innerNode->add(new Literal(4));
$node = new Call(QualifiedIdentifier::fromString('foo'));
- $node->add(new Literal('before'));
- $node->add($nestedNode1);
- $node->add(new Literal('after'));
+ $node->add($innerNode);
+
$code = $node->accept($this->_renderer);
$expected = 'foo(' . PHP_EOL;
- $expected .= " 'before'," . PHP_EOL;
$expected .= ' bar(' . PHP_EOL;
- $expected .= " 'before'," . PHP_EOL;
- $expected .= ' spam(' . PHP_EOL;
- $expected .= " 'one'," . PHP_EOL;
- $expected .= " 'two'," . PHP_EOL;
- $expected .= " 'three'" . PHP_EOL;
- $expected .= ' ),' . PHP_EOL;
- $expected .= " 'after'" . PHP_EOL;
- $expected .= ' ),' . PHP_EOL;
- $expected .= " 'after'" . PHP_EOL;
+ $expected .= ' 1,' . PHP_EOL;
+ $expected .= ' 2,' . PHP_EOL;
+ $expected .= ' 3,' . PHP_EOL;
+ $expected .= ' 4' . PHP_EOL;
+ $expected .= ' )' . PHP_EOL;
$expected .= ')';
$this->assertSame($expected, $code);
}

0 comments on commit 009cc96

Please sign in to comment.