Skip to content

Commit 9aa80f9

Browse files
committed
Change how to stringify declared classes and such
Currently, the library treats class names, interface names, trait names, and enumeration names as simple strings, but treating them as particular values is a better approach. Signed-off-by: Henrique Moody <[email protected]>
1 parent 8465af4 commit 9aa80f9

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ echo stringify(new RuntimeException()) . PHP_EOL;
153153

154154
echo stringify(new InvalidArgumentException('This is the exception message')) . PHP_EOL;
155155
// `InvalidArgumentException { "This is the exception message" in file.php:112 }`
156+
157+
echo stringify(Traversable::class) . PHP_EOL;
158+
// `Traversable`
156159
```
157160

158161
To see more examples of how to use the library check the [integration tests](tests/integration).

src/Stringifiers/CompositeStringifier.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static function createDefault(): self
4343
new ResourceStringifier($quoter),
4444
new BoolStringifier($quoter),
4545
new NullStringifier($quoter),
46+
new DeclaredStringifier($quoter),
4647
$jsonEncodableStringifier = new JsonEncodableStringifier(),
4748
);
4849
$stringifier->prependStringifier(
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 Respect\Stringifier\Quoter;
14+
use Respect\Stringifier\Stringifier;
15+
16+
use function class_exists;
17+
use function enum_exists;
18+
use function interface_exists;
19+
use function is_string;
20+
use function trait_exists;
21+
22+
final class DeclaredStringifier implements Stringifier
23+
{
24+
public function __construct(
25+
private readonly Quoter $quoter
26+
) {
27+
}
28+
29+
public function stringify(mixed $raw, int $depth): ?string
30+
{
31+
if (!is_string($raw) || $this->isNotDeclared($raw)) {
32+
return null;
33+
}
34+
35+
return $this->quoter->quote($raw, $depth);
36+
}
37+
38+
public function isNotDeclared(string $raw): bool
39+
{
40+
return !class_exists($raw) && !interface_exists($raw) && !trait_exists($raw) && !enum_exists($raw);
41+
}
42+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--FILE--
2+
<?php
3+
4+
declare(strict_types=1);
5+
6+
require 'vendor/autoload.php';
7+
8+
outputMultiple(
9+
Traversable::class,
10+
ArrayIterator::class,
11+
BasicEnumeration::class,
12+
Respect\Stringifier\Helpers\ObjectHelper::class,
13+
);
14+
?>
15+
--EXPECT--
16+
`Traversable`
17+
`ArrayIterator`
18+
`BasicEnumeration`
19+
`Respect\Stringifier\Helpers\ObjectHelper`
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 ArrayObject;
14+
use BasicEnumeration;
15+
use Countable;
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
use PHPUnit\Framework\Attributes\DataProvider;
18+
use PHPUnit\Framework\Attributes\Test;
19+
use PHPUnit\Framework\TestCase;
20+
use Respect\Stringifier\Helpers\ObjectHelper;
21+
use Respect\Stringifier\Stringifiers\DeclaredStringifier;
22+
use Respect\Stringifier\Test\Double\FakeQuoter;
23+
24+
#[CoversClass(DeclaredStringifier::class)]
25+
final class DeclaredStringifierTest extends TestCase
26+
{
27+
private const DEPTH = 0;
28+
29+
#[Test]
30+
public function itShouldNotStringifyWhenRawValueIsNotExists(): void
31+
{
32+
$sut = new DeclaredStringifier(new FakeQuoter());
33+
34+
self::assertNull($sut->stringify('NotAClassInterfaceTraitOrEnum', self::DEPTH));
35+
}
36+
37+
#[Test]
38+
#[DataProvider('existsRawValuesProvider')]
39+
public function itShouldStringifyWhenRawValueIsExists(string $raw): void
40+
{
41+
$quoter = new FakeQuoter();
42+
43+
$sut = new DeclaredStringifier($quoter);
44+
45+
$actual = $sut->stringify($raw, self::DEPTH);
46+
$expected = $quoter->quote($raw, self::DEPTH);
47+
48+
self::assertEquals($expected, $actual);
49+
}
50+
51+
/**
52+
* @return array<int, array<int, string>>
53+
*/
54+
public static function existsRawValuesProvider(): array
55+
{
56+
return [
57+
[ArrayObject::class],
58+
[Countable::class],
59+
[BasicEnumeration::class],
60+
[ObjectHelper::class],
61+
];
62+
}
63+
}

0 commit comments

Comments
 (0)