Skip to content

Commit 3bc006a

Browse files
committed
feat: strictTypes configurable form Mapper attribute
1 parent bd4bec1 commit 3bc006a

File tree

8 files changed

+32
-4
lines changed

8 files changed

+32
-4
lines changed

src/Attribute/Mapper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __construct(
2323
public ?bool $checkAttributes = null,
2424
public ?ConstructorStrategy $constructorStrategy = null,
2525
public ?bool $allowReadOnlyTargetToPopulate = null,
26+
public ?bool $strictTypes = null,
2627
public int $priority = 0,
2728
public ?string $dateTimeFormat = null,
2829
) {

src/Event/GenerateMapperEvent.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function __construct(
2222
public ?bool $checkAttributes = null,
2323
public ?ConstructorStrategy $constructorStrategy = null,
2424
public ?bool $allowReadOnlyTargetToPopulate = null,
25+
public ?bool $strictTypes = null,
2526
) {
2627
}
2728
}

src/EventListener/MapperListener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function __invoke(GenerateMapperEvent $event): void
7272
$event->checkAttributes ??= $mapper->checkAttributes;
7373
$event->constructorStrategy ??= $mapper->constructorStrategy;
7474
$event->allowReadOnlyTargetToPopulate ??= $mapper->allowReadOnlyTargetToPopulate;
75+
$event->strictTypes ??= $mapper->strictTypes;
7576
$event->mapperMetadata->dateTimeFormat = $mapper->dateTimeFormat;
7677
}
7778
}

src/Generator/MapperGenerator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
private MapperConstructorGenerator $mapperConstructorGenerator;
3636
private InjectMapperMethodStatementsGenerator $injectMapperMethodStatementsGenerator;
3737
private MapMethodStatementsGenerator $mapMethodStatementsGenerator;
38-
private bool $declareStrictTypes;
3938
private bool $disableGeneratedMapper;
4039

4140
public function __construct(
@@ -56,7 +55,6 @@ public function __construct(
5655

5756
$this->injectMapperMethodStatementsGenerator = new InjectMapperMethodStatementsGenerator();
5857

59-
$this->declareStrictTypes = $configuration->strictTypes;
6058
$this->disableGeneratedMapper = !$configuration->autoRegister;
6159
}
6260

@@ -75,7 +73,7 @@ public function generate(GeneratorMetadata $metadata): array
7573
}
7674

7775
$statements = [];
78-
if ($this->declareStrictTypes) {
76+
if ($metadata->strictTypes) {
7977
// @phpstan-ignore argument.type
8078
$statements[] = new Stmt\Declare_([create_declare_item('strict_types', create_scalar_int(1))]);
8179
}

src/Metadata/GeneratorMetadata.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public function __construct(
2424
public readonly array $propertiesMetadata,
2525
public readonly bool $checkAttributes = true,
2626
public readonly ConstructorStrategy $constructorStrategy = ConstructorStrategy::AUTO,
27-
public bool $allowReadOnlyTargetToPopulate = false,
27+
public readonly bool $allowReadOnlyTargetToPopulate = false,
28+
public readonly bool $strictTypes = false,
2829
public readonly ?string $provider = null,
2930
) {
3031
$this->variableRegistry = new VariableRegistry();

src/Metadata/MetadataFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ private function createGeneratorMetadata(MapperMetadata $mapperMetadata): Genera
312312
$mapperEvent->checkAttributes ?? $this->configuration->attributeChecking,
313313
$mapperEvent->constructorStrategy ?? $this->configuration->constructorStrategy,
314314
$mapperEvent->allowReadOnlyTargetToPopulate ?? $this->configuration->allowReadOnlyTargetToPopulate,
315+
$mapperEvent->strictTypes ?? $this->configuration->strictTypes,
315316
$mapperEvent->provider,
316317
);
317318
}

tests/AutoMapperTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,15 @@ public function testStrictTypes(): void
794794
$automapper->map($data, Fixtures\IntDTO::class);
795795
}
796796

797+
public function testStrictTypesFromMapper(): void
798+
{
799+
$this->expectException(\TypeError::class);
800+
801+
$automapper = AutoMapper::create(new Configuration(strictTypes: false, classPrefix: 'StrictTypesFromMapper_'));
802+
$data = ['foo' => 1.1];
803+
$automapper->map($data, Fixtures\IntDTOWithMapper::class);
804+
}
805+
797806
public function testWithMixedArray(): void
798807
{
799808
$user = new Fixtures\User(1, 'yolo', '13');
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AutoMapper\Tests\Fixtures;
6+
7+
use AutoMapper\Attribute\Mapper;
8+
9+
#[Mapper(strictTypes: true)]
10+
readonly class IntDTOWithMapper
11+
{
12+
public function __construct(
13+
public int $foo,
14+
) {
15+
}
16+
}

0 commit comments

Comments
 (0)