Skip to content

Commit 0b79d86

Browse files
committed
Helpers::format() is preferred over formatArgs() because we have variadics :)
1 parent 546c901 commit 0b79d86

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

src/PhpGenerator/Helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private static function _dump(&$var, int $level = 0)
110110
throw new Nette\InvalidArgumentException('Cannot dump anonymous class.');
111111

112112
} elseif (in_array($class, ['DateTime', 'DateTimeImmutable'], true)) {
113-
return self::formatArgs("new $class(?, new DateTimeZone(?))", [$var->format('Y-m-d H:i:s.u'), $var->getTimeZone()->getName()]);
113+
return self::format("new $class(?, new DateTimeZone(?))", $var->format('Y-m-d H:i:s.u'), $var->getTimeZone()->getName());
114114
}
115115

116116
$arr = (array) $var;

src/PhpGenerator/Method.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function __toString(): string
6363
*/
6464
public function setBody(?string $code, array $args = null): self
6565
{
66-
$this->body = $args === null || $code === null ? $code : Helpers::formatArgs($code, $args);
66+
$this->body = $args === null || $code === null ? $code : Helpers::format($code, ...$args);
6767
return $this;
6868
}
6969

src/PhpGenerator/Traits/FunctionLike.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ trait FunctionLike
4343
*/
4444
public function setBody(string $code, array $args = null): self
4545
{
46-
$this->body = $args === null ? $code : Helpers::formatArgs($code, $args);
46+
$this->body = $args === null ? $code : Helpers::format($code, ...$args);
4747
return $this;
4848
}
4949

@@ -59,7 +59,7 @@ public function getBody(): string
5959
*/
6060
public function addBody(string $code, array $args = null): self
6161
{
62-
$this->body .= ($args === null ? $code : Helpers::formatArgs($code, $args)) . "\n";
62+
$this->body .= ($args === null ? $code : Helpers::format($code, ...$args)) . "\n";
6363
return $this;
6464
}
6565

tests/PhpGenerator/Helpers.format.phpt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ Assert::same('func', Helpers::format('func'));
1717
Assert::same('func(1)', Helpers::format('func(?)', 1));
1818
Assert::same('func', Helpers::formatArgs('func', []));
1919
Assert::same('func(1)', Helpers::formatArgs('func(?)', [1]));
20-
Assert::same('func(1 ? 2 : 3)', Helpers::formatArgs('func(1 \? 2 : 3)', []));
21-
Assert::same('func([1, 2])', Helpers::formatArgs('func(?)', [[1, 2]]));
22-
Assert::same('func(1, 2)', Helpers::formatArgs('func(...?)', [[1, 2]]));
23-
Assert::same('func(1, 2)', Helpers::formatArgs('func(?*)', [[1, 2]])); // old way
20+
Assert::same('func(1 ? 2 : 3)', Helpers::format('func(1 \? 2 : 3)'));
21+
Assert::same('func([1, 2])', Helpers::format('func(?)', [1, 2]));
22+
Assert::same('func(1, 2)', Helpers::format('func(...?)', [1, 2]));
23+
Assert::same('func(1, 2)', Helpers::format('func(?*)', [1, 2])); // old way
2424
same(
2525
'func(
2626
10,
@@ -51,25 +51,25 @@ same(
5151
35,
5252
36
5353
)',
54-
Helpers::formatArgs('func(?*)', [range(10, 36)])
54+
Helpers::format('func(?*)', range(10, 36))
5555
);
5656

5757
Assert::exception(function () {
58-
Helpers::formatArgs('func(...?)', [1, 2]);
58+
Helpers::format('func(...?)', 1, 2);
5959
}, Nette\InvalidArgumentException::class, 'Argument must be an array.');
6060

6161
Assert::exception(function () {
6262
Helpers::format('func(?)', 1, 2);
6363
}, Nette\InvalidArgumentException::class, 'Insufficient number of placeholders.');
6464

6565
Assert::exception(function () {
66-
Helpers::formatArgs('func(?, ?, ?)', [1, 2]);
66+
Helpers::format('func(?, ?, ?)', [1, 2]);
6767
}, Nette\InvalidArgumentException::class, 'Insufficient number of arguments.');
6868

69-
Assert::same('$a = 2', Helpers::formatArgs('$? = ?', ['a', 2]));
70-
Assert::same('$obj->a = 2', Helpers::formatArgs('$obj->? = ?', ['a', 2]));
71-
Assert::same('$obj->{1} = 2', Helpers::formatArgs('$obj->? = ?', [1, 2]));
72-
Assert::same('$obj->{\' \'} = 2', Helpers::formatArgs('$obj->? = ?', [' ', 2]));
69+
Assert::same('$a = 2', Helpers::format('$? = ?', 'a', 2));
70+
Assert::same('$obj->a = 2', Helpers::format('$obj->? = ?', 'a', 2));
71+
Assert::same('$obj->{1} = 2', Helpers::format('$obj->? = ?', 1, 2));
72+
Assert::same('$obj->{\' \'} = 2', Helpers::format('$obj->? = ?', ' ', 2));
7373

7474
Assert::same('Item', Helpers::formatMember('Item'));
7575
Assert::same("{'0Item'}", Helpers::formatMember('0Item'));

0 commit comments

Comments
 (0)