Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
8 changes: 2 additions & 6 deletions src/Symfony/Component/ObjectMapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,10 @@
final class ObjectMapper implements ObjectMapperInterface
{
/**
* A SplObjectStorage that tracks recursive references.
* Tracks recursive references.
*/
private ?\SplObjectStorage $objectMap = null;

/**
* @param ContainerInterface $transformCallableLocator
* @param ContainerInterface $conditionCallableLocator
*/
public function __construct(
private readonly ObjectMapperMetadataFactoryInterface $metadataFactory = new ReflectionObjectMapperMetadataFactory(),
private readonly ?PropertyAccessorInterface $propertyAccessor = null,
Expand Down Expand Up @@ -77,7 +73,7 @@ public function map(object $source, object|string|null $target = null): object
$mappedTarget = $this->applyTransforms($map, $mappedTarget, $mappedTarget);

if (!\is_object($mappedTarget)) {
throw new MappingTransformException(sprintf('Cannot map "%s" to a non-object target of type "%s".', get_debug_type($source), get_debug_type($mappedTarget)));
throw new MappingTransformException(\sprintf('Cannot map "%s" to a non-object target of type "%s".', get_debug_type($source), get_debug_type($mappedTarget)));
}
}

Expand Down
19 changes: 9 additions & 10 deletions src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function testHasNothingToMapTo()
{
$this->expectException(MappingException::class);
$this->expectExceptionMessage('Mapping target not found for source "class@anonymous".');
(new ObjectMapper())->map(new class () {});
(new ObjectMapper())->map(new class {});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

}

public function testHasNothingToMapToWithNamedClass()
Expand Down Expand Up @@ -211,7 +211,7 @@ public function get(string $id): mixed
};
}

public function testSourceOnly(): void
public function testSourceOnly()
{
$a = new \stdClass();
$a->name = 'test';
Expand All @@ -220,7 +220,7 @@ public function testSourceOnly(): void
$this->assertInstanceOf(SourceOnly::class, $mapped);
$this->assertSame('test', $mapped->mappedName);

$a = new class () {
$a = new class {
public function __get(string $key): string
{
return match ($key) {
Expand All @@ -235,13 +235,12 @@ public function __get(string $key): string
$this->assertSame('test', $mapped->mappedName);
}


public function testTransformToWrongValueType(): void
public function testTransformToWrongValueType()
{
$this->expectException(MappingTransformException::class);
$this->expectExceptionMessage('Cannot map "stdClass" to a non-object target of type "string".');

$u = new \stdClass;
$u = new \stdClass();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

$u->foo = 'bar';

$metadata = $this->createStub(ObjectMapperMetadataFactoryInterface::class);
Expand All @@ -250,16 +249,16 @@ public function testTransformToWrongValueType(): void
$mapper->map($u);
}

public function testTransformToWrongObject(): void
public function testTransformToWrongObject()
{
$this->expectException(MappingException::class);
$this->expectExceptionMessage(sprintf('Expected the mapped object to be an instance of "%s" but got "stdClass".', ClassWithoutTarget::class));
$this->expectExceptionMessage(\sprintf('Expected the mapped object to be an instance of "%s" but got "stdClass".', ClassWithoutTarget::class));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There should also be a CS fixer for this

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, didn't found it on the go


$u = new \stdClass;
$u = new \stdClass();
$u->foo = 'bar';

$metadata = $this->createStub(ObjectMapperMetadataFactoryInterface::class);
$metadata->method('create')->with($u)->willReturn([new Mapping(target: ClassWithoutTarget::class, transform: fn() => new \stdClass)]);
$metadata->method('create')->with($u)->willReturn([new Mapping(target: ClassWithoutTarget::class, transform: fn () => new \stdClass())]);
$mapper = new ObjectMapper($metadata);
$mapper->map($u);
}
Expand Down
Loading