Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Remove NewLineInterface #90

Merged
merged 1 commit into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/Format/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class Format implements FormatInterface
private $indent;

/**
* @var NewLineInterface
* @var NewLine
*/
private $newLine;

Expand All @@ -39,14 +39,14 @@ final class Format implements FormatInterface
private $hasFinalNewLine;

/**
* @param int $jsonEncodeOptions
* @param Indent $indent
* @param NewLineInterface $newLine
* @param bool $hasFinalNewLine
* @param int $jsonEncodeOptions
* @param Indent $indent
* @param NewLine $newLine
* @param bool $hasFinalNewLine
*
* @throws Exception\InvalidJsonEncodeOptionsException
*/
public function __construct(int $jsonEncodeOptions, Indent $indent, NewLineInterface $newLine, bool $hasFinalNewLine)
public function __construct(int $jsonEncodeOptions, Indent $indent, NewLine $newLine, bool $hasFinalNewLine)
{
if (0 > $jsonEncodeOptions) {
throw Exception\InvalidJsonEncodeOptionsException::fromJsonEncodeOptions($jsonEncodeOptions);
Expand Down Expand Up @@ -80,7 +80,7 @@ public function indent(): Indent
return $this->indent;
}

public function newLine(): NewLineInterface
public function newLine(): NewLine
{
return $this->newLine;
}
Expand Down Expand Up @@ -112,7 +112,7 @@ public function withIndent(Indent $indent): FormatInterface
return $mutated;
}

public function withNewLine(NewLineInterface $newLine): FormatInterface
public function withNewLine(NewLine $newLine): FormatInterface
{
$mutated = clone $this;

Expand Down Expand Up @@ -157,7 +157,7 @@ private static function detectIndent(string $encoded): Indent
);
}

private static function detectNewLine(string $encoded): NewLineInterface
private static function detectNewLine(string $encoded): NewLine
{
if (1 === \preg_match('/(?P<newLine>\r\n|\n|\r)/', $encoded, $match)) {
return NewLine::fromString($match['newLine']);
Expand Down
4 changes: 2 additions & 2 deletions src/Format/FormatInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function jsonEncodeOptions(): int;

public function indent(): Indent;

public function newLine(): NewLineInterface;
public function newLine(): NewLine;

public function hasFinalNewLine(): bool;

Expand All @@ -36,7 +36,7 @@ public function withJsonEncodeOptions(int $jsonEncodeOptions): self;

public function withIndent(Indent $indent): self;

public function withNewLine(NewLineInterface $newLine): self;
public function withNewLine(NewLine $newLine): self;

public function withHasFinalNewLine(bool $hasFinalNewLine): self;
}
6 changes: 3 additions & 3 deletions src/Format/NewLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Localheinz\Json\Normalizer\Exception;

final class NewLine implements NewLineInterface
final class NewLine
{
/**
* @var string
Expand All @@ -37,9 +37,9 @@ public function __toString(): string
*
* @throws Exception\InvalidNewLineStringException
*
* @return NewLineInterface
* @return self
*/
public static function fromString(string $string): NewLineInterface
public static function fromString(string $string): self
{
if (1 !== \preg_match('/^(?>\r\n|\n|\r)$/', $string)) {
throw Exception\InvalidNewLineStringException::fromString($string);
Expand Down
19 changes: 0 additions & 19 deletions src/Format/NewLineInterface.php

This file was deleted.

28 changes: 14 additions & 14 deletions test/Unit/Format/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Localheinz\Json\Normalizer\Format\Format;
use Localheinz\Json\Normalizer\Format\FormatInterface;
use Localheinz\Json\Normalizer\Format\Indent;
use Localheinz\Json\Normalizer\Format\NewLineInterface;
use Localheinz\Json\Normalizer\Format\NewLine;
use Localheinz\Json\Normalizer\JsonInterface;
use Localheinz\Test\Util\Helper;
use PHPUnit\Framework;
Expand All @@ -38,15 +38,15 @@ public function testConstructorRejectsInvalidJsonEncodeOptions(): void
{
$jsonEncodeOptions = -1;
$indent = Indent::fromString(' ');
$newLine = $this->prophesize(NewLineInterface::class);
$newLine = NewLine::fromString("\r\n");
$hasFinalNewLine = true;

$this->expectException(Exception\InvalidJsonEncodeOptionsException::class);

new Format(
$jsonEncodeOptions,
$indent,
$newLine->reveal(),
$newLine,
$hasFinalNewLine
);
}
Expand All @@ -60,18 +60,18 @@ public function testConstructorSetsValues(bool $hasFinalNewLine): void
{
$jsonEncodeOptions = \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES;
$indent = Indent::fromString(' ');
$newLine = $this->prophesize(NewLineInterface::class);
$newLine = NewLine::fromString("\r\n");

$format = new Format(
$jsonEncodeOptions,
$indent,
$newLine->reveal(),
$newLine,
$hasFinalNewLine
);

$this->assertSame($jsonEncodeOptions, $format->jsonEncodeOptions());
$this->assertSame($indent, $format->indent());
$this->assertSame($newLine->reveal(), $format->newLine());
$this->assertSame($newLine, $format->newLine());
$this->assertSame($hasFinalNewLine, $format->hasFinalNewLine());
}

Expand All @@ -82,7 +82,7 @@ public function testWithJsonEncodeOptionsRejectsInvalidJsonEncodeOptions(): void
$format = new Format(
\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES,
Indent::fromString(' '),
$this->prophesize(NewLineInterface::class)->reveal(),
NewLine::fromString("\r\n"),
true
);

Expand All @@ -96,7 +96,7 @@ public function testWithJsonEncodeOptionsClonesFormatAndSetsJsonEncodeOptions():
$format = new Format(
\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES,
Indent::fromString(' '),
$this->prophesize(NewLineInterface::class)->reveal(),
NewLine::fromString("\r\n"),
true
);

Expand All @@ -116,7 +116,7 @@ public function testWithIndentClonesFormatAndSetsIndent(): void
$format = new Format(
\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES,
Indent::fromString(' '),
$this->prophesize(NewLineInterface::class)->reveal(),
NewLine::fromString("\r\n"),
true
);

Expand All @@ -129,20 +129,20 @@ public function testWithIndentClonesFormatAndSetsIndent(): void

public function testWithNewLineClonesFormatAndSetsNewLine(): void
{
$newLine = $this->prophesize(NewLineInterface::class);
$newLine = NewLine::fromString("\r\n");

$format = new Format(
\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES,
Indent::fromString(' '),
$this->prophesize(NewLineInterface::class)->reveal(),
NewLine::fromString("\r"),
true
);

$mutated = $format->withNewLine($newLine->reveal());
$mutated = $format->withNewLine($newLine);

$this->assertInstanceOf(FormatInterface::class, $mutated);
$this->assertNotSame($format, $mutated);
$this->assertSame($newLine->reveal(), $mutated->newLine());
$this->assertSame($newLine, $mutated->newLine());
}

/**
Expand All @@ -155,7 +155,7 @@ public function testWithHasFinalNewLineClonesFormatAndSetsFinalNewLine(bool $has
$format = new Format(
\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES,
Indent::fromString(' '),
$this->prophesize(NewLineInterface::class)->reveal(),
NewLine::fromString("\r\n"),
false
);

Expand Down
12 changes: 3 additions & 9 deletions test/Unit/Format/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Localheinz\Json\Normalizer\Format\Formatter;
use Localheinz\Json\Normalizer\Format\FormatterInterface;
use Localheinz\Json\Normalizer\Format\Indent;
use Localheinz\Json\Normalizer\Format\NewLineInterface;
use Localheinz\Json\Normalizer\Format\NewLine;
use Localheinz\Json\Normalizer\JsonInterface;
use Localheinz\Json\Printer;
use Localheinz\Test\Util\Helper;
Expand Down Expand Up @@ -54,13 +54,7 @@ public function testFormatEncodesWithJsonEncodeOptionsIndentsAndPossiblySuffixes
]);

$indent = Indent::fromString($indentString);

$newLine = $this->prophesize(NewLineInterface::class);

$newLine
->__toString()
->shouldBeCalled()
->willReturn($newLineString);
$newLine = NewLine::fromString($newLineString);

$encoded = <<<'JSON'
{
Expand Down Expand Up @@ -105,7 +99,7 @@ public function testFormatEncodesWithJsonEncodeOptionsIndentsAndPossiblySuffixes
$format
->newLine()
->shouldBeCalled()
->willReturn($newLine->reveal());
->willReturn($newLine);

$format
->hasFinalNewLine()
Expand Down
11 changes: 1 addition & 10 deletions test/Unit/Format/NewLineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,13 @@

use Localheinz\Json\Normalizer\Exception;
use Localheinz\Json\Normalizer\Format\NewLine;
use Localheinz\Json\Normalizer\Format\NewLineInterface;
use Localheinz\Test\Util\Helper;
use PHPUnit\Framework;

/**
* @internal
*/
final class NewLineTest extends Framework\TestCase
{
use Helper;

public function testImplementsNewLineInterface(): void
{
$this->assertClassImplementsInterface(NewLineInterface::class, NewLine::class);
}

/**
* @dataProvider providerInvalidNewLineString
*
Expand Down Expand Up @@ -72,7 +63,7 @@ public function testFromStringReturnsNewLine(string $string): void
{
$newLine = NewLine::fromString($string);

$this->assertInstanceOf(NewLineInterface::class, $newLine);
$this->assertInstanceOf(NewLine::class, $newLine);
$this->assertSame($string, $newLine->__toString());
}

Expand Down