Skip to content

Commit 91369ab

Browse files
committed
feat: configuration to generate code with strict types
1 parent 2eb0671 commit 91369ab

File tree

14 files changed

+96
-8
lines changed

14 files changed

+96
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Added
10+
- [GH#180](https://github.com/jolicode/automapper/pull/180) Add configuration to generate code with strict types
11+
912
### Fixed
1013
- [GH#167](https://github.com/jolicode/automapper/pull/167) Fix property metadata attribute name in docs
1114
- [GH#166](https://github.com/jolicode/automapper/pull/166) Remove cache for property info, use specific services instead

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/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public function __construct(
3636
* Does the mapper should throw an exception if the target is read-only.
3737
*/
3838
public bool $allowReadOnlyTargetToPopulate = false,
39+
/**
40+
* Add declare(strict_types=1) to generated code.
41+
*/
42+
public bool $strictTypes = false,
3943
) {
4044
}
4145
}

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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
use PhpParser\Node\Stmt;
2121
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
2222

23+
use function AutoMapper\PhpParser\create_declare_item;
24+
use function AutoMapper\PhpParser\create_scalar_int;
25+
2326
/**
2427
* Generates code for a mapping class.
2528
*
@@ -58,22 +61,31 @@ public function __construct(
5861
/**
5962
* Generate Class AST given metadata for a mapper.
6063
*
64+
* @return Stmt[]
65+
*
6166
* @throws CompileException
6267
* @throws InvalidMappingException
6368
*/
64-
public function generate(GeneratorMetadata $metadata): Stmt\Class_
69+
public function generate(GeneratorMetadata $metadata): array
6570
{
6671
if ($this->disableGeneratedMapper) {
6772
throw new InvalidMappingException('No mapper found for source ' . $metadata->mapperMetadata->source . ' and target ' . $metadata->mapperMetadata->target);
6873
}
6974

70-
return (new Builder\Class_($metadata->mapperMetadata->className))
75+
$statements = [];
76+
if ($metadata->strictTypes) {
77+
// @phpstan-ignore argument.type
78+
$statements[] = new Stmt\Declare_([create_declare_item('strict_types', create_scalar_int(1))]);
79+
}
80+
$statements[] = (new Builder\Class_($metadata->mapperMetadata->className))
7181
->makeFinal()
7282
->extend(GeneratedMapper::class)
7383
->addStmt($this->constructorMethod($metadata))
7484
->addStmt($this->mapMethod($metadata))
7585
->addStmt($this->registerMappersMethod($metadata))
7686
->getNode();
87+
88+
return $statements;
7789
}
7890

7991
/**

src/Loader/EvalLoader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public function __construct(
3131

3232
public function loadClass(MapperMetadata $mapperMetadata): void
3333
{
34-
$class = $this->generator->generate($this->metadataFactory->getGeneratorMetadata($mapperMetadata->source, $mapperMetadata->target));
35-
36-
eval($this->printer->prettyPrint([$class]));
34+
eval($this->printer->prettyPrint($this->generator->generate(
35+
$this->metadataFactory->getGeneratorMetadata($mapperMetadata->source, $mapperMetadata->target)
36+
)));
3737
}
3838

3939
public function buildMappers(MetadataRegistry $registry): bool

src/Loader/FileLoader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ public function createGeneratedMapper(MapperMetadata $mapperMetadata): string
7878
$className = $mapperMetadata->className;
7979
$classPath = $this->directory . \DIRECTORY_SEPARATOR . $className . '.php';
8080

81-
$generatorMetadata = $this->metadataFactory->getGeneratorMetadata($mapperMetadata->source, $mapperMetadata->target);
82-
$classCode = $this->printer->prettyPrint([$this->generator->generate($generatorMetadata)]);
81+
$classCode = $this->printer->prettyPrint($this->generator->generate(
82+
$this->metadataFactory->getGeneratorMetadata($mapperMetadata->source, $mapperMetadata->target)
83+
));
8384

8485
$this->write($classPath, "<?php\n\n" . $classCode . "\n");
8586

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
}

0 commit comments

Comments
 (0)