Skip to content

Commit

Permalink
Helpers::format() is preferred over formatArgs() because we have vari…
Browse files Browse the repository at this point in the history
…adics :)
  • Loading branch information
dg committed Mar 14, 2019
1 parent 546c901 commit 0b79d86
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/PhpGenerator/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private static function _dump(&$var, int $level = 0)
throw new Nette\InvalidArgumentException('Cannot dump anonymous class.');

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

$arr = (array) $var;
Expand Down
2 changes: 1 addition & 1 deletion src/PhpGenerator/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __toString(): string
*/
public function setBody(?string $code, array $args = null): self
{
$this->body = $args === null || $code === null ? $code : Helpers::formatArgs($code, $args);
$this->body = $args === null || $code === null ? $code : Helpers::format($code, ...$args);
return $this;
}

Expand Down
4 changes: 2 additions & 2 deletions src/PhpGenerator/Traits/FunctionLike.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ trait FunctionLike
*/
public function setBody(string $code, array $args = null): self
{
$this->body = $args === null ? $code : Helpers::formatArgs($code, $args);
$this->body = $args === null ? $code : Helpers::format($code, ...$args);
return $this;
}

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

Expand Down
22 changes: 11 additions & 11 deletions tests/PhpGenerator/Helpers.format.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ Assert::same('func', Helpers::format('func'));
Assert::same('func(1)', Helpers::format('func(?)', 1));
Assert::same('func', Helpers::formatArgs('func', []));
Assert::same('func(1)', Helpers::formatArgs('func(?)', [1]));
Assert::same('func(1 ? 2 : 3)', Helpers::formatArgs('func(1 \? 2 : 3)', []));
Assert::same('func([1, 2])', Helpers::formatArgs('func(?)', [[1, 2]]));
Assert::same('func(1, 2)', Helpers::formatArgs('func(...?)', [[1, 2]]));
Assert::same('func(1, 2)', Helpers::formatArgs('func(?*)', [[1, 2]])); // old way
Assert::same('func(1 ? 2 : 3)', Helpers::format('func(1 \? 2 : 3)'));
Assert::same('func([1, 2])', Helpers::format('func(?)', [1, 2]));
Assert::same('func(1, 2)', Helpers::format('func(...?)', [1, 2]));
Assert::same('func(1, 2)', Helpers::format('func(?*)', [1, 2])); // old way
same(
'func(
10,
Expand Down Expand Up @@ -51,25 +51,25 @@ same(
35,
36
)',
Helpers::formatArgs('func(?*)', [range(10, 36)])
Helpers::format('func(?*)', range(10, 36))
);

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

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

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

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

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

0 comments on commit 0b79d86

Please sign in to comment.