Skip to content

Commit

Permalink
feat: Introduce a new factory for SymbolsRegistry (#1004)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Apr 14, 2024
1 parent f69a3f8 commit 73b3b80
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 177 deletions.
27 changes: 27 additions & 0 deletions src/Symbol/SymbolsRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ final class SymbolsRegistry implements Countable
*/
private array $recordedClasses = [];

/**
* @param array<array{string|FullyQualified, string|FullyQualified}> $functions
* @param array<array{string|FullyQualified, string|FullyQualified}> $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
*/
Expand Down
51 changes: 10 additions & 41 deletions tests/Autoload/AutoloadDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -408,31 +404,4 @@ public static function autoloadProvider(): iterable
PHP,
];
}

/**
* @param array<string, string> $functions
* @param array<string, string> $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;
}
}
111 changes: 37 additions & 74 deletions tests/Autoload/ScoperAutoloadGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'],
],
),
[],
Expand Down Expand Up @@ -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'],
],
),
[],
Expand Down Expand Up @@ -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'],
],
),
[],
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -608,31 +598,4 @@ function humbug_phpscoper_expose_class($exposed, $prefixed) {
PHP,
];
}

/**
* @param array<string, string> $functions
* @param array<string, string> $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;
}
}
38 changes: 5 additions & 33 deletions tests/SpecFrameworkTest/SpecPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -426,31 +425,4 @@ private static function createSymbolsConfiguration(array $config): SymbolsConfig

return $factory->createSymbolsConfiguration($config);
}

/**
* @param array<string, string> $functions
* @param array<string, string> $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;
}
}
Loading

0 comments on commit 73b3b80

Please sign in to comment.