Skip to content

Commit 0ca50a7

Browse files
committed
Helpers::formatArgs() refactoring
1 parent eff0597 commit 0ca50a7

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
@@ -165,34 +165,30 @@ public static function format($statement, ...$args)
165165
*/
166166
public static function formatArgs($statement, array $args)
167167
{
168-
$a = strpos($statement, '?');
169-
while ($a !== FALSE) {
170-
if (!$args) {
168+
$tokens = preg_split('#(\$\?|->\?|::\?|\?\*|\?)#', $statement, -1, PREG_SPLIT_DELIM_CAPTURE);
169+
$res = '';
170+
foreach ($tokens as $n => $token) {
171+
if ($n % 2 === 0) {
172+
$res .= $token;
173+
} elseif (!$args) {
171174
throw new Nette\InvalidArgumentException('Insufficient number of arguments.');
172-
}
173-
$arg = array_shift($args);
174-
if (substr($statement, $a + 1, 1) === '*') { // ?*
175+
} elseif ($token === '?') {
176+
$res .= self::dump(array_shift($args));
177+
} elseif ($token === '?*') {
178+
$arg = array_shift($args);
175179
if (!is_array($arg)) {
176180
throw new Nette\InvalidArgumentException('Argument must be an array.');
177181
}
178-
$s = substr($statement, 0, $a);
179182
$sep = '';
180183
foreach ($arg as $tmp) {
181-
$s .= $sep . self::dump($tmp);
182-
$sep = strlen($s) - strrpos($s, "\n") > self::WRAP_LENGTH ? ",\n\t" : ', ';
184+
$res .= $sep . self::dump($tmp);
185+
$sep = strlen($res) - strrpos($res, "\n") > self::WRAP_LENGTH ? ",\n\t" : ', ';
183186
}
184-
$statement = $s . substr($statement, $a + 2);
185-
$a = strlen($s);
186-
187-
} else {
188-
$arg = substr($statement, $a - 1, 1) === '$' || in_array(substr($statement, $a - 2, 2), ['->', '::'], TRUE)
189-
? self::formatMember($arg) : self::_dump($arg);
190-
$statement = substr_replace($statement, $arg, $a, 1);
191-
$a += strlen($arg);
187+
} else { // $ -> ::
188+
$res .= substr($token, 0, -1) . self::formatMember(array_shift($args));
192189
}
193-
$a = strpos($statement, '?', $a);
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
@@ -19,8 +19,8 @@ Assert::same('func(1)', Helpers::formatArgs('func(?)', [1, 2]));
1919
Assert::same('func([1, 2])', Helpers::formatArgs('func(?)', [[1, 2]]));
2020
Assert::same('func(1, 2)', Helpers::formatArgs('func(?*)', [[1, 2]]));
2121
Assert::same(
22-
"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)",
23-
Helpers::formatArgs('func(?*)', [range(10, 40)])
22+
"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)",
23+
Helpers::formatArgs('func(?*)', [range(10, 50)])
2424
);
2525

2626
Assert::exception(function () {

0 commit comments

Comments
 (0)