From 73b3b8073a9db434c43e1d2613288348251434ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Sun, 14 Apr 2024 20:05:55 +0200 Subject: [PATCH] feat: Introduce a new factory for SymbolsRegistry (#1004) --- src/Symbol/SymbolsRegistry.php | 27 +++++ tests/Autoload/AutoloadDumperTest.php | 51 ++------ .../Autoload/ScoperAutoloadGeneratorTest.php | 111 ++++++------------ tests/SpecFrameworkTest/SpecPrinterTest.php | 38 +----- tests/Symbol/SymbolsRegistryTest.php | 43 +++---- 5 files changed, 93 insertions(+), 177 deletions(-) diff --git a/src/Symbol/SymbolsRegistry.php b/src/Symbol/SymbolsRegistry.php index 07f587f5..2960b6b3 100644 --- a/src/Symbol/SymbolsRegistry.php +++ b/src/Symbol/SymbolsRegistry.php @@ -33,6 +33,33 @@ final class SymbolsRegistry implements Countable */ private array $recordedClasses = []; + /** + * @param array $functions + * @param array $classes + */ + public static function create( + array $functions = [], + array $classes = [], + ): self { + $registry = new self(); + + foreach ($functions as [$original, $alias]) { + $registry->recordFunction( + $original instanceof FullyQualified ? $original : new FullyQualified($original), + $alias instanceof FullyQualified ? $alias : new FullyQualified($alias), + ); + } + + foreach ($classes as [$original, $alias]) { + $registry->recordClass( + $original instanceof FullyQualified ? $original : new FullyQualified($original), + $alias instanceof FullyQualified ? $alias : new FullyQualified($alias), + ); + } + + return $registry; + } + /** * @param self[] $symbolsRegistries */ diff --git a/tests/Autoload/AutoloadDumperTest.php b/tests/Autoload/AutoloadDumperTest.php index 15e656f8..04277845 100644 --- a/tests/Autoload/AutoloadDumperTest.php +++ b/tests/Autoload/AutoloadDumperTest.php @@ -16,7 +16,6 @@ use Humbug\PhpScoper\Symbol\SymbolsRegistry; use KevinGH\Box\Composer\AutoloadDumper; -use PhpParser\Node\Name\FullyQualified; use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -107,12 +106,11 @@ public static function autoloadProvider(): iterable ]; yield 'global symbols' => [ - self::createRegistry( + SymbolsRegistry::create( [ - 'bar' => 'Humbug\bar', - 'foo' => 'Humbug\foo', + ['bar', 'Humbug\bar'], + ['foo', 'Humbug\foo'], ], - [], ), [], <<<'PHP' @@ -204,12 +202,11 @@ public static function autoloadProvider(): iterable ]; yield 'global symbols with file hashes' => [ - self::createRegistry( + SymbolsRegistry::create( [ - 'bar' => 'Humbug\bar', - 'foo' => 'Humbug\foo', + ['bar', 'Humbug\bar'], + ['foo', 'Humbug\foo'], ], - [], ), ['a610a8e036135f992c6edfb10ca9f4e9', 'e252736c6babb7c097ab6692dbcb2a5a'], <<<'PHP' @@ -301,13 +298,12 @@ public static function autoloadProvider(): iterable ]; yield 'namespaced symbols' => [ - self::createRegistry( + SymbolsRegistry::create( [ - 'Acme\bar' => 'Humbug\Acme\bar', - 'Acme\foo' => 'Humbug\Acme\foo', - 'Emca\baz' => 'Humbug\Emca\baz', + ['Acme\bar', 'Humbug\Acme\bar'], + ['Acme\foo', 'Humbug\Acme\foo'], + ['Emca\baz', 'Humbug\Emca\baz'], ], - [], ), [], <<<'PHP' @@ -408,31 +404,4 @@ public static function autoloadProvider(): iterable PHP, ]; } - - /** - * @param array $functions - * @param array $classes - */ - private static function createRegistry( - array $functions, - array $classes - ): SymbolsRegistry { - $registry = new SymbolsRegistry(); - - foreach ($functions as $original => $alias) { - $registry->recordFunction( - new FullyQualified($original), - new FullyQualified($alias), - ); - } - - foreach ($classes as $original => $alias) { - $registry->recordClass( - new FullyQualified($original), - new FullyQualified($alias), - ); - } - - return $registry; - } } diff --git a/tests/Autoload/ScoperAutoloadGeneratorTest.php b/tests/Autoload/ScoperAutoloadGeneratorTest.php index e4eb5412..a3cc75a2 100644 --- a/tests/Autoload/ScoperAutoloadGeneratorTest.php +++ b/tests/Autoload/ScoperAutoloadGeneratorTest.php @@ -15,7 +15,6 @@ namespace Humbug\PhpScoper\Autoload; use Humbug\PhpScoper\Symbol\SymbolsRegistry; -use PhpParser\Node\Name\FullyQualified; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -102,12 +101,11 @@ public static function provideRegistry(): iterable ]; yield 'global functions recorded' => [ - self::createRegistry( + SymbolsRegistry::create( [ - 'bar' => 'Humbug\bar', - 'foo' => 'Humbug\foo', + ['bar', 'Humbug\bar'], + ['foo', 'Humbug\foo'], ], - [], ), [], <<<'PHP' @@ -144,12 +142,11 @@ public static function provideRegistry(): iterable ]; yield 'global functions recorded unordered' => [ - self::createRegistry( + SymbolsRegistry::create( [ - 'foo' => 'Humbug\foo', - 'bar' => 'Humbug\bar', + ['foo', 'Humbug\foo'], + ['bar', 'Humbug\bar'], ], - [], ), [], <<<'PHP' @@ -186,13 +183,12 @@ public static function provideRegistry(): iterable ]; yield 'namespaced functions recorded' => [ - self::createRegistry( + SymbolsRegistry::create( [ - 'Acme\bar' => 'Humbug\Acme\bar', - 'Acme\foo' => 'Humbug\Acme\foo', - 'Emca\baz' => 'Humbug\Emca\baz', + ['Acme\bar', 'Humbug\Acme\bar'], + ['Acme\foo', 'Humbug\Acme\foo'], + ['Emca\baz', 'Humbug\Emca\baz'], ], - [], ), [], <<<'PHP' @@ -239,13 +235,12 @@ public static function provideRegistry(): iterable ]; yield 'namespaced functions recorded with hashes' => [ - self::createRegistry( + SymbolsRegistry::create( [ - 'Acme\bar' => 'Humbug\Acme\bar', - 'Acme\foo' => 'Humbug\Acme\foo', - 'Emca\baz' => 'Humbug\Emca\baz', + ['Acme\bar', 'Humbug\Acme\bar'], + ['Acme\foo', 'Humbug\Acme\foo'], + ['Emca\baz', 'Humbug\Emca\baz'], ], - [], ), ['a610a8e036135f992c6edfb10ca9f4e9', 'e252736c6babb7c097ab6692dbcb2a5a'], <<<'PHP' @@ -292,13 +287,12 @@ public static function provideRegistry(): iterable ]; yield 'namespaced functions recorded unordered' => [ - self::createRegistry( + SymbolsRegistry::create( [ - 'Acme\foo' => 'Humbug\Acme\foo', - 'Emca\baz' => 'Humbug\Emca\baz', - 'Acme\bar' => 'Humbug\Acme\bar', + ['Acme\foo', 'Humbug\Acme\foo'], + ['Emca\baz', 'Humbug\Emca\baz'], + ['Acme\bar', 'Humbug\Acme\bar'], ], - [], ), [], <<<'PHP' @@ -345,10 +339,9 @@ public static function provideRegistry(): iterable ]; yield 'classes recorded' => [ - self::createRegistry( - [], - [ - 'A\Foo' => 'Humbug\A\Foo', + SymbolsRegistry::create( + classes: [ + ['A\Foo', 'Humbug\A\Foo'], ], ), [], @@ -392,11 +385,10 @@ function humbug_phpscoper_expose_class($exposed, $prefixed) { ]; yield 'global classes recorded' => [ - self::createRegistry( - [], - [ - 'Foo' => 'Humbug\Foo', - 'Bar' => 'Humbug\Bar', + SymbolsRegistry::create( + classes: [ + ['Foo', 'Humbug\Foo'], + ['Bar', 'Humbug\Bar'], ], ), [], @@ -441,16 +433,16 @@ function humbug_phpscoper_expose_class($exposed, $prefixed) { ]; yield 'nominal' => [ - self::createRegistry( + SymbolsRegistry::create( [ - 'bar' => 'Humbug\bar', - 'foo' => 'Humbug\foo', - 'Acme\foo' => 'Humbug\Acme\foo', - 'Acme\bar' => 'Humbug\Acme\bar', - 'Emca\baz' => 'Humbug\Emca\baz', + ['bar', 'Humbug\bar'], + ['foo', 'Humbug\foo'], + ['Acme\foo', 'Humbug\Acme\foo'], + ['Acme\bar', 'Humbug\Acme\bar'], + ['Emca\baz', 'Humbug\Emca\baz'], ], [ - 'A\Foo' => 'Humbug\A\Foo', + ['A\Foo', 'Humbug\A\Foo'], ], ), [], @@ -517,11 +509,10 @@ function humbug_phpscoper_expose_class($exposed, $prefixed) { // https://github.com/humbug/php-scoper/issues/267 yield '__autoload global function with no namespaced functions' => [ - self::createRegistry( + SymbolsRegistry::create( [ - '__autoload' => 'Humbug\__autoload', + ['__autoload', 'Humbug\__autoload'], ], - [], ), [], <<<'PHP' @@ -558,12 +549,11 @@ function humbug_phpscoper_expose_class($exposed, $prefixed) { // https://github.com/humbug/php-scoper/issues/267 yield '__autoload global function with namespaced functions' => [ - self::createRegistry( + SymbolsRegistry::create( [ - '__autoload' => 'Humbug\__autoload', - 'Acme\foo' => 'Humbug\Acme\foo', + ['__autoload', 'Humbug\__autoload'], + ['Acme\foo', 'Humbug\Acme\foo'], ], - [], ), [], <<<'PHP' @@ -608,31 +598,4 @@ function humbug_phpscoper_expose_class($exposed, $prefixed) { PHP, ]; } - - /** - * @param array $functions - * @param array $classes - */ - private static function createRegistry( - array $functions, - array $classes - ): SymbolsRegistry { - $registry = new SymbolsRegistry(); - - foreach ($functions as $original => $alias) { - $registry->recordFunction( - new FullyQualified($original), - new FullyQualified($alias), - ); - } - - foreach ($classes as $original => $alias) { - $registry->recordClass( - new FullyQualified($original), - new FullyQualified($alias), - ); - } - - return $registry; - } } diff --git a/tests/SpecFrameworkTest/SpecPrinterTest.php b/tests/SpecFrameworkTest/SpecPrinterTest.php index acb1a46c..071b2185 100644 --- a/tests/SpecFrameworkTest/SpecPrinterTest.php +++ b/tests/SpecFrameworkTest/SpecPrinterTest.php @@ -20,7 +20,6 @@ use Humbug\PhpScoper\SpecFramework\SpecPrinter; use Humbug\PhpScoper\SpecFramework\SpecScenario; use Humbug\PhpScoper\Symbol\SymbolsRegistry; -use PhpParser\Node\Name\FullyQualified; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -345,14 +344,14 @@ public static function scenarioProvider(): iterable [], [], ), - self::createRegistry( + SymbolsRegistry::create( [ - 'recorded_function' => 'Humbug\recorded_function', - 'another_recorded_function' => 'Humbug\another_recorded_function', + ['recorded_function', 'Humbug\recorded_function'], + ['another_recorded_function', 'Humbug\another_recorded_function'], ], [ - 'RecordedClass' => 'Humbug\RecordedClass', - 'AnotherRecordedClass' => 'Humbug\AnotherRecordedClass', + ['RecordedClass', 'Humbug\RecordedClass'], + ['AnotherRecordedClass', 'Humbug\AnotherRecordedClass'], ], ), $actualCode, @@ -426,31 +425,4 @@ private static function createSymbolsConfiguration(array $config): SymbolsConfig return $factory->createSymbolsConfiguration($config); } - - /** - * @param array $functions - * @param array $classes - */ - private static function createRegistry( - array $functions, - array $classes, - ): SymbolsRegistry { - $registry = new SymbolsRegistry(); - - foreach ($functions as $original => $alias) { - $registry->recordFunction( - new FullyQualified($original), - new FullyQualified($alias), - ); - } - - foreach ($classes as $original => $alias) { - $registry->recordClass( - new FullyQualified($original), - new FullyQualified($alias), - ); - } - - return $registry; - } } diff --git a/tests/Symbol/SymbolsRegistryTest.php b/tests/Symbol/SymbolsRegistryTest.php index 01ced874..c5cbaa4e 100644 --- a/tests/Symbol/SymbolsRegistryTest.php +++ b/tests/Symbol/SymbolsRegistryTest.php @@ -40,7 +40,7 @@ public function test_it_records_functions_and_classes( array $expectedRecordedClasses, int $expectedCount ): void { - $registry = self::createRegistry($functions, $classes); + $registry = SymbolsRegistry::create($functions, $classes); self::assertStateIs( $registry, @@ -59,7 +59,7 @@ public function test_it_can_be_serialized_and_unserialized( array $functions, array $classes, ): void { - $registry = self::createRegistry($functions, $classes); + $registry = SymbolsRegistry::create($functions, $classes); $unserializedRegistry = SymbolsRegistry::unserialize( $registry->serialize(), @@ -174,7 +174,7 @@ public static function provideRegistryToMerge(): iterable ]; yield 'elements in the source' => [ - self::createRegistry( + SymbolsRegistry::create( [ [$main, $scopedMain], ], @@ -194,7 +194,7 @@ public static function provideRegistryToMerge(): iterable yield 'elements in the target' => [ new SymbolsRegistry(), - self::createRegistry( + SymbolsRegistry::create( [ [$main, $scopedMain], ], @@ -212,7 +212,7 @@ public static function provideRegistryToMerge(): iterable ]; yield 'elements on both sides' => [ - self::createRegistry( + SymbolsRegistry::create( [ [$main, $scopedMain], ], @@ -220,7 +220,7 @@ public static function provideRegistryToMerge(): iterable [$testCase, $scopedTestCase], ], ), - self::createRegistry( + SymbolsRegistry::create( [ [$dump, $scopedDump], ], @@ -240,7 +240,7 @@ public static function provideRegistryToMerge(): iterable ]; yield 'elements on both sides with duplicates entries from the target' => [ - self::createRegistry( + SymbolsRegistry::create( [ [$main, $scopedMain], ], @@ -248,7 +248,7 @@ public static function provideRegistryToMerge(): iterable [$testCase, $scopedTestCase], ], ), - self::createRegistry( + SymbolsRegistry::create( [ [$main, $scopedMain], [$dump, $scopedDump], @@ -270,7 +270,7 @@ public static function provideRegistryToMerge(): iterable ]; yield 'elements on both sides with duplicates entries from the source' => [ - self::createRegistry( + SymbolsRegistry::create( [ [$main, $scopedMain], [$dump, $scopedDump], @@ -280,7 +280,7 @@ public static function provideRegistryToMerge(): iterable [$finder, $scopedFinder], ], ), - self::createRegistry( + SymbolsRegistry::create( [ [$dump, $scopedDump], ], @@ -349,15 +349,15 @@ public static function provideRegistriesToMerge(): iterable yield 'nominal' => [ [ - self::createRegistry( + SymbolsRegistry::create( [[$main, $scopedMain]], [[$testCase, $scopedTestCase]], ), - self::createRegistry( + SymbolsRegistry::create( [[$dump, $scopedDump]], [[$finder, $scopedFinder]], ), - self::createRegistry( + SymbolsRegistry::create( [[$dd, $scopedDd]], [[$fileSystem, $scopedFileSystem]], ), @@ -378,7 +378,7 @@ public static function provideRegistriesToMerge(): iterable public function test_it_exposes_recorded_classes(): void { - $registry = self::createRegistry( + $registry = SymbolsRegistry::create( [[new FullyQualified('foo'), new FullyQualified('Humbug\foo')]], [[new FullyQualified('Bar'), new FullyQualified('Humbug\Bar')]], ); @@ -408,19 +408,4 @@ private static function assertStateIs( ); self::assertCount($expectedCount, $symbolsRegistry); } - - private static function createRegistry(array $functions, array $classes): SymbolsRegistry - { - $registry = new SymbolsRegistry(); - - foreach ($functions as [$original, $alias]) { - $registry->recordFunction($original, $alias); - } - - foreach ($classes as [$original, $alias]) { - $registry->recordClass($original, $alias); - } - - return $registry; - } }