Skip to content

Commit 083ed66

Browse files
committed
refactor: remove redundant $toClass from MicroMapper, allowing improvement of generic templating
1 parent 81190a2 commit 083ed66

File tree

5 files changed

+22
-23
lines changed

5 files changed

+22
-23
lines changed

src/MapperInterface.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
* Also add #[AsMapper(from: Foo:class, to: Bar:class)] to each mapper class.
1616
*
1717
* @author Ryan Weaver <[email protected]>
18+
*
19+
* @template TFrom of object
20+
* @template TTo of object
1821
*/
1922
interface MapperInterface
2023
{
@@ -24,22 +27,21 @@ interface MapperInterface
2427
* This method should load (e.g. from the database) or instantiate the "to object".
2528
* Avoid populating any properties except for an identifier.
2629
*
27-
* @template TTo of object
28-
*
29-
* @param class-string<TTo> $toClass
30+
* @param TFrom $from
31+
* @param array<mixed> $context
3032
*
3133
* @return TTo
3234
*/
33-
public function load(object $from, string $toClass, array $context): object;
35+
public function load(object $from, array $context): object;
3436

3537
/**
3638
* Populate the data onto the "to object" from the "from object".
3739
*
3840
* Receives the "to object" returned from load().
3941
*
40-
* @template TTo of object
41-
*
42-
* @param TTo $to
42+
* @param TFrom $from
43+
* @param TTo $to
44+
* @param array<mixed> $context
4345
*
4446
* @return TTo
4547
*/

src/MicroMapper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function map(object $from, string $toClass, array $context = []): object
6464
continue;
6565
}
6666

67-
$toObject = $mapperConfig->getMapper()->load($from, $toClass, $context);
67+
$toObject = $mapperConfig->getMapper()->load($from, $context);
6868

6969
// avoid fully populated objects if max depth is reached
7070
if (null === $this->maxDepth || $this->currentDepth < $this->maxDepth) {

tests/PHPStan/data/mapper.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
use Symfonycasts\MicroMapper\MapperInterface;
12+
use Symfonycasts\MicroMapper\Tests\fixtures\Dinosaur;
1313
use Symfonycasts\MicroMapper\Tests\fixtures\DinosaurDto;
1414

15+
use Symfonycasts\MicroMapper\Tests\fixtures\DinosaurToDtoMapper;
1516
use function PHPStan\Testing\assertType;
1617

17-
function doMapperInterfaceLoad(MapperInterface $microMapper): void
18+
function doMapperInterfaceLoad(DinosaurToDtoMapper $dinosaurMapper, Dinosaur $dinosaur): void
1819
{
19-
assertType(DinosaurDto::class, $microMapper->load(new \stdClass(), DinosaurDto::class));
20+
assertType(DinosaurDto::class, $dinosaurMapper->load( $dinosaur, []));
2021
}
2122

22-
function doMapperInterfacePopulate(MapperInterface $microMapper, DinosaurDto $dto): void
23+
function doMapperInterfacePopulate(DinosaurToDtoMapper $dinosaurMapper, Dinosaur $dinosaur, DinosaurDto $dto): void
2324
{
24-
assertType(DinosaurDto::class, $microMapper->populate(new \stdClass(), $dto));
25+
assertType(DinosaurDto::class, $dinosaurMapper->populate($dinosaur, $dto, []));
2526
}

tests/fixtures/DinoRegionToDtoMapper.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,24 @@
1313
use Symfonycasts\MicroMapper\MapperInterface;
1414
use Symfonycasts\MicroMapper\MicroMapperInterface;
1515

16+
/** @implements MapperInterface<DinoRegion, DinoRegionDto> */
1617
#[AsMapper(from: DinoRegion::class, to: DinoRegionDto::class)]
1718
class DinoRegionToDtoMapper implements MapperInterface
1819
{
1920
public function __construct(private MicroMapperInterface $microMapper)
2021
{
2122
}
2223

23-
public function load(object $from, string $toClass, array $context): object
24+
public function load(object $from, array $context): object
2425
{
25-
$dto = new $toClass();
26+
$dto = new DinoRegionDto();
2627
$dto->id = $from->id;
2728

2829
return $dto;
2930
}
3031

3132
public function populate(object $from, object $to, array $context): object
3233
{
33-
\assert($from instanceof DinoRegion);
34-
\assert($to instanceof DinoRegionDto);
35-
3634
$to->name = $from->name;
3735
$to->climate = $from->climate;
3836
$shallowDinosaurDtos = [];

tests/fixtures/DinosaurToDtoMapper.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,24 @@
1313
use Symfonycasts\MicroMapper\MapperInterface;
1414
use Symfonycasts\MicroMapper\MicroMapperInterface;
1515

16+
/** @implements MapperInterface<Dinosaur, DinosaurDto> */
1617
#[AsMapper(from: Dinosaur::class, to: DinosaurDto::class)]
1718
class DinosaurToDtoMapper implements MapperInterface
1819
{
1920
public function __construct(private MicroMapperInterface $microMapper)
2021
{
2122
}
2223

23-
public function load(object $from, string $toClass, array $context): object
24+
public function load(object $from, array $context): object
2425
{
25-
$dto = new $toClass();
26+
$dto = new DinosaurDto();
2627
$dto->id = $from->id;
2728

2829
return $dto;
2930
}
3031

3132
public function populate(object $from, object $to, array $context): object
3233
{
33-
\assert($from instanceof Dinosaur);
34-
\assert($to instanceof DinosaurDto);
35-
3634
$to->genus = $from->genus;
3735
$to->species = $from->species;
3836
$to->region = $this->microMapper->map($from->region, DinoRegionDto::class, [

0 commit comments

Comments
 (0)