Skip to content

Commit be6f2f4

Browse files
committed
Change how to stringify Fiber objects
When stringifying `Fiber` objects, the most important thing is to see which callback it will execute, but currently, the `ObjectStringifier` doesn't show that because it can't find the callable as a property. Signed-off-by: Henrique Moody <[email protected]>
1 parent bc0071e commit be6f2f4

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

src/Stringifiers/ClusterStringifier.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public static function createDefault(): self
4646
self::MAXIMUM_DEPTH,
4747
self::MAXIMUM_NUMBER_OF_ITEMS
4848
);
49+
$callableStringifier = new CallableStringifier($stringifier, $quoter);
4950

5051
$stringifier->setStringifiers([
5152
new IteratorObjectStringifier($stringifier, $quoter),
@@ -55,7 +56,8 @@ public static function createDefault(): self
5556
new JsonSerializableObjectStringifier($jsonEncodableStringifier, $quoter),
5657
new EnumerationStringifier($quoter),
5758
new ObjectWithDebugInfoStringifier($arrayStringifier, $quoter),
58-
new CallableStringifier($stringifier, $quoter),
59+
new FiberObjectStringifier($callableStringifier, $quoter),
60+
$callableStringifier,
5961
new ObjectStringifier($stringifier, $quoter, self::MAXIMUM_DEPTH, self::MAXIMUM_NUMBER_OF_PROPERTIES),
6062
$arrayStringifier,
6163
new InfiniteNumberStringifier($quoter),
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 Fiber;
14+
use ReflectionFiber;
15+
use Respect\Stringifier\Quoter;
16+
use Respect\Stringifier\Stringifier;
17+
18+
use function sprintf;
19+
20+
final class FiberObjectStringifier implements Stringifier
21+
{
22+
public function __construct(
23+
private readonly Stringifier $stringifier,
24+
private readonly Quoter $quoter
25+
) {
26+
}
27+
28+
public function stringify(mixed $raw, int $depth): ?string
29+
{
30+
if (!$raw instanceof Fiber) {
31+
return null;
32+
}
33+
34+
return $this->quoter->quote(
35+
sprintf(
36+
'Fiber { %s }',
37+
$this->stringifier->stringify((new ReflectionFiber($raw))->getCallable(), $depth + 1)
38+
),
39+
$depth
40+
);
41+
}
42+
}
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 Fiber('strlen'));
9+
?>
10+
--EXPECT--
11+
`Fiber { strlen(string $string): int }`
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 Fiber;
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\Attributes\Test;
16+
use PHPUnit\Framework\TestCase;
17+
use Respect\Stringifier\Stringifiers\FiberObjectStringifier;
18+
use Respect\Stringifier\Test\Double\FakeQuoter;
19+
use Respect\Stringifier\Test\Double\FakeStringifier;
20+
use stdClass;
21+
use WithInvoke;
22+
23+
use function sprintf;
24+
25+
#[CoversClass(FiberObjectStringifier::class)]
26+
final class FiberObjectStringifierTest extends TestCase
27+
{
28+
private const DEPTH = 0;
29+
30+
#[Test]
31+
public function itShouldNotStringifyRawValueWhenItIsNotAnObjectWithDebugInfo(): void
32+
{
33+
$sut = new FiberObjectStringifier(new FakeStringifier(), new FakeQuoter());
34+
35+
self::assertNull($sut->stringify(new stdClass(), self::DEPTH));
36+
}
37+
38+
#[Test]
39+
public function itShouldStringifyRawValueWhenItIsAnObjectWithDebugInfo(): void
40+
{
41+
$callable = new WithInvoke();
42+
43+
$raw = new Fiber($callable);
44+
45+
$stringifier = new FakeStringifier();
46+
$quoter = new FakeQuoter();
47+
48+
$string = $stringifier->stringify($callable, self::DEPTH + 1);
49+
50+
$sut = new FiberObjectStringifier($stringifier, $quoter);
51+
52+
$actual = $sut->stringify($raw, self::DEPTH);
53+
$expected = $quoter->quote(
54+
sprintf('Fiber { %s }', $string),
55+
self::DEPTH
56+
);
57+
58+
self::assertSame($expected, $actual);
59+
}
60+
}

0 commit comments

Comments
 (0)