From 0b79d86ab00e00389c7cea5f6772beed9bdac819 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sat, 2 Mar 2019 20:29:51 +0100 Subject: [PATCH] Helpers::format() is preferred over formatArgs() because we have variadics :) --- src/PhpGenerator/Helpers.php | 2 +- src/PhpGenerator/Method.php | 2 +- src/PhpGenerator/Traits/FunctionLike.php | 4 ++-- tests/PhpGenerator/Helpers.format.phpt | 22 +++++++++++----------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/PhpGenerator/Helpers.php b/src/PhpGenerator/Helpers.php index b265e7b7..1303a494 100644 --- a/src/PhpGenerator/Helpers.php +++ b/src/PhpGenerator/Helpers.php @@ -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; diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 124240ce..bf2ab538 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -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; } diff --git a/src/PhpGenerator/Traits/FunctionLike.php b/src/PhpGenerator/Traits/FunctionLike.php index 4c368df2..3d2b2343 100644 --- a/src/PhpGenerator/Traits/FunctionLike.php +++ b/src/PhpGenerator/Traits/FunctionLike.php @@ -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; } @@ -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; } diff --git a/tests/PhpGenerator/Helpers.format.phpt b/tests/PhpGenerator/Helpers.format.phpt index 627b896c..91245abc 100644 --- a/tests/PhpGenerator/Helpers.format.phpt +++ b/tests/PhpGenerator/Helpers.format.phpt @@ -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, @@ -51,11 +51,11 @@ 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 () { @@ -63,13 +63,13 @@ Assert::exception(function () { }, 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'));