Skip to content

Commit 6243066

Browse files
committed
Change how to stringify arrays
Currently, we're using "{}" around arrays, but that's not intuitive since we declare arrays with "[]" on PHP. Signed-off-by: Henrique Moody <[email protected]>
1 parent 228b8a3 commit 6243066

File tree

10 files changed

+249
-208
lines changed

10 files changed

+249
-208
lines changed

src/Stringifiers/ArrayStringifier.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222

2323
final class ArrayStringifier implements Stringifier
2424
{
25+
private const LIMIT_EXCEEDED_PLACEHOLDER = '...';
26+
2527
public function __construct(
2628
private readonly Stringifier $stringifier,
2729
private readonly Quoter $quoter,
2830
private readonly int $maximumDepth,
29-
private readonly int $itemsLimit
31+
private readonly int $maximumNumberOfItems
3032
) {
3133
}
3234

@@ -37,30 +39,40 @@ public function stringify(mixed $raw, int $depth): ?string
3739
}
3840

3941
if (empty($raw)) {
40-
return $this->quoter->quote('{ }', $depth);
42+
return $this->quoter->quote('[]', $depth);
4143
}
4244

4345
if ($depth >= $this->maximumDepth) {
44-
return '...';
46+
return $this->quoter->quote(self::LIMIT_EXCEEDED_PLACEHOLDER, $depth);
4547
}
4648

4749
$items = [];
48-
$itemsCount = 0;
4950
$isSequential = $this->isSequential($raw);
5051
foreach ($raw as $key => $value) {
51-
if (++$itemsCount > $this->itemsLimit) {
52-
$items[$itemsCount] = '...';
52+
if (count($items) >= $this->maximumNumberOfItems) {
53+
$items[] = self::LIMIT_EXCEEDED_PLACEHOLDER;
5354
break;
5455
}
5556

56-
$items[$itemsCount] = '';
57-
if ($isSequential === false) {
58-
$items[$itemsCount] .= sprintf('%s: ', $this->stringifier->stringify($key, $depth + 1));
57+
$stringifiedValue = $this->stringifyKeyValue($value, $depth + 1);
58+
if ($isSequential === true) {
59+
$items[] = $stringifiedValue;
60+
continue;
5961
}
60-
$items[$itemsCount] .= $this->stringifier->stringify($value, $depth + 1);
62+
63+
$items[] = sprintf('%s: %s', $this->stringifier->stringify($key, $depth + 1), $stringifiedValue);
64+
}
65+
66+
return $this->quoter->quote(sprintf('[%s]', implode(', ', $items)), $depth);
67+
}
68+
69+
private function stringifyKeyValue(mixed $value, int $depth): ?string
70+
{
71+
if (is_array($value)) {
72+
return $this->stringify($value, $depth);
6173
}
6274

63-
return $this->quoter->quote(sprintf('{ %s }', implode(', ', $items)), $depth);
75+
return $this->stringifier->stringify($value, $depth);
6476
}
6577

6678
/**

src/Stringifiers/ClusterStringifier.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
final class ClusterStringifier implements Stringifier
1717
{
18+
private const MAXIMUM_DEPTH = 3;
19+
private const MAXIMUM_NUMBER_OF_ITEMS = 5;
20+
1821
/**
1922
* @var Stringifier[]
2023
*/
@@ -40,7 +43,7 @@ public static function createDefault(): self
4043
new StringableObjectStringifier($stringifier),
4144
new JsonSerializableStringifier($stringifier, $quoter),
4245
new ObjectStringifier($stringifier, $quoter),
43-
new ArrayStringifier($stringifier, $quoter, 3, 5),
46+
new ArrayStringifier($stringifier, $quoter, self::MAXIMUM_DEPTH, self::MAXIMUM_NUMBER_OF_ITEMS),
4447
new InfiniteStringifier($quoter),
4548
new NanStringifier($quoter),
4649
new ResourceStringifier($quoter),

tests/integration/stringify-array.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ output([
2525
]);
2626
?>
2727
--EXPECT--
28-
`{ 1, NULL, { 1.0, { [resource] (stream), ..., { } } }, FALSE, [object] (stdClass: { }), ... }`
28+
`[1, NULL, [1.0, [[resource] (stream), ..., []]], FALSE, [object] (stdClass: []), ...]`

tests/integration/stringify-object-jsonSerializable.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ require 'vendor/autoload.php';
88
output(new Respect\Stringifier\Test\MyJsonSerializable());
99
?>
1010
--EXPECT--
11-
`[json-serializable] (Respect\Stringifier\Test\MyJsonSerializable: { 1, 2, 3 })`
11+
`[json-serializable] (Respect\Stringifier\Test\MyJsonSerializable: [1, 2, 3])`

tests/integration/stringify-object-throwable.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ require 'vendor/autoload.php';
88
output(new InvalidArgumentException());
99
?>
1010
--EXPECTF--
11-
`[throwable] (InvalidArgumentException: { "message": "", "code": 0, "file": "%s:%d" })`
11+
`[throwable] (InvalidArgumentException: ["message": "", "code": 0, "file": "%s:%d"])`

tests/integration/stringify-object-traversable.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ require 'vendor/autoload.php';
88
output(new Respect\Stringifier\Test\MyTraversable());
99
?>
1010
--EXPECT--
11-
`[traversable] (Respect\Stringifier\Test\MyTraversable: { 1, 2, 3 })`
11+
`[traversable] (Respect\Stringifier\Test\MyTraversable: [1, 2, 3])`

tests/integration/stringify-object.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ require 'vendor/autoload.php';
88
output(new Respect\Stringifier\Test\MyObject());
99
?>
1010
--EXPECT--
11-
`[object] (Respect\Stringifier\Test\MyObject: { "foo": TRUE })`
11+
`[object] (Respect\Stringifier\Test\MyObject: ["foo": TRUE])`

tests/src/Double/FakeQuoter.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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\Double;
12+
13+
use Respect\Stringifier\Quoter;
14+
15+
use function sprintf;
16+
17+
final class FakeQuoter implements Quoter
18+
{
19+
private const SYMBOL = '#';
20+
21+
public function quote(string $string, int $depth): string
22+
{
23+
return sprintf('%1$s%2$s%1$s%3$d', self::SYMBOL, $string, $depth);
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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\Double;
12+
13+
use Respect\Stringifier\Stringifier;
14+
15+
use function hash;
16+
use function implode;
17+
use function serialize;
18+
19+
final class FakeStringifier implements Stringifier
20+
{
21+
public function stringify(mixed $raw, int $depth): ?string
22+
{
23+
return implode('.', ['fake', $depth, hash('crc32', serialize($raw))]);
24+
}
25+
}

0 commit comments

Comments
 (0)