Skip to content

Commit 42626d5

Browse files
committed
PhpGenerator: short arrays are dumped in single line
1 parent 7fbce88 commit 42626d5

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

src/PhpGenerator/Helpers.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,21 @@ private static function _dump(&$var, $level = 0)
7777
throw new Nette\InvalidArgumentException('Nesting level too deep or recursive dependency.');
7878

7979
} else {
80-
$out = "\n";
80+
$out = '';
81+
$outAlt = "\n$space";
8182
$var[$marker] = TRUE;
8283
$counter = 0;
8384
foreach ($var as $k => &$v) {
8485
if ($k !== $marker) {
85-
$out .= "$space\t" . ($k === $counter ? '' : self::_dump($k, $level + 1) . " => ") . self::_dump($v, $level + 1) . ",\n";
86+
$item = ($k === $counter ? '' : self::_dump($k, $level + 1) . ' => ') . self::_dump($v, $level + 1);
8687
$counter = is_int($k) ? max($k + 1, $counter) : $counter;
88+
$out .= ($out === '' ? '' : ', ') . $item;
89+
$outAlt .= "\t$item,\n$space";
8790
}
8891
}
8992
unset($var[$marker]);
90-
$out .= $space;
9193
}
92-
return "array($out)";
94+
return 'array(' . (strpos($out, "\n") === FALSE && strlen($out) < 40 ? $out : $outAlt) . ')';
9395

9496
} elseif (is_object($var)) {
9597
$arr = (array) $var;

tests/PhpGenerator/ClassType.expect

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ abstract final class Example extends ParentClass implements IExample, IOne
2020

2121
public $order = RecursiveIteratorIterator::SELF_FIRST;
2222

23-
public static $sections = array(
24-
'first' => TRUE,
25-
);
23+
public static $sections = array('first' => TRUE);
2624

2725

2826
/**

tests/PhpGenerator/Helpers.dump().phpt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ Assert::same( "'I\xc3\xb1t\xc3\xabrn\xc3\xa2ti\xc3\xb4n\xc3\xa0liz\xc3\xa6ti\xc3
3535
Assert::same( '"\rHello \$"', Helpers::dump("\rHello $") );
3636
Assert::same( 'array()', Helpers::dump(array()) );
3737

38-
Assert::same( "array(\n\t\$s,\n)", Helpers::dump(array(new PhpLiteral('$s'))) );
38+
Assert::same( "array(\$s)", Helpers::dump(array(new PhpLiteral('$s'))) );
3939

40-
Assert::same( "array(\n\t1,\n\t2,\n\t3,\n)", Helpers::dump(array(1,2,3)) );
41-
Assert::same( "array(\n\t'a',\n\t7 => 'b',\n\t'c',\n\t'9a' => 'd',\n\t'e',\n)", Helpers::dump(array('a', 7 => 'b', 'c', '9a' => 'd', 9 => 'e')) );
42-
Assert::same( "array(\n\t'a' => 1,\n\tarray(\n\t\t\"\\r\" => \"\\r\",\n\t\t2,\n\t),\n\t3,\n)", Helpers::dump(array('a' => 1, array("\r" => "\r", 2), 3)) );
40+
Assert::same( "array(1, 2, 3)", Helpers::dump(array(1,2,3)) );
41+
Assert::same( "array('a', 7 => 'b', 'c', '9a' => 'd', 'e')", Helpers::dump(array('a', 7 => 'b', 'c', '9a' => 'd', 9 => 'e')) );
42+
Assert::same( "array(\n\tarray(\n\t\t'a',\n\t\t'loooooooooooooooooooooooooooooooooong',\n\t),\n)", Helpers::dump(array(array('a', 'loooooooooooooooooooooooooooooooooong'))) );
43+
Assert::same( "array('a' => 1, array(\"\\r\" => \"\\r\", 2), 3)", Helpers::dump(array('a' => 1, array("\r" => "\r", 2), 3)) );
4344

4445
Assert::same( "(object) array(\n\t'a' => 1,\n\t'b' => 2,\n)", Helpers::dump((object) array('a' => 1, 'b' => 2)) );
4546
Assert::same( "(object) array(\n\t'a' => (object) array(\n\t\t'b' => 2,\n\t),\n)" , Helpers::dump((object) array('a' => (object) array('b' => 2))) );

tests/PhpGenerator/Helpers.format.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Assert::same( 'func(1)', Helpers::format('func(?)', 1, 2) );
1919

2020
Assert::same( 'func', Helpers::formatArgs('func', array(1, 2)) );
2121
Assert::same( 'func(1)', Helpers::formatArgs('func(?)', array(1, 2)) );
22-
Assert::same( "func(array(\n\t1,\n\t2,\n))", Helpers::formatArgs('func(?)', array(array(1, 2))) );
22+
Assert::same( "func(array(1, 2))", Helpers::formatArgs('func(?)', array(array(1, 2))) );
2323
Assert::same( 'func(1, 2)', Helpers::formatArgs('func(?*)', array(array(1, 2))) );
2424

2525
Assert::exception(function() {

0 commit comments

Comments
 (0)