Skip to content

Commit 2853941

Browse files
authored
feat: Make it easier to serialize/unserialize a SymbolsRegistry (#915)
1 parent 940c3ae commit 2853941

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

phpstan.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ parameters:
5858
path: 'tests/AutoReview/GAE2ECollector.php'
5959
- message: '#normalizeSymbolsRegistryReference#'
6060
path: 'tests/Console/Command/AddInspectCommandIntegrationTest.php'
61+
- message: '#unserialize#'
62+
path: 'src/Symbol/SymbolsRegistry.php'

src/Symbol/SymbolsRegistry.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use PhpParser\Node\Name\FullyQualified;
1919
use function array_values;
2020
use function count;
21+
use function serialize;
22+
use function unserialize;
2123

2224
final class SymbolsRegistry implements Countable
2325
{
@@ -31,6 +33,9 @@ final class SymbolsRegistry implements Countable
3133
*/
3234
private array $recordedClasses = [];
3335

36+
/**
37+
* @param self[] $symbolsRegistries
38+
*/
3439
public static function createFromRegistries(array $symbolsRegistries): self
3540
{
3641
$symbolsRegistry = new self();
@@ -42,6 +47,19 @@ public static function createFromRegistries(array $symbolsRegistries): self
4247
return $symbolsRegistry;
4348
}
4449

50+
public static function unserialize(string $serialized): self
51+
{
52+
return unserialize(
53+
$serialized,
54+
['allowed_classes' => [self::class]],
55+
);
56+
}
57+
58+
public function serialize(): string
59+
{
60+
return serialize($this);
61+
}
62+
4563
public function merge(self $symbolsRegistry): void
4664
{
4765
foreach ($symbolsRegistry->getRecordedFunctions() as [$original, $alias]) {

tests/Symbol/SymbolsRegistryTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@ public function test_it_records_functions_and_classes(
5050
);
5151
}
5252

53+
/**
54+
* @dataProvider provideRecords
55+
*
56+
* @param array<array{FullyQualified, FullyQualified}> $functions
57+
* @param array<array{FullyQualified, FullyQualified}> $classes
58+
*/
59+
public function test_it_can_be_serialized_and_unserialized(
60+
array $functions,
61+
array $classes,
62+
): void {
63+
$registry = self::createRegistry($functions, $classes);
64+
65+
$unserializedRegistry = SymbolsRegistry::unserialize(
66+
$registry->serialize(),
67+
);
68+
69+
self::assertEquals($unserializedRegistry, $registry);
70+
}
71+
5372
public static function provideRecords(): iterable
5473
{
5574
$main = new FullyQualified('PHPUnit\main');

0 commit comments

Comments
 (0)