Skip to content

Commit

Permalink
feat: strictTypes configurable from Mapper attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMeshok committed Sep 3, 2024
1 parent bd4bec1 commit 0531fcc
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Attribute/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct(
public ?bool $checkAttributes = null,
public ?ConstructorStrategy $constructorStrategy = null,
public ?bool $allowReadOnlyTargetToPopulate = null,
public ?bool $strictTypes = null,
public int $priority = 0,
public ?string $dateTimeFormat = null,
) {
Expand Down
1 change: 1 addition & 0 deletions src/Event/GenerateMapperEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(
public ?bool $checkAttributes = null,
public ?ConstructorStrategy $constructorStrategy = null,
public ?bool $allowReadOnlyTargetToPopulate = null,
public ?bool $strictTypes = null,
) {
}
}
1 change: 1 addition & 0 deletions src/EventListener/MapperListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function __invoke(GenerateMapperEvent $event): void
$event->checkAttributes ??= $mapper->checkAttributes;
$event->constructorStrategy ??= $mapper->constructorStrategy;
$event->allowReadOnlyTargetToPopulate ??= $mapper->allowReadOnlyTargetToPopulate;
$event->strictTypes ??= $mapper->strictTypes;
$event->mapperMetadata->dateTimeFormat = $mapper->dateTimeFormat;
}
}
4 changes: 1 addition & 3 deletions src/Generator/MapperGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
private MapperConstructorGenerator $mapperConstructorGenerator;
private InjectMapperMethodStatementsGenerator $injectMapperMethodStatementsGenerator;
private MapMethodStatementsGenerator $mapMethodStatementsGenerator;
private bool $declareStrictTypes;
private bool $disableGeneratedMapper;

public function __construct(
Expand All @@ -56,7 +55,6 @@ public function __construct(

$this->injectMapperMethodStatementsGenerator = new InjectMapperMethodStatementsGenerator();

$this->declareStrictTypes = $configuration->strictTypes;
$this->disableGeneratedMapper = !$configuration->autoRegister;
}

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

$statements = [];
if ($this->declareStrictTypes) {
if ($metadata->strictTypes) {
// @phpstan-ignore argument.type
$statements[] = new Stmt\Declare_([create_declare_item('strict_types', create_scalar_int(1))]);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Metadata/GeneratorMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function __construct(
public readonly array $propertiesMetadata,
public readonly bool $checkAttributes = true,
public readonly ConstructorStrategy $constructorStrategy = ConstructorStrategy::AUTO,
public bool $allowReadOnlyTargetToPopulate = false,
public readonly bool $allowReadOnlyTargetToPopulate = false,
public readonly bool $strictTypes = false,
public readonly ?string $provider = null,
) {
$this->variableRegistry = new VariableRegistry();
Expand Down
1 change: 1 addition & 0 deletions src/Metadata/MetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ private function createGeneratorMetadata(MapperMetadata $mapperMetadata): Genera
$mapperEvent->checkAttributes ?? $this->configuration->attributeChecking,
$mapperEvent->constructorStrategy ?? $this->configuration->constructorStrategy,
$mapperEvent->allowReadOnlyTargetToPopulate ?? $this->configuration->allowReadOnlyTargetToPopulate,
$mapperEvent->strictTypes ?? $this->configuration->strictTypes,
$mapperEvent->provider,
);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/AutoMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,15 @@ public function testStrictTypes(): void
$automapper->map($data, Fixtures\IntDTO::class);
}

public function testStrictTypesFromMapper(): void
{
$this->expectException(\TypeError::class);

$automapper = AutoMapper::create(new Configuration(strictTypes: false, classPrefix: 'StrictTypesFromMapper_'));
$data = ['foo' => 1.1];
$automapper->map($data, Fixtures\IntDTOWithMapper::class);
}

public function testWithMixedArray(): void
{
$user = new Fixtures\User(1, 'yolo', '13');
Expand Down
16 changes: 16 additions & 0 deletions tests/Fixtures/IntDTOWithMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures;

use AutoMapper\Attribute\Mapper;

#[Mapper(strictTypes: true)]
readonly class IntDTOWithMapper
{
public function __construct(
public int $foo,
) {
}
}

0 comments on commit 0531fcc

Please sign in to comment.