From 4ca56804aa99285a25e814e31314d432c5d490aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Sat, 18 May 2024 22:01:13 +0200 Subject: [PATCH 1/2] retry... --- .makefile/e2e.file | 18 +++ _specs/expose-hierarchy.php | 108 +++++++++++++++++ .../composer.json | 10 ++ .../composer.lock | 18 +++ .../expected-output | 1 + .../index.php | 10 ++ .../polyfill/Polyfill.php | 7 ++ .../scoper.inc.php | 13 +++ .../src/Component.php | 5 + .../src/Frame.php | 5 + .../src/ObjectInterface.php | 5 + .../src/Window.php | 5 + .../stubs/StringeableLike.php | 7 ++ specs/misc/expose-hierarchy.php | 110 ++++++++++++++++++ .../NodeVisitor/ClassIdentifierRecorder.php | 27 +++++ src/Symbol/SymbolsRegistry.php | 24 +++- .../Autoload/ScoperAutoloadGeneratorTest.php | 8 +- tests/SpecFramework/SpecScenario.php | 4 +- tests/SpecFrameworkTest/SpecPrinterTest.php | 4 +- tests/Symbol/SymbolsRegistryTest.php | 24 ++-- 20 files changed, 387 insertions(+), 26 deletions(-) create mode 100644 _specs/expose-hierarchy.php create mode 100644 fixtures/set041-exposed-symbols-hierarchy/composer.json create mode 100644 fixtures/set041-exposed-symbols-hierarchy/composer.lock create mode 100644 fixtures/set041-exposed-symbols-hierarchy/expected-output create mode 100644 fixtures/set041-exposed-symbols-hierarchy/index.php create mode 100644 fixtures/set041-exposed-symbols-hierarchy/polyfill/Polyfill.php create mode 100644 fixtures/set041-exposed-symbols-hierarchy/scoper.inc.php create mode 100644 fixtures/set041-exposed-symbols-hierarchy/src/Component.php create mode 100644 fixtures/set041-exposed-symbols-hierarchy/src/Frame.php create mode 100644 fixtures/set041-exposed-symbols-hierarchy/src/ObjectInterface.php create mode 100644 fixtures/set041-exposed-symbols-hierarchy/src/Window.php create mode 100644 fixtures/set041-exposed-symbols-hierarchy/stubs/StringeableLike.php create mode 100644 specs/misc/expose-hierarchy.php diff --git a/.makefile/e2e.file b/.makefile/e2e.file index 8d8fe6eb5..26cf0c512 100644 --- a/.makefile/e2e.file +++ b/.makefile/e2e.file @@ -449,6 +449,24 @@ e2e_040: $(PHP_SCOPER_PHAR_BIN) diff fixtures/set040-polyfills/expected-output build/set040/output +.PHONY: e2e_041 +m: # Runs end-to-end tests for the fixture set e2e_041 — Codebase using a polyfill +e2e_041: $(PHP_SCOPER_PHAR_BIN) + rm -rf fixtures/set041-exposed-symbols-hierarchy/vendor || true + composer --working-dir=fixtures/set041-exposed-symbols-hierarchy dump-autoload + + $(PHP_SCOPER_PHAR) add-prefix . \ + --working-dir=fixtures/set041-exposed-symbols-hierarchy \ + --output-dir=../../build/set041 \ + --force \ + --no-interaction \ + --stop-on-failure + composer --working-dir=build/set041 dump-autoload + + php build/set041/index.php > build/set041/output || true + + diff fixtures/set041-exposed-symbols-hierarchy/expected-output build/set041/output + # # Rules from files diff --git a/_specs/expose-hierarchy.php b/_specs/expose-hierarchy.php new file mode 100644 index 000000000..303c00a8c --- /dev/null +++ b/_specs/expose-hierarchy.php @@ -0,0 +1,108 @@ +, + * Pádraic Brady + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Humbug\PhpScoper\SpecFramework\Config\Meta; +use Humbug\PhpScoper\SpecFramework\Config\SpecWithConfig; + +return [ + 'meta' => new Meta( + title: 'Ensures the exposed symbols follow the required hierarchy.', + exposeClasses: ['/.h*/'], + ), + + 'PHP 8.1 Polyfill (right order)' => SpecWithConfig::create( + spec: <<<'PHP' + SpecWithConfig::create( + spec: <<<'PHP' + SpecWithConfig::create( + spec: <<<'PHP' + [ + __DIR__.'/index.php', + ], + 'expose-classes' => [ + 'Set041\Polyfill\PhpTokenLike', + ], + 'exclude-classes' => [ + 'StringeableLike', + ], +]; diff --git a/fixtures/set041-exposed-symbols-hierarchy/src/Component.php b/fixtures/set041-exposed-symbols-hierarchy/src/Component.php new file mode 100644 index 000000000..3ad805fc1 --- /dev/null +++ b/fixtures/set041-exposed-symbols-hierarchy/src/Component.php @@ -0,0 +1,5 @@ +, + * Pádraic Brady + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Humbug\PhpScoper\SpecFramework\Config\Meta; +use Humbug\PhpScoper\SpecFramework\Config\SpecWithConfig; + +return [ + 'meta' => new Meta( + title: 'Ensures the exposed symbols follow the required hierarchy.', + exposeClasses: ['/.h*/'], + ), + + 'PHP 8.1 Polyfill (right order)' => SpecWithConfig::create( + spec: <<<'PHP' + SpecWithConfig::create( + spec: <<<'PHP' + SpecWithConfig::create( + spec: <<<'PHP' + symbolsRegistry->recordClass( $resolvedName, FullyQualifiedFactory::concat($this->prefix, $resolvedName), + self::getDependencies($parent), ); } return $node; } + /** + * @return FullyQualified[] + */ + private static function getDependencies(Class_|Interface_ $node): array + { + return match(true) { + $node instanceof Class_ => self::getClassDependencies($node), + $node instanceof Interface_ => $node->extends, + }; + } + + private static function getClassDependencies(Class_ $class_): array + { + $dependencies = []; + + if (null !== $class_->extends) { + $dependencies[] = [$class_->extends]; + } + + $dependencies[] = $class_->implements; + + return [...$dependencies]; + } + private function shouldBeAliased(string $resolvedName): bool { if ($this->enrichedReflector->isExposedClass($resolvedName)) { diff --git a/src/Symbol/SymbolsRegistry.php b/src/Symbol/SymbolsRegistry.php index 2960b6b35..74cc4fdd0 100644 --- a/src/Symbol/SymbolsRegistry.php +++ b/src/Symbol/SymbolsRegistry.php @@ -16,6 +16,7 @@ use Countable; use PhpParser\Node\Name\FullyQualified; +use function array_map; use function array_values; use function count; use function serialize; @@ -35,7 +36,7 @@ final class SymbolsRegistry implements Countable /** * @param array $functions - * @param array $classes + * @param array}> $classes */ public static function create( array $functions = [], @@ -50,10 +51,14 @@ public static function create( ); } - foreach ($classes as [$original, $alias]) { + foreach ($classes as [$original, $alias, $dependencies]) { $registry->recordClass( $original instanceof FullyQualified ? $original : new FullyQualified($original), $alias instanceof FullyQualified ? $alias : new FullyQualified($alias), + array_map( + static fn (string|FullyQualified $name) => $name instanceof FullyQualified ? $name : new FullyQualified($name), + $dependencies ?? [], + ), ); } @@ -93,8 +98,8 @@ public function merge(self $symbolsRegistry): void $this->recordedFunctions[$original] = [$original, $alias]; } - foreach ($symbolsRegistry->getRecordedClasses() as [$original, $alias]) { - $this->recordedClasses[$original] = [$original, $alias]; + foreach ($symbolsRegistry->getRecordedClasses() as [$original, $alias, $dependencies]) { + $this->recordedClasses[$original] = [$original, $alias, $dependencies]; } } @@ -111,9 +116,16 @@ public function getRecordedFunctions(): array return array_values($this->recordedFunctions); } - public function recordClass(FullyQualified $original, FullyQualified $alias): void + /** + * @param FullyQualified[] $dependencies + */ + public function recordClass( + FullyQualified $original, + FullyQualified $alias, + array $dependencies = [], + ): void { - $this->recordedClasses[(string) $original] = [(string) $original, (string) $alias]; + $this->recordedClasses[(string) $original] = [(string) $original, (string) $alias, $dependencies]; } /** diff --git a/tests/Autoload/ScoperAutoloadGeneratorTest.php b/tests/Autoload/ScoperAutoloadGeneratorTest.php index 7fa4b91d9..e4a36779e 100644 --- a/tests/Autoload/ScoperAutoloadGeneratorTest.php +++ b/tests/Autoload/ScoperAutoloadGeneratorTest.php @@ -341,7 +341,7 @@ public static function provideRegistry(): iterable yield 'classes recorded' => [ SymbolsRegistry::create( classes: [ - ['A\Foo', 'Humbug\A\Foo'], + ['A\Foo', 'Humbug\A\Foo', []], ], ), [], @@ -387,8 +387,8 @@ function humbug_phpscoper_expose_class($exposed, $prefixed) { yield 'global classes recorded' => [ SymbolsRegistry::create( classes: [ - ['Foo', 'Humbug\Foo'], - ['Bar', 'Humbug\Bar'], + ['Foo', 'Humbug\Foo', []], + ['Bar', 'Humbug\Bar', []], ], ), [], @@ -442,7 +442,7 @@ function humbug_phpscoper_expose_class($exposed, $prefixed) { ['Emca\baz', 'Humbug\Emca\baz'], ], [ - ['A\Foo', 'Humbug\A\Foo'], + ['A\Foo', 'Humbug\A\Foo', []], ], ), [], diff --git a/tests/SpecFramework/SpecScenario.php b/tests/SpecFramework/SpecScenario.php index 706dab142..465f3aa4d 100644 --- a/tests/SpecFramework/SpecScenario.php +++ b/tests/SpecFramework/SpecScenario.php @@ -93,8 +93,8 @@ public function assertExpectedResult( $actualRecordedExposedClasses = $symbolsRegistry->getRecordedClasses(); - self::assertSameRecordedSymbols( - $assert, + // The order matters for classes + $assert->assertSame( $this->expectedRegisteredClasses, $actualRecordedExposedClasses, $specMessage, diff --git a/tests/SpecFrameworkTest/SpecPrinterTest.php b/tests/SpecFrameworkTest/SpecPrinterTest.php index 071b2185d..3e12b20e0 100644 --- a/tests/SpecFrameworkTest/SpecPrinterTest.php +++ b/tests/SpecFrameworkTest/SpecPrinterTest.php @@ -350,8 +350,8 @@ public static function scenarioProvider(): iterable ['another_recorded_function', 'Humbug\another_recorded_function'], ], [ - ['RecordedClass', 'Humbug\RecordedClass'], - ['AnotherRecordedClass', 'Humbug\AnotherRecordedClass'], + ['RecordedClass', 'Humbug\RecordedClass', []], + ['AnotherRecordedClass', 'Humbug\AnotherRecordedClass', []], ], ), $actualCode, diff --git a/tests/Symbol/SymbolsRegistryTest.php b/tests/Symbol/SymbolsRegistryTest.php index c5cbaa4ee..945b94d9d 100644 --- a/tests/Symbol/SymbolsRegistryTest.php +++ b/tests/Symbol/SymbolsRegistryTest.php @@ -28,7 +28,7 @@ final class SymbolsRegistryTest extends TestCase { /** * @param array $functions - * @param array $classes + * @param array $classes * @param list $expectedRecordedFunctions * @param list $expectedRecordedClasses */ @@ -52,7 +52,7 @@ public function test_it_records_functions_and_classes( /** * @param array $functions - * @param array $classes + * @param array $classes */ #[DataProvider('provideRecords')] public function test_it_can_be_serialized_and_unserialized( @@ -179,7 +179,7 @@ public static function provideRegistryToMerge(): iterable [$main, $scopedMain], ], [ - [$testCase, $scopedTestCase], + [$testCase, $scopedTestCase, []], ], ), new SymbolsRegistry(), @@ -199,7 +199,7 @@ public static function provideRegistryToMerge(): iterable [$main, $scopedMain], ], [ - [$testCase, $scopedTestCase], + [$testCase, $scopedTestCase, []], ], ), [ @@ -217,7 +217,7 @@ public static function provideRegistryToMerge(): iterable [$main, $scopedMain], ], [ - [$testCase, $scopedTestCase], + [$testCase, $scopedTestCase, []], ], ), SymbolsRegistry::create( @@ -225,7 +225,7 @@ public static function provideRegistryToMerge(): iterable [$dump, $scopedDump], ], [ - [$finder, $scopedFinder], + [$finder, $scopedFinder, []], ], ), [ @@ -245,7 +245,7 @@ public static function provideRegistryToMerge(): iterable [$main, $scopedMain], ], [ - [$testCase, $scopedTestCase], + [$testCase, $scopedTestCase, []], ], ), SymbolsRegistry::create( @@ -254,8 +254,8 @@ public static function provideRegistryToMerge(): iterable [$dump, $scopedDump], ], [ - [$testCase, $scopedTestCase], - [$finder, $scopedFinder], + [$testCase, $scopedTestCase, []], + [$finder, $scopedFinder, []], ], ), [ @@ -276,8 +276,8 @@ public static function provideRegistryToMerge(): iterable [$dump, $scopedDump], ], [ - [$testCase, $scopedTestCase], - [$finder, $scopedFinder], + [$testCase, $scopedTestCase, []], + [$finder, $scopedFinder, []], ], ), SymbolsRegistry::create( @@ -285,7 +285,7 @@ public static function provideRegistryToMerge(): iterable [$dump, $scopedDump], ], [ - [$finder, $scopedFinder], + [$finder, $scopedFinder, []], ], ), [ From de09cca546a56cb04dbec1d563f525f07144d805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 22 May 2024 22:57:32 +0200 Subject: [PATCH 2/2] fix name --- .../set041-exposed-symbols-hierarchy/stubs/StringeableLike.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fixtures/set041-exposed-symbols-hierarchy/stubs/StringeableLike.php b/fixtures/set041-exposed-symbols-hierarchy/stubs/StringeableLike.php index 998703ce0..2615331bd 100644 --- a/fixtures/set041-exposed-symbols-hierarchy/stubs/StringeableLike.php +++ b/fixtures/set041-exposed-symbols-hierarchy/stubs/StringeableLike.php @@ -1,7 +1,7 @@