Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value generator enum support #143

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
dependencies:
- "locked"
php-version:
- "7.4"
- "8.0"
operating-system:
- "ubuntu-latest"

Expand Down
1 change: 0 additions & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
- "highest"
- "locked"
php-version:
- "7.4"
- "8.0"
- "8.1"
operating-system:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand All @@ -29,7 +29,7 @@
"dealerdirect/phpcodesniffer-composer-installer": true
},
"platform": {
"php": "7.4.99"
"php": "8.0.99"
},
"sort-packages": true
},
Expand Down
6 changes: 3 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Generator/FileGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function setNamespace($namespace)
*
* @param bool $withResolvedAs
* @return array
* @psalm-return array<int, array{string, null|string, false|null|string}>
* @psalm-return array<int, array{string, null|string, null|string}>
*/
public function getUses($withResolvedAs = false)
{
Expand Down
14 changes: 14 additions & 0 deletions src/Generator/ValueGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';
/**#@-*/
Expand Down Expand Up @@ -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,
];
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions test/Generator/TestAsset/TestEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace LaminasTest\Code\Generator\TestAsset;

enum TestEnum
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, enums were introduced in 8.1, hence the parser failure 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, yeah, I'm gonna pull PHP 8.0 support, but gonna work on the other issues in the milestone first.

{
case Test1;
case Test2;
}
29 changes: 29 additions & 0 deletions test/Generator/ValueGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
use Laminas\Code\Generator\PropertyValueGenerator;
use Laminas\Code\Generator\ValueGenerator;
use Laminas\Stdlib\ArrayObject as StdlibArrayObject;
use LaminasTest\Code\Generator\TestAsset\TestEnum;
use PHPUnit\Framework\TestCase;

use function fopen;
use function sprintf;
use function str_replace;

/**
Expand Down Expand Up @@ -388,6 +390,33 @@ public function testPropertyDefaultValueCanHandleBool()
self::assertNotEquals($valueGenerator1->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
Expand Down