Skip to content

Commit

Permalink
Merge pull request #89 from localheinz/fix/indent
Browse files Browse the repository at this point in the history
Fix: Remove IndentInterface
  • Loading branch information
localheinz authored Oct 5, 2018
2 parents beb73a2 + fdfc29e commit 4e017fc
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 78 deletions.
12 changes: 6 additions & 6 deletions src/Format/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class Format implements FormatInterface
private $jsonEncodeOptions;

/**
* @var IndentInterface
* @var Indent
*/
private $indent;

Expand All @@ -40,13 +40,13 @@ final class Format implements FormatInterface

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

public function indent(): IndentInterface
public function indent(): Indent
{
return $this->indent;
}
Expand Down Expand Up @@ -103,7 +103,7 @@ public function withJsonEncodeOptions(int $jsonEncodeOptions): FormatInterface
return $mutated;
}

public function withIndent(IndentInterface $indent): FormatInterface
public function withIndent(Indent $indent): FormatInterface
{
$mutated = clone $this;

Expand Down Expand Up @@ -145,7 +145,7 @@ private static function detectJsonEncodeOptions(string $encoded): int
return $jsonEncodeOptions;
}

private static function detectIndent(string $encoded): IndentInterface
private static function detectIndent(string $encoded): Indent
{
if (1 === \preg_match('/^(?P<indent>( +|\t+)).*/m', $encoded, $match)) {
return Indent::fromString($match['indent']);
Expand Down
4 changes: 2 additions & 2 deletions src/Format/FormatInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface FormatInterface
{
public function jsonEncodeOptions(): int;

public function indent(): IndentInterface;
public function indent(): Indent;

public function newLine(): NewLineInterface;

Expand All @@ -34,7 +34,7 @@ public function hasFinalNewLine(): bool;
*/
public function withJsonEncodeOptions(int $jsonEncodeOptions): self;

public function withIndent(IndentInterface $indent): self;
public function withIndent(Indent $indent): self;

public function withNewLine(NewLineInterface $newLine): self;

Expand Down
10 changes: 5 additions & 5 deletions src/Format/Indent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Localheinz\Json\Normalizer\Exception;

final class Indent implements IndentInterface
final class Indent
{
/**
* @var string
Expand All @@ -37,9 +37,9 @@ public function __toString(): string
*
* @throws Exception\InvalidIndentStringException
*
* @return IndentInterface
* @return self
*/
public static function fromString(string $string): IndentInterface
public static function fromString(string $string): self
{
if (1 !== \preg_match('/^( *|\t+)$/', $string)) {
throw Exception\InvalidIndentStringException::fromString($string);
Expand All @@ -55,9 +55,9 @@ public static function fromString(string $string): IndentInterface
* @throws Exception\InvalidIndentSizeException
* @throws Exception\InvalidIndentStyleException
*
* @return IndentInterface
* @return self
*/
public static function fromSizeAndStyle(int $size, string $style): IndentInterface
public static function fromSizeAndStyle(int $size, string $style): self
{
$minimumSize = 1;

Expand Down
19 changes: 0 additions & 19 deletions src/Format/IndentInterface.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/IndentNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
final class IndentNormalizer implements NormalizerInterface
{
/**
* @var Format\IndentInterface
* @var Format\Indent
*/
private $indent;

Expand All @@ -27,7 +27,7 @@ final class IndentNormalizer implements NormalizerInterface
*/
private $printer;

public function __construct(Format\IndentInterface $indent, Printer\PrinterInterface $printer = null)
public function __construct(Format\Indent $indent, Printer\PrinterInterface $printer = null)
{
$this->indent = $indent;
$this->printer = $printer ?: new Printer\Printer();
Expand Down
28 changes: 14 additions & 14 deletions test/Unit/Format/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Localheinz\Json\Normalizer\Exception;
use Localheinz\Json\Normalizer\Format\Format;
use Localheinz\Json\Normalizer\Format\FormatInterface;
use Localheinz\Json\Normalizer\Format\IndentInterface;
use Localheinz\Json\Normalizer\Format\Indent;
use Localheinz\Json\Normalizer\Format\NewLineInterface;
use Localheinz\Json\Normalizer\JsonInterface;
use Localheinz\Test\Util\Helper;
Expand All @@ -37,15 +37,15 @@ public function testImplementsFormatInterface(): void
public function testConstructorRejectsInvalidJsonEncodeOptions(): void
{
$jsonEncodeOptions = -1;
$indent = $this->prophesize(IndentInterface::class);
$indent = Indent::fromString(' ');
$newLine = $this->prophesize(NewLineInterface::class);
$hasFinalNewLine = true;

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

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

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

$this->assertSame($jsonEncodeOptions, $format->jsonEncodeOptions());
$this->assertSame($indent->reveal(), $format->indent());
$this->assertSame($indent, $format->indent());
$this->assertSame($newLine->reveal(), $format->newLine());
$this->assertSame($hasFinalNewLine, $format->hasFinalNewLine());
}
Expand All @@ -81,7 +81,7 @@ public function testWithJsonEncodeOptionsRejectsInvalidJsonEncodeOptions(): void

$format = new Format(
\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES,
$this->prophesize(IndentInterface::class)->reveal(),
Indent::fromString(' '),
$this->prophesize(NewLineInterface::class)->reveal(),
true
);
Expand All @@ -95,7 +95,7 @@ public function testWithJsonEncodeOptionsClonesFormatAndSetsJsonEncodeOptions():
{
$format = new Format(
\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES,
$this->prophesize(IndentInterface::class)->reveal(),
Indent::fromString(' '),
$this->prophesize(NewLineInterface::class)->reveal(),
true
);
Expand All @@ -111,20 +111,20 @@ public function testWithJsonEncodeOptionsClonesFormatAndSetsJsonEncodeOptions():

public function testWithIndentClonesFormatAndSetsIndent(): void
{
$indent = $this->prophesize(IndentInterface::class);
$indent = Indent::fromString("\t");

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

$mutated = $format->withIndent($indent->reveal());
$mutated = $format->withIndent($indent);

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

public function testWithNewLineClonesFormatAndSetsNewLine(): void
Expand All @@ -133,7 +133,7 @@ public function testWithNewLineClonesFormatAndSetsNewLine(): void

$format = new Format(
\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES,
$this->prophesize(IndentInterface::class)->reveal(),
Indent::fromString(' '),
$this->prophesize(NewLineInterface::class)->reveal(),
true
);
Expand All @@ -154,7 +154,7 @@ public function testWithHasFinalNewLineClonesFormatAndSetsFinalNewLine(bool $has
{
$format = new Format(
\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES,
$this->prophesize(IndentInterface::class)->reveal(),
Indent::fromString(' '),
$this->prophesize(NewLineInterface::class)->reveal(),
false
);
Expand Down
11 changes: 3 additions & 8 deletions test/Unit/Format/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Localheinz\Json\Normalizer\Format\FormatInterface;
use Localheinz\Json\Normalizer\Format\Formatter;
use Localheinz\Json\Normalizer\Format\FormatterInterface;
use Localheinz\Json\Normalizer\Format\IndentInterface;
use Localheinz\Json\Normalizer\Format\Indent;
use Localheinz\Json\Normalizer\Format\NewLineInterface;
use Localheinz\Json\Normalizer\JsonInterface;
use Localheinz\Json\Printer;
Expand Down Expand Up @@ -53,12 +53,7 @@ public function testFormatEncodesWithJsonEncodeOptionsIndentsAndPossiblySuffixes
"\r",
]);

$indent = $this->prophesize(IndentInterface::class);

$indent
->__toString()
->shouldBeCalled()
->willReturn($indentString);
$indent = Indent::fromString($indentString);

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

Expand Down Expand Up @@ -105,7 +100,7 @@ public function testFormatEncodesWithJsonEncodeOptionsIndentsAndPossiblySuffixes
$format
->indent()
->shouldBeCalled()
->willReturn($indent->reveal());
->willReturn($indent);

$format
->newLine()
Expand Down
10 changes: 2 additions & 8 deletions test/Unit/Format/IndentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Localheinz\Json\Normalizer\Exception;
use Localheinz\Json\Normalizer\Format\Indent;
use Localheinz\Json\Normalizer\Format\IndentInterface;
use Localheinz\Test\Util\Helper;
use PHPUnit\Framework;

Expand All @@ -26,11 +25,6 @@ final class IndentTest extends Framework\TestCase
{
use Helper;

public function testImplementsIndentInterface(): void
{
$this->assertClassImplementsInterface(IndentInterface::class, Indent::class);
}

/**
* @dataProvider providerInvalidSize
*
Expand Down Expand Up @@ -95,7 +89,7 @@ public function testFromSizeAndStyleReturnsIndent(int $size, string $style, stri
$style
);

$this->assertInstanceOf(IndentInterface::class, $indent);
$this->assertInstanceOf(Indent::class, $indent);

$this->assertSame($string, $indent->__toString());
}
Expand Down Expand Up @@ -154,7 +148,7 @@ public function testFromStringReturnsIndent(string $string): void
{
$indent = Indent::fromString($string);

$this->assertInstanceOf(IndentInterface::class, $indent);
$this->assertInstanceOf(Indent::class, $indent);

$this->assertSame($string, $indent->__toString());
}
Expand Down
18 changes: 4 additions & 14 deletions test/Unit/IndentNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Localheinz\Json\Normalizer\Test\Unit;

use Localheinz\Json\Normalizer\Format\IndentInterface;
use Localheinz\Json\Normalizer\Format\Indent;
use Localheinz\Json\Normalizer\IndentNormalizer;
use Localheinz\Json\Normalizer\JsonInterface;
use Localheinz\Json\Printer\PrinterInterface;
Expand All @@ -26,17 +26,7 @@ final class IndentNormalizerTest extends AbstractNormalizerTestCase
{
public function testNormalizeUsesPrinterToNormalizeJsonWithIndent(): void
{
$indentString = $this->faker()->randomElement([
' ',
"\t",
]);

$indent = $this->prophesize(IndentInterface::class);

$indent
->__toString()
->shouldBeCalled()
->willReturn($indentString);
$indent = Indent::fromString(' ');

$encoded = <<<'JSON'
{
Expand Down Expand Up @@ -64,13 +54,13 @@ public function testNormalizeUsesPrinterToNormalizeJsonWithIndent(): void
$printer
->print(
Argument::is($encoded),
Argument::is($indentString)
Argument::is($indent->__toString())
)
->shouldBeCalled()
->willReturn($indented);

$normalizer = new IndentNormalizer(
$indent->reveal(),
$indent,
$printer->reveal()
);

Expand Down

0 comments on commit 4e017fc

Please sign in to comment.