Skip to content

Commit 25a2ccc

Browse files
committed
Change how to stringify objects with __debugInfo()
That method intends to enable engineers to have a better overview of the object when they want to debug it. That said, using that method when it's present is better. The representation is similar to the one I used on Stringeable objects, in which we have clear that we're executing a specific method rather than just showing the values. Signed-off-by: Henrique Moody <[email protected]>
1 parent d2fd8c7 commit 25a2ccc

File tree

5 files changed

+139
-1
lines changed

5 files changed

+139
-1
lines changed

src/Stringifiers/ClusterStringifier.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,22 @@ public static function createDefault(): self
4040
$stringifier = new self();
4141

4242
$jsonEncodableStringifier = new JsonEncodableStringifier();
43+
$arrayStringifier = new ArrayStringifier(
44+
$stringifier,
45+
$quoter,
46+
self::MAXIMUM_DEPTH,
47+
self::MAXIMUM_NUMBER_OF_ITEMS
48+
);
4349

4450
$stringifier->setStringifiers([
4551
new IteratorObjectStringifier($stringifier, $quoter),
4652
new DateTimeStringifier($quoter, DateTimeInterface::ATOM),
4753
new ThrowableObjectStringifier($jsonEncodableStringifier, $quoter),
4854
new StringableObjectStringifier($jsonEncodableStringifier, $quoter),
4955
new JsonSerializableObjectStringifier($jsonEncodableStringifier, $quoter),
56+
new ObjectWithDebugInfoStringifier($arrayStringifier, $quoter),
5057
new ObjectStringifier($stringifier, $quoter, self::MAXIMUM_DEPTH, self::MAXIMUM_NUMBER_OF_PROPERTIES),
51-
new ArrayStringifier($stringifier, $quoter, self::MAXIMUM_DEPTH, self::MAXIMUM_NUMBER_OF_ITEMS),
58+
$arrayStringifier,
5259
new InfiniteNumberStringifier($quoter),
5360
new NotANumberStringifier($quoter),
5461
new ResourceStringifier($quoter),
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\Stringifiers;
12+
13+
use Respect\Stringifier\Helpers\ObjectHelper;
14+
use Respect\Stringifier\Quoter;
15+
use Respect\Stringifier\Stringifier;
16+
17+
use function is_object;
18+
use function method_exists;
19+
20+
final class ObjectWithDebugInfoStringifier implements Stringifier
21+
{
22+
use ObjectHelper;
23+
24+
public function __construct(
25+
private readonly Stringifier $stringifier,
26+
private readonly Quoter $quoter
27+
) {
28+
}
29+
30+
public function stringify(mixed $raw, int $depth): ?string
31+
{
32+
if (!(is_object($raw) && method_exists($raw, '__debugInfo'))) {
33+
return null;
34+
}
35+
36+
return $this->quoter->quote(
37+
$this->format($raw, '__debugInfo() =>', $this->stringifier->stringify($raw->__debugInfo(), $depth + 1)),
38+
$depth
39+
);
40+
}
41+
}

tests/fixtures/WithDebugInfo.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
final class WithDebugInfo
12+
{
13+
/**
14+
* @return array<string, string>
15+
*/
16+
public function __debugInfo(): array
17+
{
18+
return ['info' => 'This is the return of __debugInfo()'];
19+
}
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--FILE--
2+
<?php
3+
4+
declare(strict_types=1);
5+
6+
require 'vendor/autoload.php';
7+
8+
output(new WithDebugInfo());
9+
?>
10+
--EXPECT--
11+
`WithDebugInfo { __debugInfo() => ["info": "This is the return of __debugInfo()"] }`
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\Stringifiers;
12+
13+
use PHPUnit\Framework\Attributes\CoversClass;
14+
use PHPUnit\Framework\Attributes\Test;
15+
use PHPUnit\Framework\TestCase;
16+
use Respect\Stringifier\Helpers\ObjectHelper;
17+
use Respect\Stringifier\Stringifiers\ObjectWithDebugInfoStringifier;
18+
use Respect\Stringifier\Test\Double\FakeQuoter;
19+
use Respect\Stringifier\Test\Double\FakeStringifier;
20+
use stdClass;
21+
use WithDebugInfo;
22+
23+
use function sprintf;
24+
25+
#[CoversClass(ObjectHelper::class)]
26+
#[CoversClass(ObjectWithDebugInfoStringifier::class)]
27+
final class ObjectWithDebugInfoStringifierTest extends TestCase
28+
{
29+
private const DEPTH = 0;
30+
31+
#[Test]
32+
public function itShouldNotStringifyRawValueWhenItIsNotAnObjectWithDebugInfo(): void
33+
{
34+
$sut = new ObjectWithDebugInfoStringifier(new FakeStringifier(), new FakeQuoter());
35+
36+
self::assertNull($sut->stringify(new stdClass(), self::DEPTH));
37+
}
38+
39+
#[Test]
40+
public function itShouldStringifyRawValueWhenItIsAnObjectWithDebugInfo(): void
41+
{
42+
$raw = new WithDebugInfo();
43+
44+
$stringifier = new FakeStringifier();
45+
$quoter = new FakeQuoter();
46+
47+
$string = $stringifier->stringify($raw->__debugInfo(), self::DEPTH + 1);
48+
49+
$sut = new ObjectWithDebugInfoStringifier($stringifier, $quoter);
50+
51+
$actual = $sut->stringify($raw, self::DEPTH);
52+
$expected = $quoter->quote(
53+
sprintf('%s { __debugInfo() => %s }', WithDebugInfo::class, $string),
54+
self::DEPTH
55+
);
56+
57+
self::assertSame($expected, $actual);
58+
}
59+
}

0 commit comments

Comments
 (0)