From a7d2dee67a0908c89dcaef1fd70d36f448405e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Wed, 29 Dec 2021 12:48:27 +0100 Subject: [PATCH] Enhancement: Extract named constructor --- CHANGELOG.md | 2 ++ src/Format/Format.php | 16 +++++++++++++++- test/Unit/FixedFormatNormalizerTest.php | 2 +- test/Unit/Format/FormatTest.php | 12 ++++++------ test/Unit/Format/FormatterTest.php | 2 +- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 487fe14d..00360330 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ For a full diff see [`1.0.3...main`][1.0.3...main]. - Renamed `Format::__toString()`, `Indent::__toString()`, and `Json::__toString()` to `Format::toString()`, `Indent::toString()`, and `Json::toString()`, requiring consumers to explicitly invoke methods instead of allowing to cast to `string` ([#589]), by [@localheinz] - Started using the `SchemaValidator` provided by `ergebnis/json-schema-validator` ([#595]), by [@localheinz] - Renamed `Format\JsonEncodeOptions::value()` to `Format\JsonEncodeOptions::toInt()` ([#603]), by [@localheinz] +- Extracted `Format\Format::create()` as named constructor and reduced visibility of `__construct` to `private` ([#608]), by [@localheinz] ### Fixed @@ -436,6 +437,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0]. [#595]: https://github.com/ergebnis/json-normalizer/pull/595 [#597]: https://github.com/ergebnis/json-normalizer/pull/597 [#603]: https://github.com/ergebnis/json-normalizer/pull/603 +[#608]: https://github.com/ergebnis/json-normalizer/pull/608 [@BackEndTea]: https://github.com/BackEndTea [@dependabot]: https://github.com/dependabot diff --git a/src/Format/Format.php b/src/Format/Format.php index b42dc660..0fe2f08f 100644 --- a/src/Format/Format.php +++ b/src/Format/Format.php @@ -22,7 +22,7 @@ final class Format private NewLine $newLine; private bool $hasFinalNewLine; - public function __construct( + private function __construct( JsonEncodeOptions $jsonEncodeOptions, Indent $indent, NewLine $newLine, @@ -34,6 +34,20 @@ public function __construct( $this->hasFinalNewLine = $hasFinalNewLine; } + public static function create( + JsonEncodeOptions $jsonEncodeOptions, + Indent $indent, + NewLine $newLine, + bool $hasFinalNewLine + ): self { + return new self( + $jsonEncodeOptions, + $indent, + $newLine, + $hasFinalNewLine, + ); + } + public static function fromJson(Json $json): self { $encoded = $json->encoded(); diff --git a/test/Unit/FixedFormatNormalizerTest.php b/test/Unit/FixedFormatNormalizerTest.php index dcaf6c8d..89a595f6 100644 --- a/test/Unit/FixedFormatNormalizerTest.php +++ b/test/Unit/FixedFormatNormalizerTest.php @@ -35,7 +35,7 @@ public function testNormalizeNormalizesAndFormatsUsingFormat(): void { $faker = self::faker(); - $format = new Format\Format( + $format = Format\Format::create( Format\JsonEncodeOptions::fromInt($faker->numberBetween(1)), Format\Indent::fromString("\t"), Format\NewLine::fromString("\r\n"), diff --git a/test/Unit/Format/FormatTest.php b/test/Unit/Format/FormatTest.php index f44329ee..a01b4d29 100644 --- a/test/Unit/Format/FormatTest.php +++ b/test/Unit/Format/FormatTest.php @@ -32,13 +32,13 @@ final class FormatTest extends Framework\TestCase /** * @dataProvider \Ergebnis\DataProvider\BoolProvider::arbitrary() */ - public function testConstructorSetsValues(bool $hasFinalNewLine): void + public function testCreateReturnsFormat(bool $hasFinalNewLine): void { $jsonEncodeOptions = Format\JsonEncodeOptions::fromInt(\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES); $indent = Format\Indent::fromString(' '); $newLine = Format\NewLine::fromString("\r\n"); - $format = new Format\Format( + $format = Format\Format::create( $jsonEncodeOptions, $indent, $newLine, @@ -53,7 +53,7 @@ public function testConstructorSetsValues(bool $hasFinalNewLine): void public function testWithJsonEncodeOptionsClonesFormatAndSetsJsonEncodeOptions(): void { - $format = new Format\Format( + $format = Format\Format::create( Format\JsonEncodeOptions::fromInt(\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES), Format\Indent::fromString(' '), Format\NewLine::fromString("\r\n"), @@ -72,7 +72,7 @@ public function testWithIndentClonesFormatAndSetsIndent(): void { $indent = Format\Indent::fromString("\t"); - $format = new Format\Format( + $format = Format\Format::create( Format\JsonEncodeOptions::fromInt(\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES), Format\Indent::fromString(' '), Format\NewLine::fromString("\r\n"), @@ -89,7 +89,7 @@ public function testWithNewLineClonesFormatAndSetsNewLine(): void { $newLine = Format\NewLine::fromString("\r\n"); - $format = new Format\Format( + $format = Format\Format::create( Format\JsonEncodeOptions::fromInt(\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES), Format\Indent::fromString(' '), Format\NewLine::fromString("\r"), @@ -107,7 +107,7 @@ public function testWithNewLineClonesFormatAndSetsNewLine(): void */ public function testWithHasFinalNewLineClonesFormatAndSetsFinalNewLine(bool $hasFinalNewLine): void { - $format = new Format\Format( + $format = Format\Format::create( Format\JsonEncodeOptions::fromInt(\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES), Format\Indent::fromString(' '), Format\NewLine::fromString("\r\n"), diff --git a/test/Unit/Format/FormatterTest.php b/test/Unit/Format/FormatterTest.php index 466a16a0..4d3e7063 100644 --- a/test/Unit/Format/FormatterTest.php +++ b/test/Unit/Format/FormatterTest.php @@ -73,7 +73,7 @@ public function testFormatEncodesWithJsonEncodeOptionsIndentsAndPossiblySuffixes } JSON; - $format = new Format\Format( + $format = Format\Format::create( Format\JsonEncodeOptions::fromInt($jsonEncodeOptions), Format\Indent::fromString($indentString), Format\NewLine::fromString($newLineString),