diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml index e1292130..abbcb1d1 100644 --- a/.github/workflows/coding-standards.yml +++ b/.github/workflows/coding-standards.yml @@ -15,7 +15,7 @@ jobs: dependencies: - "locked" php-version: - - "7.4" + - "8.0" operating-system: - "ubuntu-latest" diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 62296047..70aab3e9 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -17,7 +17,6 @@ jobs: - "highest" - "locked" php-version: - - "7.4" - "8.0" - "8.1" operating-system: diff --git a/composer.json b/composer.json index 983878ee..c49e7c5b 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "homepage": "https://laminas.dev", "license": "BSD-3-Clause", "require": { - "php": ">=7.4, <8.2" + "php": ">=8.0, <8.2" }, "require-dev": { "ext-phar": "*", @@ -29,7 +29,7 @@ "dealerdirect/phpcodesniffer-composer-installer": true }, "platform": { - "php": "7.4.99" + "php": "8.0.99" }, "sort-packages": true }, diff --git a/composer.lock b/composer.lock index f280a9fc..525b9bf5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d114f0e2528a6bcb8103648fdcc50721", + "content-hash": "19b88939a630e881e0dcbcd01eb49b9e", "packages": [], "packages-dev": [ { @@ -4473,13 +4473,13 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=7.4, <8.2" + "php": ">=8.0, <8.2" }, "platform-dev": { "ext-phar": "*" }, "platform-overrides": { - "php": "7.4.99" + "php": "8.0.99" }, "plugin-api-version": "2.3.0" } diff --git a/src/Generator/FileGenerator.php b/src/Generator/FileGenerator.php index b533923f..a05218fc 100644 --- a/src/Generator/FileGenerator.php +++ b/src/Generator/FileGenerator.php @@ -193,7 +193,7 @@ public function setNamespace($namespace) * * @param bool $withResolvedAs * @return array - * @psalm-return array + * @psalm-return array */ public function getUses($withResolvedAs = false) { diff --git a/src/Generator/ValueGenerator.php b/src/Generator/ValueGenerator.php index 56251d11..2eb74395 100644 --- a/src/Generator/ValueGenerator.php +++ b/src/Generator/ValueGenerator.php @@ -5,6 +5,7 @@ use ArrayObject as SplArrayObject; use Laminas\Code\Exception\InvalidArgumentException; use Laminas\Stdlib\ArrayObject as StdlibArrayObject; +use UnitEnum; use function addcslashes; use function array_keys; @@ -43,6 +44,7 @@ class ValueGenerator extends AbstractGenerator public const TYPE_ARRAY_LONG = 'array_long'; public const TYPE_CONSTANT = 'constant'; public const TYPE_NULL = 'null'; + public const TYPE_ENUM = 'enum'; public const TYPE_OBJECT = 'object'; public const TYPE_OTHER = 'other'; /**#@-*/ @@ -264,6 +266,7 @@ protected function getValidatedType($type) self::TYPE_ARRAY_LONG, self::TYPE_CONSTANT, self::TYPE_NULL, + self::TYPE_ENUM, self::TYPE_OBJECT, self::TYPE_OTHER, ]; @@ -304,6 +307,10 @@ public function getAutoDeterminedType($value) case 'NULL': return self::TYPE_NULL; case 'object': + if ($value instanceof UnitEnum) { + return self::TYPE_ENUM; + } + // enums are typed as objects, so this fall through is intentional case 'resource': case 'unknown type': default: @@ -418,6 +425,13 @@ public function generate() } $output .= $endArray; break; + case self::TYPE_ENUM: + if (! is_object($value)) { + throw new Exception\RuntimeException('Value is not an object.'); + } + + $output = sprintf('%s::%s', get_class($value), (string) $value->name); + break; case self::TYPE_OTHER: default: throw new Exception\RuntimeException(sprintf( diff --git a/test/Generator/TestAsset/TestEnum.php b/test/Generator/TestAsset/TestEnum.php new file mode 100644 index 00000000..c376e87f --- /dev/null +++ b/test/Generator/TestAsset/TestEnum.php @@ -0,0 +1,9 @@ +generate(), $valueGenerator2->generate()); } + /** @requires PHP >= 8.1 */ + public function testPropertyDefaultValueCanHandleEnums(): void + { + $valueGenerator1 = new ValueGenerator( + TestEnum::Test1, + ValueGenerator::TYPE_AUTO, + ValueGenerator::OUTPUT_MULTIPLE_LINE + ); + + $valueGenerator2 = new ValueGenerator( + TestEnum::Test2, + ValueGenerator::TYPE_ENUM, + ValueGenerator::OUTPUT_MULTIPLE_LINE + ); + + $valueGenerator1->initEnvironmentConstants(); + $valueGenerator2->initEnvironmentConstants(); + /** @var TestEnum $value1 */ + $value1 = $valueGenerator1->generate(); + /** @var TestEnum $value2 */ + $value2 = $valueGenerator2->generate(); + + self::assertNotEquals($value1, $value2); + self::assertEquals(sprintf('%s::%s', TestEnum::class, 'Test1'), $value1); + self::assertEquals(sprintf('%s::%s', TestEnum::class, 'Test2'), $value2); + } + /** * @dataProvider simpleArray * @param string $type