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

Enhancement: Extract constant for indent characters #384

Merged
merged 1 commit into from
Oct 31, 2020
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
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

For a full diff see [`0.13.1...main`][0.13.1...main].
For a full diff see [`0.14.0...main`][0.14.0...main].

## [`0.14.0`][0.14.0]

For a full diff see [`0.13.1...0.14.0`][0.13.1...0.14.0].

### Added

* Extracted an `Indent::CHARACTERS` constant that exposes a map of indent styles to indent characters ([#383]), by [@localheinz]

## [`0.13.1`][0.13.1]

Expand Down Expand Up @@ -266,6 +274,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[0.12.0]: https://github.com/ergebnis/json-normalizer/releases/tag/0.12.0
[0.13.0]: https://github.com/ergebnis/json-normalizer/releases/tag/0.13.0
[0.13.1]: https://github.com/ergebnis/json-normalizer/releases/tag/0.13.1
[0.14.0]: https://github.com/ergebnis/json-normalizer/releases/tag/0.14.0

[5d8b3e2...0.1.0]: https://github.com/ergebnis/json-normalizer/compare/5d8b3e2...0.1.0
[0.1.0...0.2.0]: https://github.com/ergebnis/json-normalizer/compare/0.1.0...0.2.0
Expand All @@ -284,7 +293,8 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[0.11.0...0.12.0]: https://github.com/ergebnis/json-normalizer/compare/0.11.0...0.12.0
[0.12.0...0.13.0]: https://github.com/ergebnis/json-normalizer/compare/0.12.0...0.13.0
[0.13.0...0.13.1]: https://github.com/ergebnis/json-normalizer/compare/0.13.0...0.13.1
[0.13.1...main]: https://github.com/ergebnis/json-normalizer/compare/0.13.1...main
[0.13.1...0.14.0]: https://github.com/ergebnis/json-normalizer/compare/0.13.1...0.14.0
[0.14.0...main]: https://github.com/ergebnis/json-normalizer/compare/0.14.0...main

[#1]: https://github.com/ergebnis/json-normalizer/pull/1
[#2]: https://github.com/ergebnis/json-normalizer/pull/2
Expand Down Expand Up @@ -345,6 +355,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[#269]: https://github.com/ergebnis/json-normalizer/pull/269
[#308]: https://github.com/ergebnis/json-normalizer/pull/308
[#335]: https://github.com/ergebnis/json-normalizer/pull/335
[#383]: https://github.com/ergebnis/json-normalizer/pull/383

[@BackEndTea]: https://github.com/BackEndTea
[@ergebnis]: https://github.com/ergebnis
Expand Down
17 changes: 8 additions & 9 deletions src/Format/Indent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

final class Indent
{
public const CHARACTERS = [
'space' => ' ',
'tab' => "\t",
];

/**
* @var string
*/
Expand Down Expand Up @@ -60,21 +65,15 @@ public static function fromSizeAndStyle(int $size, string $style): self
);
}

/** @var array<string, string> $characters */
$characters = [
'space' => ' ',
'tab' => "\t",
];

if (!\array_key_exists($style, $characters)) {
if (!\array_key_exists($style, self::CHARACTERS)) {
throw Exception\InvalidIndentStyleException::fromStyleAndAllowedStyles(
$style,
...\array_keys($characters)
...\array_keys(self::CHARACTERS)
);
}

$value = \str_repeat(
$characters[$style],
self::CHARACTERS[$style],
$size
);

Expand Down
29 changes: 17 additions & 12 deletions test/Unit/Format/IndentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,22 @@ final class IndentTest extends Framework\TestCase
{
use Helper;

public function testConstants(): void
{
$characters = [
'space' => ' ',
'tab' => "\t",
];

self::assertSame($characters, Indent::CHARACTERS);
}

/**
* @dataProvider providerInvalidSize
*/
public function testFromSizeAndStyleRejectsInvalidSize(int $size): void
{
$style = self::faker()->randomElement([
'space',
'tab',
]);
$style = self::faker()->randomElement(\array_keys(Indent::CHARACTERS));

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

Expand Down Expand Up @@ -103,7 +110,7 @@ public function testFromSizeAndStyleReturnsIndent(int $size, string $style, stri
public function providerSizeStyleAndIndentString(): \Generator
{
foreach (self::sizes() as $key => $size) {
foreach (self::characters() as $style => $character) {
foreach (Indent::CHARACTERS as $style => $character) {
$string = \str_repeat(
$character,
$size
Expand Down Expand Up @@ -162,7 +169,7 @@ public function testFromStringReturnsIndent(string $string): void
public function providerValidIndentString(): \Generator
{
foreach (self::sizes() as $key => $size) {
foreach (self::characters() as $style => $character) {
foreach (Indent::CHARACTERS as $style => $character) {
$string = \str_repeat(
$character,
$size
Expand Down Expand Up @@ -226,14 +233,12 @@ public function testFromJsonReturnsIndentSniffedFromObject(string $actualIndent,
*/
public function providerPureIndentAndSniffedIndent(): \Generator
{
$characters = [
'space' => ' ',
'tab' => "\t",
$sizes = [
1,
3,
];

$sizes = [1, 3];

foreach ($characters as $style => $character) {
foreach (Indent::CHARACTERS as $style => $character) {
foreach ($sizes as $size) {
$key = \sprintf(
'%s-%d',
Expand Down