Skip to content

Commit 24ccd2a

Browse files
committed
Helpers::formatArgs() splits long ?* into multiple lines
1 parent f735a31 commit 24ccd2a

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/PhpGenerator/Helpers.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,22 @@ public static function formatArgs($statement, array $args)
160160
if (!is_array($arg)) {
161161
throw new Nette\InvalidArgumentException('Argument must be an array.');
162162
}
163-
$arg = implode(', ', array_map(array(__CLASS__, 'dump'), $arg));
164-
$statement = substr_replace($statement, $arg, $a, 2);
163+
$s = substr($statement, 0, $a);
164+
$sep = '';
165+
foreach ($arg as $tmp) {
166+
$s .= $sep . self::dump($tmp);
167+
$sep = strlen($s) - strrpos($s, "\n") > 100 ? ",\n\t" : ', ';
168+
}
169+
$statement = $s . substr($statement, $a + 2);
170+
$a = strlen($s);
165171

166172
} else {
167173
$arg = substr($statement, $a - 1, 1) === '$' || in_array(substr($statement, $a - 2, 2), array('->', '::'), TRUE)
168174
? self::formatMember($arg) : self::_dump($arg);
169175
$statement = substr_replace($statement, $arg, $a, 1);
176+
$a += strlen($arg);
170177
}
171-
$a = strpos($statement, '?', $a + strlen($arg));
178+
$a = strpos($statement, '?', $a);
172179
}
173180
return $statement;
174181
}

tests/PhpGenerator/Helpers.format.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Assert::same( 'func', Helpers::formatArgs('func', array(1, 2)) );
1818
Assert::same( 'func(1)', Helpers::formatArgs('func(?)', array(1, 2)) );
1919
Assert::same( "func(array(1, 2))", Helpers::formatArgs('func(?)', array(array(1, 2))) );
2020
Assert::same( 'func(1, 2)', Helpers::formatArgs('func(?*)', array(array(1, 2))) );
21+
Assert::same(
22+
"func(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,\n\t35, 36, 37, 38, 39, 40)",
23+
Helpers::formatArgs('func(?*)', array(range(10, 40)))
24+
);
2125

2226
Assert::exception(function() {
2327
Helpers::formatArgs('func(?*)', array(1, 2));

0 commit comments

Comments
 (0)