Skip to content

Commit a109163

Browse files
authored
Optimize condition order to avoid unnecessary method calls in AutoMapper (#286)
This PR reorders the generated condition checks in the generate() method to prioritize the customCondition() check before other standard conditions. In the generated mapper code, this change allows custom condition to occur earlier. As a result, potentially heavy methods, such as those that may trigger database queries are not executed if the custom condition fails. Before: ```php if (isAllowedAttribute(...) && groupsCheck(...) && customCondition(...)) { // DB hit $result->someProperty = $value->someMethod(); // DB hit } ``` After: ```php if (customCondition(...) && isAllowedAttribute(...) && groupsCheck(...)) { $result->someProperty = $value->someMethod(); // DB hit only if needed } ```
2 parents 9acfbea + cd11637 commit a109163

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
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

8+
## [9.4.2] - 2025-07-17
9+
### Changed
10+
- [GH#286](https://github.com/jolicode/automapper/pull/286) Optimize condition order to avoid unnecessary method calls by prioritizing custom conditions
11+
812
## [Unreleased]
913
### Fixed
1014
- [GH#272](https://github.com/jolicode/automapper/pull/272) Fixed circular references with promoted properties

src/Generator/PropertyConditionsGenerator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function generate(GeneratorMetadata $metadata, PropertyMetadata $property
4040
{
4141
$conditions = [];
4242

43+
$conditions[] = $this->customCondition($metadata, $propertyMetadata);
4344
$conditions[] = $this->propertyExistsForStdClass($metadata, $propertyMetadata);
4445
$conditions[] = $this->propertyExistsForArray($metadata, $propertyMetadata);
4546

@@ -60,8 +61,6 @@ public function generate(GeneratorMetadata $metadata, PropertyMetadata $property
6061
$conditions[] = $this->maxDepthCheck($metadata, $propertyMetadata);
6162
}
6263

63-
$conditions[] = $this->customCondition($metadata, $propertyMetadata);
64-
6564
$conditions = array_values(array_filter($conditions));
6665

6766
if (!$conditions) {

0 commit comments

Comments
 (0)