Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 4 additions & 33 deletions src/Quoters/CodeQuoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,15 @@

use Respect\Stringifier\Quoter;

use function mb_strlen;
use function mb_substr;
use function sprintf;
use function str_contains;
use function strpos;

final class CodeQuoter implements Quoter
{
private const string OBJECT_PLACEHOLDER = ' ... }';
private const string ARRAY_PLACEHOLDER = ' ... ]';
private const string GENERIC_PLACEHOLDER = ' ...';
private readonly LimiterQuoter $limiter;

public function __construct(private readonly int $maximumLength)
public function __construct(int $maximumLength)
{
$this->limiter = new LimiterQuoter($maximumLength - 2);
}

public function quote(string $string, int $depth): string
Expand All @@ -34,30 +29,6 @@ public function quote(string $string, int $depth): string
return $string;
}

$limitWithQuotes = $this->maximumLength - 2;
if (mb_strlen($string) <= $limitWithQuotes) {
return $this->code($string);
}

$filtered = mb_substr($string, 0, $limitWithQuotes);
if (strpos($filtered, '[') === 0) {
return $this->placeholder($filtered, self::ARRAY_PLACEHOLDER);
}

if (str_contains($filtered, '{')) {
return $this->placeholder($filtered, self::OBJECT_PLACEHOLDER);
}

return $this->placeholder($filtered, self::GENERIC_PLACEHOLDER);
}

private function code(string $string): string
{
return sprintf('`%s`', $string);
}

private function placeholder(string $string, string $placeholder): string
{
return $this->code(mb_substr($string, 0, -1 * mb_strlen($placeholder)) . $placeholder);
return sprintf('`%s`', $this->limiter->quote($string, $depth));
}
}
52 changes: 52 additions & 0 deletions src/Quoters/LimiterQuoter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of Respect/Stringifier.
* Copyright (c) Henrique Moody <henriquemoody@gmail.com>
* SPDX-License-Identifier: MIT
*/

declare(strict_types=1);

namespace Respect\Stringifier\Quoters;

use Respect\Stringifier\Quoter;

use function mb_strlen;
use function mb_substr;
use function str_contains;
use function strpos;

final class LimiterQuoter implements Quoter
{
private const string OBJECT_PLACEHOLDER = ' ... }';
private const string ARRAY_PLACEHOLDER = ' ... ]';
private const string GENERIC_PLACEHOLDER = ' ...';

public function __construct(private readonly int $maximumLength)
{
}

public function quote(string $string, int $depth): string
{
if (mb_strlen($string) <= $this->maximumLength) {
return $string;
}

$filtered = mb_substr($string, 0, $this->maximumLength);
if (strpos($filtered, '[') === 0) {
return $this->truncate($filtered, self::ARRAY_PLACEHOLDER);
}

if (str_contains($filtered, '{')) {
return $this->truncate($filtered, self::OBJECT_PLACEHOLDER);
}

return $this->truncate($filtered, self::GENERIC_PLACEHOLDER);
}

private function truncate(string $string, string $placeholder): string
{
return mb_substr($string, 0, -1 * mb_strlen($placeholder)) . $placeholder;
}
}
49 changes: 49 additions & 0 deletions tests/unit/Quoters/LimiterQuoterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of Respect/Stringifier.
* Copyright (c) Henrique Moody <henriquemoody@gmail.com>
* SPDX-License-Identifier: MIT
*/

declare(strict_types=1);

namespace Respect\Stringifier\Test\Unit\Quoters;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Respect\Stringifier\Quoters\LimiterQuoter;

use function mb_strlen;

#[CoversClass(LimiterQuoter::class)]
final class LimiterQuoterTest extends TestCase
{
private const int LIMIT = 18;

#[Test]
#[DataProvider('provideData')]
public function itShouldLimitString(string $string, string $expected): void
{
$sut = new LimiterQuoter(self::LIMIT);

$actual = $sut->quote($string, 0);

self::assertSame($expected, $actual);
self::assertLessThanOrEqual(self::LIMIT, mb_strlen($actual));
}

/** @return array<int, array<int, string>> */
public static function provideData(): array
{
return [
['short string', 'short string'],
['1234567890ABCDEFGH', '1234567890ABCDEFGH'],
['1234567890ABCDEFGHI', '1234567890ABCD ...'],
['class { 90ABCDEFGH }', 'class { 90AB ... }'],
['[2, 5, 7, A, D, G, H]', '[2, 5, 7, A, ... ]'],
];
}
}
Loading