Skip to content

Commit 86ae598

Browse files
committed
Limit the number of characters to 120
The stringified values can get quite long, so I decided to limit the number of characters, and I've chosen an arbitrary number of 120 that felt good to me. Signed-off-by: Henrique Moody <[email protected]>
1 parent c457629 commit 86ae598

File tree

5 files changed

+131
-71
lines changed

5 files changed

+131
-71
lines changed

src/Quoters/CodeQuoter.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Quoters/StandardQuoter.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Respect/Stringifier.
5+
* Copyright (c) Henrique Moody <[email protected]>
6+
* SPDX-License-Identifier: MIT
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Respect\Stringifier\Quoters;
12+
13+
use Respect\Stringifier\Quoter;
14+
15+
use function mb_strlen;
16+
use function mb_substr;
17+
use function sprintf;
18+
use function str_contains;
19+
use function strpos;
20+
21+
final class StandardQuoter implements Quoter
22+
{
23+
private const OBJECT_PLACEHOLDER = ' ... }';
24+
private const ARRAY_PLACEHOLDER = ' ... ]';
25+
private const GENERIC_PLACEHOLDER = ' ...';
26+
27+
public function __construct(private readonly int $maximumLength)
28+
{
29+
}
30+
31+
public function quote(string $string, int $depth): string
32+
{
33+
if ($depth > 0) {
34+
return $string;
35+
}
36+
37+
$limitWithQuotes = $this->maximumLength - 2;
38+
if (mb_strlen($string) <= $limitWithQuotes) {
39+
return $this->code($string);
40+
}
41+
42+
$filtered = mb_substr($string, 0, $limitWithQuotes);
43+
if (strpos($filtered, '[') === 0) {
44+
return $this->placeholder($filtered, self::ARRAY_PLACEHOLDER);
45+
}
46+
47+
if (str_contains($filtered, '{')) {
48+
return $this->placeholder($filtered, self::OBJECT_PLACEHOLDER);
49+
}
50+
51+
return $this->placeholder($filtered, self::GENERIC_PLACEHOLDER);
52+
}
53+
54+
private function code(string $string): string
55+
{
56+
return sprintf('`%s`', $string);
57+
}
58+
59+
private function placeholder(string $string, string $placeholder): string
60+
{
61+
return $this->code(mb_substr($string, 0, -1 * mb_strlen($placeholder)) . $placeholder);
62+
}
63+
}

src/Stringifiers/CompositeStringifier.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace Respect\Stringifier\Stringifiers;
1212

1313
use DateTimeInterface;
14-
use Respect\Stringifier\Quoters\CodeQuoter;
14+
use Respect\Stringifier\Quoters\StandardQuoter;
1515
use Respect\Stringifier\Stringifier;
1616

1717
use function array_unshift;
@@ -21,6 +21,7 @@ final class CompositeStringifier implements Stringifier
2121
private const MAXIMUM_DEPTH = 3;
2222
private const MAXIMUM_NUMBER_OF_ITEMS = 5;
2323
private const MAXIMUM_NUMBER_OF_PROPERTIES = self::MAXIMUM_NUMBER_OF_ITEMS;
24+
private const MAXIMUM_LENGTH = 120;
2425

2526
/**
2627
* @var Stringifier[]
@@ -34,7 +35,7 @@ public function __construct(Stringifier ...$stringifiers)
3435

3536
public static function createDefault(): self
3637
{
37-
$quoter = new CodeQuoter();
38+
$quoter = new StandardQuoter(self::MAXIMUM_LENGTH);
3839

3940
$stringifier = new self(
4041
new InfiniteNumberStringifier($quoter),

tests/unit/Quoters/CodeQuoterTest.php

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Respect/Stringifier.
5+
* Copyright (c) Henrique Moody <[email protected]>
6+
* SPDX-License-Identifier: MIT
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Respect\Stringifier\Test\Unit\Quoters;
12+
13+
use PHPUnit\Framework\Attributes\CoversClass;
14+
use PHPUnit\Framework\Attributes\DataProvider;
15+
use PHPUnit\Framework\Attributes\Test;
16+
use PHPUnit\Framework\TestCase;
17+
use Respect\Stringifier\Quoters\StandardQuoter;
18+
19+
use function strlen;
20+
21+
#[CoversClass(StandardQuoter::class)]
22+
final class StandardQuoterTest extends TestCase
23+
{
24+
private const LIMIT = 20;
25+
26+
#[Test]
27+
public function itShouldNotQuoteWhenDepthIsBiggerThanZero(): void
28+
{
29+
$quoter = new StandardQuoter(self::LIMIT);
30+
31+
$expectedValue = 'code';
32+
$actualValue = $quoter->quote('code', 1);
33+
34+
self::assertSame($expectedValue, $actualValue);
35+
}
36+
37+
#[Test]
38+
#[DataProvider('provideData')]
39+
public function isShouldQuoteWhenDepthIsBiggerThanZero(string $string, string $expected): void
40+
{
41+
$sut = new StandardQuoter(self::LIMIT);
42+
43+
$actual = $sut->quote($string, 0);
44+
45+
self::assertSame($expected, $actual);
46+
self::assertLessThanOrEqual(self::LIMIT, strlen($actual));
47+
}
48+
49+
/**
50+
* @return array<int, array<int, string>>
51+
*/
52+
public static function provideData(): array
53+
{
54+
return [
55+
['É isso aí', '`É isso aí`'],
56+
['123456789', '`123456789`'],
57+
['1234567890ABCDEFGH', '`1234567890ABCDEFGH`'],
58+
['1234567890ABCDEFGHI', '`1234567890ABCD ...`'],
59+
['class { 90ABCDEF }', '`class { 90ABCDEF }`'],
60+
['class { 90ABCDEFGH }', '`class { 90AB ... }`'],
61+
['[2, 5, 7, A, D, G]', '`[2, 5, 7, A, D, G]`'],
62+
['[2, 5, 7, A, D, G, H]', '`[2, 5, 7, A, ... ]`'],
63+
];
64+
}
65+
}

0 commit comments

Comments
 (0)