diff --git a/composer.json b/composer.json index 40152dc..7f7f03d 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,11 @@ "Murtukov\\PHPCodeGenerator\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, "require-dev": { "phpstan/phpstan": "^1.12.4", "phpunit/phpunit": "^10", diff --git a/src/Utils.php b/src/Utils.php index 39f3e2f..c3a477c 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -96,6 +96,10 @@ private static function stringifyValue(mixed $value, bool $topLevel = false): st case 'object': if (!$value instanceof GeneratorInterface) { + if (enum_exists($value::class)) { + return '\\'.$value::class.'::'.$value->name; + } + try { $result = json_encode($value->__toString()); diff --git a/tests/Fixtures/BasicEnum.php b/tests/Fixtures/BasicEnum.php new file mode 100644 index 0000000..1d11d0b --- /dev/null +++ b/tests/Fixtures/BasicEnum.php @@ -0,0 +1,9 @@ +assertEquals('\\Tests\\Fixtures\\BasicEnum::ONE', Utils::stringify(BasicEnum::ONE)); + } + + /** + * @test + */ + public function stringifyBackedEnum(): void + { + $this->assertEquals('\\Tests\\Fixtures\\StringBackedEnum::ONE', Utils::stringify(StringBackedEnum::ONE)); + } + /** * @test */