Skip to content

Commit c80ff8a

Browse files
committed
Array should have quotes around itself
Arrays are not scalar values, therefore they should have quotes around it, like `{ }`.
1 parent 3b6ada5 commit c80ff8a

4 files changed

Lines changed: 72 additions & 15 deletions

File tree

src/Stringifiers/ArrayStringifier.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use function is_array;
1919
use function is_int;
2020
use function sprintf;
21+
use Respect\Stringifier\Quoter;
2122
use Respect\Stringifier\Stringifier;
2223

2324
/**
@@ -32,6 +33,11 @@ final class ArrayStringifier implements Stringifier
3233
*/
3334
private $stringifier;
3435

36+
/**
37+
* @var Quoter
38+
*/
39+
private $quoter;
40+
3541
/**
3642
* @var int
3743
*/
@@ -46,12 +52,14 @@ final class ArrayStringifier implements Stringifier
4652
* Initializes the stringifier.
4753
*
4854
* @param Stringifier $stringifier
55+
* @param Quoter $quoter
4956
* @param int $maximumDepth
5057
* @param int $itemsLimit
5158
*/
52-
public function __construct(Stringifier $stringifier, int $maximumDepth, int $itemsLimit)
59+
public function __construct(Stringifier $stringifier, Quoter $quoter, int $maximumDepth, int $itemsLimit)
5360
{
5461
$this->stringifier = $stringifier;
62+
$this->quoter = $quoter;
5563
$this->maximumDepth = $maximumDepth;
5664
$this->itemsLimit = $itemsLimit;
5765
}
@@ -66,7 +74,7 @@ public function stringify($raw, int $depth): ?string
6674
}
6775

6876
if (empty($raw)) {
69-
return '{ }';
77+
return $this->quoter->quote('{ }', $depth);
7078
}
7179

7280
if ($depth >= $this->maximumDepth) {
@@ -89,7 +97,7 @@ public function stringify($raw, int $depth): ?string
8997
$items[$itemsCount] .= $this->stringifier->stringify($value, $depth + 1);
9098
}
9199

92-
return sprintf('{ %s }', implode(', ', $items));
100+
return $this->quoter->quote(sprintf('{ %s }', implode(', ', $items)), $depth);
93101
}
94102

95103
private function isSequential(array $raw): bool

src/Stringifiers/ClusterStringifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static function createDefault(): self
5858
new StringableObjectStringifier($stringifier),
5959
new JsonSerializableStringifier($stringifier, $quoter),
6060
new ObjectStringifier($stringifier, $quoter),
61-
new ArrayStringifier($stringifier, 3, 5),
61+
new ArrayStringifier($stringifier, $quoter, 3, 5),
6262
new InfiniteStringifier($quoter),
6363
new NanStringifier($quoter),
6464
new ResourceStringifier($quoter),

tests/integration/stringify-array.phpt

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

tests/unit/Stringifiers/ArrayStringifierTest.php

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function shouldNotConvertToStringWhenRawValueIsNotAnArray(): void
3939
->expects($this->never())
4040
->method('quote');
4141

42-
$arrayStringifier = new ArrayStringifier($stringifierMock, 3, 5 );
42+
$arrayStringifier = new ArrayStringifier($stringifierMock, $quoterMock, 3, 5 );
4343

4444
self::assertNull($arrayStringifier->stringify($raw, $depth));
4545
}
@@ -65,7 +65,7 @@ public function shouldReturnAPlaceHolderWhenDepthIsEqualsToMaximumDepth(): void
6565
->expects($this->never())
6666
->method('quote');
6767

68-
$arrayStringifier = new ArrayStringifier($stringifierMock, $maximumDepth, 5);
68+
$arrayStringifier = new ArrayStringifier($stringifierMock, $quoterMock, $maximumDepth, 5);
6969

7070
self::assertSame($expected, $arrayStringifier->stringify($raw, $depth));
7171
}
@@ -91,7 +91,7 @@ public function shouldReturnAPlaceHolderWhenDepthIsBiggerThanMaximumDepth(): voi
9191
->expects($this->never())
9292
->method('quote');
9393

94-
$arrayStringifier = new ArrayStringifier($stringifierMock, $maximumDepth, 5);
94+
$arrayStringifier = new ArrayStringifier($stringifierMock, $quoterMock, $maximumDepth, 5);
9595

9696
self::assertSame($expected, $arrayStringifier->stringify($raw, $depth));
9797
}
@@ -111,7 +111,14 @@ public function shouldReturnAPlaceHolderWhenRawValueIsAnEmptyArray(): void
111111
->expects($this->never())
112112
->method('stringify');
113113

114-
$arrayStringifier = new ArrayStringifier($stringifierMock, 3, 5);
114+
$quoterMock = $this->createMock(Quoter::class);
115+
$quoterMock
116+
->expects($this->once())
117+
->method('quote')
118+
->with($expected, $depth)
119+
->willReturn($expected);
120+
121+
$arrayStringifier = new ArrayStringifier($stringifierMock, $quoterMock, 3, 5);
115122

116123
self::assertSame($expected, $arrayStringifier->stringify($raw, $depth));
117124
}
@@ -131,7 +138,14 @@ public function shouldReturnAPlaceHolderWhenRawValueIsAnEmptyArrayEvenThenReache
131138
->expects($this->never())
132139
->method('stringify');
133140

134-
$arrayStringifier = new ArrayStringifier($stringifierMock, $depth, 5);
141+
$quoterMock = $this->createMock(Quoter::class);
142+
$quoterMock
143+
->expects($this->once())
144+
->method('quote')
145+
->with($expected, $depth)
146+
->willReturn($expected);
147+
148+
$arrayStringifier = new ArrayStringifier($stringifierMock, $quoterMock, $depth, 5);
135149

136150
self::assertSame($expected, $arrayStringifier->stringify($raw, $depth));
137151
}
@@ -154,7 +168,14 @@ public function shouldConvertToStringWhenRawValueIsAnArray(): void
154168
return (string) $raw;
155169
});
156170

157-
$arrayStringifier = new ArrayStringifier($stringifierMock, 3, 5);
171+
$quoterMock = $this->createMock(Quoter::class);
172+
$quoterMock
173+
->expects($this->once())
174+
->method('quote')
175+
->with($expected, $depth)
176+
->willReturn($expected);
177+
178+
$arrayStringifier = new ArrayStringifier($stringifierMock, $quoterMock, 3, 5);
158179

159180
self::assertSame($expected, $arrayStringifier->stringify($raw, $depth));
160181
}
@@ -181,7 +202,14 @@ public function shouldConvertToStringWhenRawValueIsNested(): void
181202
return (string) $raw;
182203
});
183204

184-
$arrayStringifier = new ArrayStringifier($stringifierMock, 3, 5);
205+
$quoterMock = $this->createMock(Quoter::class);
206+
$quoterMock
207+
->expects($this->once())
208+
->method('quote')
209+
->with($expected, $depth)
210+
->willReturn($expected);
211+
212+
$arrayStringifier = new ArrayStringifier($stringifierMock, $quoterMock, 3, 5);
185213

186214
self::assertSame($expected, $arrayStringifier->stringify($raw, $depth));
187215
}
@@ -204,7 +232,14 @@ public function shouldConvertToStringWhenKeysAreNotSequential(): void
204232
return (string) $raw;
205233
});
206234

207-
$arrayStringifier = new ArrayStringifier($stringifierMock, 3, 5);
235+
$quoterMock = $this->createMock(Quoter::class);
236+
$quoterMock
237+
->expects($this->once())
238+
->method('quote')
239+
->with($expected, $depth)
240+
->willReturn($expected);
241+
242+
$arrayStringifier = new ArrayStringifier($stringifierMock, $quoterMock, 3, 5);
208243

209244
self::assertSame($expected, $arrayStringifier->stringify($raw, $depth));
210245
}
@@ -227,7 +262,14 @@ public function shouldConvertToStringWhenKeysAreNotInteger(): void
227262
return (string) $raw;
228263
});
229264

230-
$arrayStringifier = new ArrayStringifier($stringifierMock, 3, 5);
265+
$quoterMock = $this->createMock(Quoter::class);
266+
$quoterMock
267+
->expects($this->once())
268+
->method('quote')
269+
->with($expected, $depth)
270+
->willReturn($expected);
271+
272+
$arrayStringifier = new ArrayStringifier($stringifierMock, $quoterMock, 3, 5);
231273

232274
self::assertSame($expected, $arrayStringifier->stringify($raw, $depth));
233275
}
@@ -252,7 +294,14 @@ public function shouldUseAPlaceholderWhenLimitOfItemsIsReached(): void
252294
return (string) $raw;
253295
});
254296

255-
$arrayStringifier = new ArrayStringifier($stringifierMock, 1, $itemsLimit);
297+
$quoterMock = $this->createMock(Quoter::class);
298+
$quoterMock
299+
->expects($this->once())
300+
->method('quote')
301+
->with($expected, $depth)
302+
->willReturn($expected);
303+
304+
$arrayStringifier = new ArrayStringifier($stringifierMock, $quoterMock, 1, $itemsLimit);
256305

257306
self::assertSame($expected, $arrayStringifier->stringify($raw, $depth));
258307
}

0 commit comments

Comments
 (0)