Skip to content

Commit ec39da7

Browse files
committed
Helpers::formatArgs() refactoring
1 parent dd0c53f commit ec39da7

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

src/PhpGenerator/Helpers.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -162,37 +162,33 @@ public static function format(string $statement, ...$args): string
162162
*/
163163
public static function formatArgs(string $statement, array $args): string
164164
{
165-
$a = strpos($statement, '?');
166-
while ($a !== FALSE) {
167-
if (!$args) {
165+
$tokens = preg_split('#(\$\?|->\?|::\?|\?\*|\?)#', $statement, -1, PREG_SPLIT_DELIM_CAPTURE);
166+
$res = '';
167+
foreach ($tokens as $n => $token) {
168+
if ($n % 2 === 0) {
169+
$res .= $token;
170+
} elseif (!$args) {
168171
throw new Nette\InvalidArgumentException('Insufficient number of arguments.');
169-
}
170-
$arg = array_shift($args);
171-
if (substr($statement, $a + 1, 1) === '*') { // ?*
172+
} elseif ($token === '?') {
173+
$res .= self::dump(array_shift($args));
174+
} elseif ($token === '?*') {
175+
$arg = array_shift($args);
172176
if (!is_array($arg)) {
173177
throw new Nette\InvalidArgumentException('Argument must be an array.');
174178
}
175-
$s = substr($statement, 0, $a);
176179
$sep = '';
177180
foreach ($arg as $tmp) {
178-
$s .= $sep . self::dump($tmp);
179-
$sep = strlen($s) - strrpos($s, "\n") > self::WRAP_LENGTH ? ",\n\t" : ', ';
181+
$res .= $sep . self::dump($tmp);
182+
$sep = strlen($res) - strrpos($res, "\n") > self::WRAP_LENGTH ? ",\n\t" : ', ';
180183
}
181-
$statement = $s . substr($statement, $a + 2);
182-
$a = strlen($s);
183-
184-
} else {
185-
$arg = substr($statement, $a - 1, 1) === '$' || in_array(substr($statement, $a - 2, 2), ['->', '::'], TRUE)
186-
? self::formatMember($arg) : self::_dump($arg);
187-
$statement = substr_replace($statement, $arg, $a, 1);
188-
$a += strlen($arg);
184+
} else { // $ -> ::
185+
$res .= substr($token, 0, -1) . self::formatMember(array_shift($args));
189186
}
190-
$a = strpos($statement, '?', $a);
191187
}
192188
if ($args) {
193189
throw new Nette\InvalidArgumentException('Insufficient number of placeholders.');
194190
}
195-
return $statement;
191+
return $res;
196192
}
197193

198194

tests/PhpGenerator/Helpers.format.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Assert::same('func(1)', Helpers::formatArgs('func(?)', [1]));
2020
Assert::same('func([1, 2])', Helpers::formatArgs('func(?)', [[1, 2]]));
2121
Assert::same('func(1, 2)', Helpers::formatArgs('func(?*)', [[1, 2]]));
2222
Assert::same(
23-
"func(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,\n\t27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40)",
24-
Helpers::formatArgs('func(?*)', [range(10, 40)])
23+
"func(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,\n\t27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,\n\t45, 46, 47, 48, 49, 50)",
24+
Helpers::formatArgs('func(?*)', [range(10, 50)])
2525
);
2626

2727
Assert::exception(function () {

0 commit comments

Comments
 (0)