Skip to content

Commit

Permalink
Closes #67
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Bergmann <sb@sebastian-bergmann.de>
Co-authored-by: Fabian Blechschmidt <github@fabian-blechschmidt.de>
Co-authored-by: Arne Blankerts <arne@blankerts.de>
Co-authored-by: Markus Staab <markus.staab@redaxo.de>
Co-authored-by: Nicola Pilcher <nicola.pilcher@gmail.com>
Co-authored-by: Andreas Möller <am@localheinz.com>
Co-authored-by: Sebastian Heuer <sebastian@phpeople.de>
  • Loading branch information
7 people committed Oct 19, 2024
1 parent 8a1c22c commit bba3ca3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
7 changes: 7 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.

## [6.2.0] - 2024-MM-DD

### Added

* [#67](https://github.com/sebastianbergmann/exporter/issues/67): Optional argument for `shortenedExport()` to control maximum string length

## [6.1.3] - 2024-07-03

### Changed
Expand Down Expand Up @@ -158,6 +164,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt

* Remove HHVM-specific code that is no longer needed

[6.2.0]: https://github.com/sebastianbergmann/exporter/compare/6.1.3...main
[6.1.3]: https://github.com/sebastianbergmann/exporter/compare/6.1.2...6.1.3
[6.1.2]: https://github.com/sebastianbergmann/exporter/compare/6.1.1...6.1.2
[6.1.1]: https://github.com/sebastianbergmann/exporter/compare/6.1.0...6.1.1
Expand Down
8 changes: 5 additions & 3 deletions src/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,16 @@ public function shortenedRecursiveExport(array &$data, ?RecursionContext $proces
*
* Newlines are replaced by the visible string '\n'.
* Contents of arrays and objects (if any) are replaced by '...'.
*
* @param positive-int $maxLengthForStrings
*/
public function shortenedExport(mixed $value): string
public function shortenedExport(mixed $value, int $maxLengthForStrings = 40): string
{
if (is_string($value)) {
$string = str_replace("\n", '', $this->exportString($value));

if (mb_strlen($string) > 40) {
return mb_substr($string, 0, 30) . '...' . mb_substr($string, -7);
if (mb_strlen($string) > $maxLengthForStrings) {
return mb_substr($string, 0, $maxLengthForStrings - 10) . '...' . mb_substr($string, -7);
}

return $string;
Expand Down
34 changes: 19 additions & 15 deletions tests/ExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,19 +324,20 @@ public static function shortenedExportProvider(): array
'float 1 - 2 / 3' => [1 - 2 / 3, '0.33333333333333337'],
'numeric string' => ['1', "'1'"],
// \n\r and \r is converted to \n
'38 single-byte characters' => [str_repeat('A', 38), '\'' . str_repeat('A', 38) . '\''],
'39 single-byte characters' => [str_repeat('A', 39), '\'' . str_repeat('A', 29) . '...' . str_repeat('A', 6) . '\''],
'38 multi-byte characters' => [str_repeat('🧪', 38), '\'' . str_repeat('🧪', 38) . '\''],
'39 multi-byte characters' => [str_repeat('🧪', 39), '\'' . str_repeat('🧪', 29) . '...' . str_repeat('🧪', 6) . '\''],
'multi-line string' => ["this\nis\na\nvery\nvery\nvery\nvery\nvery\nvery\rlong\n\rtext", "'this\\nis\\na\\nvery\\nvery\\nvery...\\rtext'"],
'empty stdClass' => [new stdClass, 'stdClass Object ()'],
'not empty stdClass' => [$obj, 'stdClass Object (...)'],
'empty array' => [[], '[]'],
'not empty array' => [$array, '[...]'],
'enum' => [ExampleEnum::Value, 'SebastianBergmann\Exporter\ExampleEnum Enum (Value)'],
'backed enum (string)' => [ExampleStringBackedEnum::Value, 'SebastianBergmann\Exporter\ExampleStringBackedEnum Enum (Value, \'value\')'],
'backen enum (integer)' => [ExampleIntegerBackedEnum::Value, 'SebastianBergmann\Exporter\ExampleIntegerBackedEnum Enum (Value, 0)'],
'recursive array' => [$recursiveArray, '[...]', 0],
'38 single-byte characters' => [str_repeat('A', 38), '\'' . str_repeat('A', 38) . '\''],
'39 single-byte characters' => [str_repeat('A', 39), '\'' . str_repeat('A', 29) . '...' . str_repeat('A', 6) . '\''],
'38 multi-byte characters' => [str_repeat('🧪', 38), '\'' . str_repeat('🧪', 38) . '\''],
'39 multi-byte characters' => [str_repeat('🧪', 39), '\'' . str_repeat('🧪', 29) . '...' . str_repeat('🧪', 6) . '\''],
'string longer than custom maximum string length' => [str_repeat('A', 21), '\'' . str_repeat('A', 9) . '...' . str_repeat('A', 6) . '\'', 20],
'multi-line string' => ["this\nis\na\nvery\nvery\nvery\nvery\nvery\nvery\rlong\n\rtext", "'this\\nis\\na\\nvery\\nvery\\nvery...\\rtext'"],
'empty stdClass' => [new stdClass, 'stdClass Object ()'],
'not empty stdClass' => [$obj, 'stdClass Object (...)'],
'empty array' => [[], '[]'],
'not empty array' => [$array, '[...]'],
'enum' => [ExampleEnum::Value, 'SebastianBergmann\Exporter\ExampleEnum Enum (Value)'],
'backed enum (string)' => [ExampleStringBackedEnum::Value, 'SebastianBergmann\Exporter\ExampleStringBackedEnum Enum (Value, \'value\')'],
'backen enum (integer)' => [ExampleIntegerBackedEnum::Value, 'SebastianBergmann\Exporter\ExampleIntegerBackedEnum Enum (Value, 0)'],
'recursive array' => [$recursiveArray, '[...]', 0],
];
}

Expand Down Expand Up @@ -472,12 +473,15 @@ public function testExport2(): void
);
}

/**
* @param positive-int $maxLengthForStrings
*/
#[DataProvider('shortenedExportProvider')]
public function testShortenedExport(mixed $value, string $expected): void
public function testShortenedExport(mixed $value, string $expected, int $maxLengthForStrings = 40): void
{
$this->assertSame(
$expected,
$this->trimNewline((new Exporter)->shortenedExport($value)),
$this->trimNewline((new Exporter)->shortenedExport($value, $maxLengthForStrings)),
);
}

Expand Down

0 comments on commit bba3ca3

Please sign in to comment.