From 18ca492b83502cd7cc5bb628277f2977549dd7d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Barto=C5=A1?= Date: Tue, 26 Nov 2024 23:35:59 +0100 Subject: [PATCH] MetaResolver: conflicting field name errors use relative property names --- src/Meta/MetaResolver.php | 22 ++++++++++++++++++++-- tests/Unit/Meta/MetaResolverTest.php | 18 +++++------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/Meta/MetaResolver.php b/src/Meta/MetaResolver.php index 4df17232..cac08e97 100644 --- a/src/Meta/MetaResolver.php +++ b/src/Meta/MetaResolver.php @@ -29,6 +29,7 @@ use Orisai\ObjectMapper\Modifiers\RequiresDependenciesModifier; use Orisai\ObjectMapper\Processing\ObjectCreator; use Orisai\ObjectMapper\Rules\RuleManager; +use Orisai\ReflectionMeta\Structure\PropertyStructure; use Orisai\SourceMap\ClassSource; use Orisai\SourceMap\PropertySource; use ReflectionClass; @@ -480,10 +481,12 @@ private function checkFieldNames(ReflectionClass $rootClass, CompileMeta $meta): $collidingPropertyStructure = $map[$fieldName] ?? null; if ($collidingPropertyStructure !== null) { + $propertyName = $this->getRelativePropertyName($propertyStructure, $rootClass); + $collidingPropertyName = $this->getRelativePropertyName($collidingPropertyStructure, $rootClass); + $message = Message::create() ->withContext("Resolving metadata of mapped object '{$rootClass->getName()}'.") - ->withProblem("Properties '{$propertyStructure->getSource()->toString()}'" - . " and '{$collidingPropertyStructure->getSource()->toString()}'" + ->withProblem("Properties '$propertyName' and '$collidingPropertyName'" . " have conflicting field name '$fieldName'.") ->withSolution('Define unique field name for each mapped property.'); @@ -495,4 +498,19 @@ private function checkFieldNames(ReflectionClass $rootClass, CompileMeta $meta): } } + /** + * @param ReflectionClass $rootClass + */ + private function getRelativePropertyName(PropertyStructure $propertyStructure, ReflectionClass $rootClass): string + { + $property = $propertyStructure->getSource()->getReflector(); + $class = $property->getDeclaringClass(); + + if ($class->getName() === $rootClass->getName()) { + return '$' . $property->getName(); + } + + return $class->getName() . '->$' . $property->getName(); + } + } diff --git a/tests/Unit/Meta/MetaResolverTest.php b/tests/Unit/Meta/MetaResolverTest.php index db35be05..618d47b3 100644 --- a/tests/Unit/Meta/MetaResolverTest.php +++ b/tests/Unit/Meta/MetaResolverTest.php @@ -130,11 +130,8 @@ public function testMultipleIdenticalFieldNames(): void <<<'TXT' Context: Resolving metadata of mapped object 'Tests\Orisai\ObjectMapper\Doubles\Invalid\MultipleIdenticalFieldNamesVO'. -Problem: Properties - 'Tests\Orisai\ObjectMapper\Doubles\Invalid\MultipleIdenticalFieldNamesVO->$property2' - and - 'Tests\Orisai\ObjectMapper\Doubles\Invalid\MultipleIdenticalFieldNamesVO->$property1' - have conflicting field name 'field'. +Problem: Properties '$property2' and '$property1' have conflicting field name + 'field'. Solution: Define unique field name for each mapped property. TXT, ); @@ -149,11 +146,8 @@ public function testFieldNameIdenticalWithAnotherPropertyName(): void <<<'TXT' Context: Resolving metadata of mapped object 'Tests\Orisai\ObjectMapper\Doubles\Invalid\FieldNameIdenticalWithAnotherPropertyNameVO'. -Problem: Properties - 'Tests\Orisai\ObjectMapper\Doubles\Invalid\FieldNameIdenticalWithAnotherPropertyNameVO->$property' - and - 'Tests\Orisai\ObjectMapper\Doubles\Invalid\FieldNameIdenticalWithAnotherPropertyNameVO->$field' - have conflicting field name 'field'. +Problem: Properties '$property' and '$field' have conflicting field name + 'field'. Solution: Define unique field name for each mapped property. TXT, ); @@ -171,9 +165,7 @@ public function testMultipleIdenticalPropertyNames(): void <<<'TXT' Context: Resolving metadata of mapped object 'Tests\Orisai\ObjectMapper\Doubles\Invalid\ChildCollidingFieldVO'. -Problem: Properties - 'Tests\Orisai\ObjectMapper\Doubles\Invalid\ChildCollidingFieldVO->$property' - and +Problem: Properties '$property' and 'Tests\Orisai\ObjectMapper\Doubles\FieldNames\ParentFieldVO->$property' have conflicting field name 'property'. Solution: Define unique field name for each mapped property.