Skip to content

Commit

Permalink
Merge pull request #589 from ergebnis/fix/to-string
Browse files Browse the repository at this point in the history
Fix: Disallow implicit cast to `string`
  • Loading branch information
localheinz committed Dec 29, 2021
2 parents 9a3dc55 + 6115688 commit c387938
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 38 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ For a full diff see [`1.0.3...main`][1.0.3...main].

- Dropped support for PHP 7.2 ([#564]), by [@dependabot]
- Dropped support for PHP 7.3 ([#573]), by [@dependabot]
- 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 [@dependabot]

### Fixed

Expand Down Expand Up @@ -423,6 +424,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[#517]: https://github.com/ergebnis/json-normalizer/pull/517
[#564]: https://github.com/ergebnis/json-normalizer/pull/564
[#573]: https://github.com/ergebnis/json-normalizer/pull/573
[#589]: https://github.com/ergebnis/json-normalizer/pull/589

[@BackEndTea]: https://github.com/BackEndTea
[@dependabot]: https://github.com/dependabot
Expand Down
6 changes: 3 additions & 3 deletions src/Format/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public function format(

$printed = $this->printer->print(
$encoded,
(string) $format->indent(),
(string) $format->newLine(),
$format->indent()->toString(),
$format->newLine()->toString(),
);

if (!$format->hasFinalNewLine()) {
return Json::fromEncoded($printed);
}

return Json::fromEncoded($printed . $format->newLine());
return Json::fromEncoded($printed . $format->newLine()->toString());
}
}
10 changes: 5 additions & 5 deletions src/Format/Indent.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ private function __construct(string $string)
$this->string = $string;
}

public function __toString(): string
{
return $this->string;
}

/**
* @throws Exception\InvalidIndentStringException
*/
Expand Down Expand Up @@ -89,4 +84,9 @@ public static function fromJson(Json $json): self
'space',
);
}

public function toString(): string
{
return $this->string;
}
}
10 changes: 5 additions & 5 deletions src/Format/NewLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ private function __construct(string $string)
$this->string = $string;
}

public function __toString(): string
{
return $this->string;
}

/**
* @throws Exception\InvalidNewLineStringException
*/
Expand All @@ -50,4 +45,9 @@ public static function fromJson(Json $json): self

return self::fromString(\PHP_EOL);
}

public function toString(): string
{
return $this->string;
}
}
2 changes: 1 addition & 1 deletion src/IndentNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function normalize(Json $json): Json
{
$withIndent = $this->printer->print(
$json->encoded(),
$this->indent->__toString(),
$this->indent->toString(),
);

return Json::fromEncoded($withIndent);
Expand Down
16 changes: 8 additions & 8 deletions src/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ private function __construct(
$this->decoded = $decoded;
}

/**
* Returns the original JSON value.
*/
public function __toString(): string
{
return $this->encoded;
}

/**
* @throws Exception\InvalidJsonEncodedException
*/
Expand Down Expand Up @@ -88,4 +80,12 @@ public function format(): Format\Format

return $this->format;
}

/**
* Returns the original JSON value.
*/
public function toString(): string
{
return $this->encoded;
}
}
2 changes: 1 addition & 1 deletion test/Unit/Format/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function testFromJsonReturnsFormatWithDefaultIndentIfJsonIsWithoutIndent(

$format = Format\Format::fromJson($json);

self::assertSame(' ', $format->indent()->__toString());
self::assertSame(' ', $format->indent()->toString());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/Unit/Format/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public function testFormatEncodesWithJsonEncodeOptionsIndentsAndPossiblySuffixes
->method('print')
->with(
self::identicalTo($encodedWithJsonEncodeOptions),
self::identicalTo($format->indent()->__toString()),
self::identicalTo($format->newLine()->__toString()),
self::identicalTo($format->indent()->toString()),
self::identicalTo($format->newLine()->toString()),
)
->willReturn($printedWithIndentAndNewLine);

Expand Down
10 changes: 5 additions & 5 deletions test/Unit/Format/IndentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function testFromSizeAndStyleReturnsIndent(int $size, string $style, stri
$style,
);

self::assertSame($string, $indent->__toString());
self::assertSame($string, $indent->toString());
}

/**
Expand Down Expand Up @@ -160,7 +160,7 @@ public function testFromStringReturnsIndent(string $string): void
{
$indent = Format\Indent::fromString($string);

self::assertSame($string, $indent->__toString());
self::assertSame($string, $indent->toString());
}

/**
Expand Down Expand Up @@ -202,7 +202,7 @@ public function testFromJsonReturnsIndentSniffedFromArray(string $actualIndent,

$indent = Format\Indent::fromJson($json);

self::assertSame($sniffedIndent, $indent->__toString());
self::assertSame($sniffedIndent, $indent->toString());
}

/**
Expand All @@ -225,7 +225,7 @@ public function testFromJsonReturnsIndentSniffedFromObject(string $actualIndent,

$indent = Format\Indent::fromJson($json);

self::assertSame($sniffedIndent, $indent->__toString());
self::assertSame($sniffedIndent, $indent->toString());
}

/**
Expand Down Expand Up @@ -298,7 +298,7 @@ public function testFromJsonReturnsIndentWithDefaultsWhenIndentCouldNotBeSniffed
4,
);

self::assertSame($default, $indent->__toString());
self::assertSame($default, $indent->toString());
}

/**
Expand Down
8 changes: 4 additions & 4 deletions test/Unit/Format/NewLineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function testFromStringReturnsNewLine(string $string): void
{
$newLine = Format\NewLine::fromString($string);

self::assertSame($string, $newLine->__toString());
self::assertSame($string, $newLine->toString());
}

/**
Expand Down Expand Up @@ -97,7 +97,7 @@ public function testFromJsonReturnsFormatWithDefaultNewLineIfNoneFound(): void

$newLine = Format\NewLine::fromJson($json);

self::assertSame(\PHP_EOL, $newLine->__toString());
self::assertSame(\PHP_EOL, $newLine->toString());
}

/**
Expand All @@ -113,7 +113,7 @@ public function testFromFormatReturnsFormatWithNewLineSniffedFromArray(string $n

$newLine = Format\NewLine::fromJson($json);

self::assertSame($newLineString, $newLine->__toString());
self::assertSame($newLineString, $newLine->toString());
}

/**
Expand All @@ -129,7 +129,7 @@ public function testFromFormatReturnsFormatWithNewLineNewLineSniffedFromObject(s

$newLine = Format\NewLine::fromJson($json);

self::assertSame($newLineString, $newLine->__toString());
self::assertSame($newLineString, $newLine->toString());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/Unit/IndentNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testNormalizeUsesPrinterToNormalizeJsonWithIndent(): void
->method('print')
->with(
self::identicalTo($json->encoded()),
self::identicalTo($indent->__toString()),
self::identicalTo($indent->toString()),
)
->willReturn($indented);

Expand Down
6 changes: 3 additions & 3 deletions test/Unit/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ public function testFromEncodedReturnsJson(string $encoded): void
{
$json = Json::fromEncoded($encoded);

self::assertSame($encoded, $json->__toString());
self::assertSame($encoded, $json->toString());
self::assertSame($encoded, $json->encoded());
self::assertSame($encoded, \json_encode($json->decoded()));

$format = Format\Format::fromJson($json);

self::assertSame($format->jsonEncodeOptions()->value(), $json->format()->jsonEncodeOptions()->value());
self::assertSame($format->indent()->__toString(), $json->format()->indent()->__toString());
self::assertSame($format->newLine()->__toString(), $json->format()->newLine()->__toString());
self::assertSame($format->indent()->toString(), $json->format()->indent()->toString());
self::assertSame($format->newLine()->toString(), $json->format()->newLine()->toString());
self::assertSame($format->hasFinalNewLine(), $json->format()->hasFinalNewLine());
}

Expand Down

0 comments on commit c387938

Please sign in to comment.