From 0252edf98ba24179b56a5a6b24e043f3421e4cb5 Mon Sep 17 00:00:00 2001 From: Rastusik Date: Thu, 4 Aug 2022 17:39:41 +0200 Subject: [PATCH 1/4] ValueGenerator - added support for enumerations Signed-off-by: Rastusik --- src/Generator/ValueGenerator.php | 14 ++++++++++++++ test/Generator/TestAsset/TestEnum.php | 9 +++++++++ test/Generator/ValueGeneratorTest.php | 25 +++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 test/Generator/TestAsset/TestEnum.php 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(); + + self::assertNotEquals($valueGenerator1->generate(), $valueGenerator2->generate()); + self::assertEquals(sprintf('%s::%s', TestEnum::class, 'Test1'), $valueGenerator1->generate()); + self::assertEquals(sprintf('%s::%s', TestEnum::class, 'Test2'), $valueGenerator2->generate()); + } + /** * @dataProvider simpleArray * @param string $type From 88fd0b715cb548c92e27d6e7876e210b594ddf8e Mon Sep 17 00:00:00 2001 From: Rastusik Date: Thu, 4 Aug 2022 22:18:31 +0200 Subject: [PATCH 2/4] composer.json - min PHP version set as 8.0 Signed-off-by: Rastusik --- composer.json | 4 ++-- composer.lock | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) 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" } From f069ead519dbbce912aae345ffa99461d008f728 Mon Sep 17 00:00:00 2001 From: Rastusik Date: Thu, 4 Aug 2022 22:32:22 +0200 Subject: [PATCH 3/4] removed php 7.4 from gh actions Signed-off-by: Rastusik --- .github/workflows/coding-standards.yml | 2 +- .github/workflows/phpunit.yml | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) 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: From 0b3632ee577f6dc031f2b8ac62c1fa60de8fb2a7 Mon Sep 17 00:00:00 2001 From: Rastusik Date: Fri, 5 Aug 2022 11:59:30 +0200 Subject: [PATCH 4/4] fixes according to psalm Signed-off-by: Rastusik --- src/Generator/FileGenerator.php | 2 +- test/Generator/ValueGeneratorTest.php | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) 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/test/Generator/ValueGeneratorTest.php b/test/Generator/ValueGeneratorTest.php index e1c43be9..b501c9c4 100644 --- a/test/Generator/ValueGeneratorTest.php +++ b/test/Generator/ValueGeneratorTest.php @@ -407,10 +407,14 @@ public function testPropertyDefaultValueCanHandleEnums(): void $valueGenerator1->initEnvironmentConstants(); $valueGenerator2->initEnvironmentConstants(); - - self::assertNotEquals($valueGenerator1->generate(), $valueGenerator2->generate()); - self::assertEquals(sprintf('%s::%s', TestEnum::class, 'Test1'), $valueGenerator1->generate()); - self::assertEquals(sprintf('%s::%s', TestEnum::class, 'Test2'), $valueGenerator2->generate()); + /** @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); } /**