From 228428747a8797d16deb4d5e38a75fbada7903f1 Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Fri, 5 Feb 2021 02:36:20 +0700 Subject: [PATCH] Add support for autoloading Enums PHP 8.1 supports Enums, and [Enums follow class-semantics](https://php.watch/versions/8.1/enums#class-semantics-autoload). Composer's class-map generator currently looks for `class`, `interface`, and `trait` keywords. If Composer is run in PHP 8.1 or later, Composer now additionally looks for `enum` keyword as well. This is similar to how Hack's `enum` support is added. This PR also adds tests for basic enums, backed enums, namespaced enums, and an enum that implements an interface and extends a class. --- src/Composer/Autoload/ClassMapGenerator.php | 2 +- tests/Composer/Test/Autoload/ClassMapGeneratorTest.php | 10 ++++++++++ .../Test/Autoload/Fixtures/php8.1/enum_backed.php | 7 +++++++ .../Test/Autoload/Fixtures/php8.1/enum_basic.php | 7 +++++++ .../Autoload/Fixtures/php8.1/enum_class_semantics.php | 7 +++++++ .../Test/Autoload/Fixtures/php8.1/enum_namespaced.php | 9 +++++++++ 6 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/Composer/Test/Autoload/Fixtures/php8.1/enum_backed.php create mode 100644 tests/Composer/Test/Autoload/Fixtures/php8.1/enum_basic.php create mode 100644 tests/Composer/Test/Autoload/Fixtures/php8.1/enum_class_semantics.php create mode 100644 tests/Composer/Test/Autoload/Fixtures/php8.1/enum_namespaced.php diff --git a/src/Composer/Autoload/ClassMapGenerator.php b/src/Composer/Autoload/ClassMapGenerator.php index 92295f021..ec8c3de5b 100644 --- a/src/Composer/Autoload/ClassMapGenerator.php +++ b/src/Composer/Autoload/ClassMapGenerator.php @@ -215,7 +215,7 @@ class ClassMapGenerator private static function findClasses($path) { $extraTypes = PHP_VERSION_ID < 50400 ? '' : '|trait'; - if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.3', '>=')) { + if (PHP_VERSION_ID >= 80100 || (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.3', '>='))) { $extraTypes .= '|enum'; } diff --git a/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php b/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php index 00ca13712..1fb73c253 100644 --- a/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php +++ b/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php @@ -96,6 +96,16 @@ class ClassMapGeneratorTest extends TestCase 'Dummy\Test\AnonClassHolder' => __DIR__ . '/Fixtures/php7.0/anonclass.php', )); } + + if (PHP_VERSION_ID >= 80100) { + $data[] = array(__DIR__ . '/Fixtures/php8.1', array( + 'RolesBasicEnum' => __DIR__ . '/Fixtures/php8.1/enum_basic.php', + 'RolesBackedEnum' => __DIR__ . '/Fixtures/php8.1/enum_backed.php', + 'RolesClassLikeEnum' => __DIR__ . '/Fixtures/php8.1/enum_class_semantics.php', + 'Foo\Bar\RolesClassLikeNamespacedEnum' => __DIR__ . '/Fixtures/php8.1/enum_namespaced.php', + )); + } + if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.3', '>=')) { $data[] = array(__DIR__ . '/Fixtures/hhvm3.3', array( 'FooEnum' => __DIR__ . '/Fixtures/hhvm3.3/HackEnum.php', diff --git a/tests/Composer/Test/Autoload/Fixtures/php8.1/enum_backed.php b/tests/Composer/Test/Autoload/Fixtures/php8.1/enum_backed.php new file mode 100644 index 000000000..d8963e056 --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/php8.1/enum_backed.php @@ -0,0 +1,7 @@ +