From 253b5c7145ba5acc2883f4822ef29985ad29a88e Mon Sep 17 00:00:00 2001 From: Sandro Keil Date: Tue, 16 Feb 2021 21:26:10 +0100 Subject: [PATCH] Fix uuid to string methods - #24 --- composer.json | 3 ++- src/ValueObject/UuidFactory.php | 10 +++++++--- tests/ValueObject/UuidFactoryTest.php | 4 ++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 56b445a..38320b7 100644 --- a/composer.json +++ b/composer.json @@ -54,7 +54,8 @@ "scripts": { "check": [ "@cs", - "@test" + "@test", + "@static-analysis" ], "cs": "php-cs-fixer fix -v --diff --dry-run", "cs-fix": "php-cs-fixer fix -v --diff", diff --git a/src/ValueObject/UuidFactory.php b/src/ValueObject/UuidFactory.php index ab3b7bb..a10f182 100644 --- a/src/ValueObject/UuidFactory.php +++ b/src/ValueObject/UuidFactory.php @@ -118,7 +118,11 @@ public function classBuilderFromNative(string $name): ClassBuilder $this->methodToString($name)->generate(), $this->methodEquals($name)->generate(), $this->methodMagicToString($name)->generate(), - )->setTyped($this->typed); + )->setTyped($this->typed) + ->addNamespaceImport( + 'Ramsey\Uuid\Uuid', + 'Ramsey\Uuid\UuidInterface', + ); } public function methodFromString(string $argumentName): MethodGenerator @@ -164,7 +168,7 @@ public function methodToString(string $propertyName): MethodGenerator 'toString', [], MethodGenerator::FLAG_PUBLIC, - new BodyGenerator($this->parser, 'return $this->' . $propertyName . ';') + new BodyGenerator($this->parser, 'return $this->' . $propertyName . '->toString();') ); $method->setTyped($this->typed); $method->setReturnType('string'); @@ -207,7 +211,7 @@ public function methodMagicToString(string $propertyName): MethodGenerator '__toString', [], MethodGenerator::FLAG_PUBLIC, - new BodyGenerator($this->parser, 'return $this->' . $propertyName . ';') + new BodyGenerator($this->parser, 'return $this->' . $propertyName . '->toString();') ); $method->setTyped($this->typed); $method->setReturnType('string'); diff --git a/tests/ValueObject/UuidFactoryTest.php b/tests/ValueObject/UuidFactoryTest.php index 5d9da2e..3a45343 100644 --- a/tests/ValueObject/UuidFactoryTest.php +++ b/tests/ValueObject/UuidFactoryTest.php @@ -101,7 +101,7 @@ private function __construct(UuidInterface $uuid) } public function toString() : string { - return $this->uuid; + return $this->uuid->toString(); } public function equals($other) : bool { @@ -112,7 +112,7 @@ public function equals($other) : bool } public function __toString() : string { - return $this->uuid; + return $this->uuid->toString(); } } EOF;