Skip to content

Commit

Permalink
chore: cleanup (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi authored Sep 23, 2024
1 parent 6416d07 commit 38b18e3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* chore: rector run
* chore: refactor `ObjectToObjectMetadataFactory` for clarity
* chore: refactor `PropertyMappingResolver`
* chore: cleanup

## 1.8.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface EagerPropertiesResolverInterface
* object's identifier is eager.
*
* @param class-string $sourceClass
* @return array<int,string>
* @return list<string>
*/
public function getEagerProperties(string $sourceClass): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public function getEagerProperties(string $sourceClass): array
return [];
}

return $metadata->getIdentifierFieldNames();
return array_values($metadata->getIdentifierFieldNames());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,51 +260,72 @@ class: $targetClass,
// create proxy if possible

try {
// ensure we can create the proxy
[$skippedProperties, $constructorIsEager] = $this->getProxyParameters(
targetClass: $targetClass,
eagerProperties: $eagerProperties,
constructorPropertyMappings: $objectToObjectMetadata
->getConstructorPropertyMappings(),
);

$this->proxyFactory
->createProxy($targetClass, function ($instance): void {}, $eagerProperties);
$objectToObjectMetadata = $objectToObjectMetadata->withTargetProxy(
targetProxySkippedProperties: $skippedProperties,
constructorIsEager: $constructorIsEager,
);
} catch (ProxyNotSupportedException $e) {
$objectToObjectMetadata = $objectToObjectMetadata
->withReasonCannotUseProxy($e->getReason());
}

// determine if the constructor contains eager properties. if it
// does, then the constructor is eager
return $objectToObjectMetadata;
}

$constructorIsEager = false;
/**
* @param class-string $targetClass
* @param list<string> $eagerProperties
* @param list<PropertyMapping> $constructorPropertyMappings
* @return array{array<string,true>,bool}
*/
private function getProxyParameters(
string $targetClass,
array $eagerProperties,
array $constructorPropertyMappings,
): array {
// ensure we can create the proxy

foreach ($objectToObjectMetadata->getConstructorPropertyMappings() as $propertyMapping) {
if (!$propertyMapping->isSourceLazy()) {
$constructorIsEager = true;
break;
}
}
$this->proxyFactory
->createProxy($targetClass, function ($instance): void {}, $eagerProperties);

// if the constructor is eager, then every constructor argument is
// eager
// determine if the constructor contains eager properties. if it
// does, then the constructor is eager

if ($constructorIsEager) {
foreach ($objectToObjectMetadata->getConstructorPropertyMappings() as $propertyMapping) {
$eagerProperties[] = $propertyMapping->getTargetProperty();
}
$constructorIsEager = false;

$eagerProperties = array_unique($eagerProperties);
foreach ($constructorPropertyMappings as $propertyMapping) {
if (!$propertyMapping->isSourceLazy()) {
$constructorIsEager = true;
break;
}
}

// skipped properties is the argument used by createLazyGhost()
// if the constructor is eager, then every constructor argument is
// eager

$skippedProperties = ClassUtil::getSkippedProperties(
$targetClass,
$eagerProperties,
);
if ($constructorIsEager) {
foreach ($constructorPropertyMappings as $propertyMapping) {
$eagerProperties[] = $propertyMapping->getTargetProperty();
}

$objectToObjectMetadata = $objectToObjectMetadata->withTargetProxy(
$skippedProperties,
$constructorIsEager,
);
} catch (ProxyNotSupportedException $e) {
$objectToObjectMetadata = $objectToObjectMetadata
->withReasonCannotUseProxy($e->getReason());
$eagerProperties = array_unique($eagerProperties);
}

return $objectToObjectMetadata;
// skipped properties is the argument used by createLazyGhost()

$skippedProperties = ClassUtil::getSkippedProperties(
$targetClass,
$eagerProperties,
);

return [$skippedProperties, $constructorIsEager];
}

/**
Expand Down
26 changes: 13 additions & 13 deletions src/Transformer/ObjectToObjectMetadata/ObjectToObjectMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,37 @@
final readonly class ObjectToObjectMetadata
{
/**
* @var array<int,PropertyMapping>
* @var list<PropertyMapping>
*/
private array $allPropertyMappings;

/**
* @var array<int,PropertyMapping>
* @var list<PropertyMapping>
*/
private array $propertyMappings;

/**
* @var array<int,PropertyMapping>
* @var list<PropertyMapping>
*/
private array $constructorPropertyMappings;

/**
* @var array<int,PropertyMapping>
* @var list<PropertyMapping>
*/
private array $lazyPropertyMappings;

/**
* @var array<int,PropertyMapping>
* @var list<PropertyMapping>
*/
private array $eagerPropertyMappings;

/**
* @param class-string $sourceClass
* @param class-string $targetClass Effective target class after resolving inheritance map
* @param class-string $providedTargetClass
* @param array<int,PropertyMapping> $allPropertyMappings
* @param list<PropertyMapping> $allPropertyMappings
* @param array<string,true> $targetProxySkippedProperties
* @param array<int,string> $sourceProperties List of the source properties. Used by `ObjectToObjectTransformer` to determine if a property is a dynamic property. A property not listed here is considered dynamic.
* @param list<string> $sourceProperties List of the source properties. Used by `ObjectToObjectTransformer` to determine if a property is a dynamic property. A property not listed here is considered dynamic.
*/
public function __construct(
private string $sourceClass,
Expand Down Expand Up @@ -182,39 +182,39 @@ public function isCloneable(): bool
}

/**
* @return array<int,PropertyMapping>
* @return list<PropertyMapping>
*/
public function getPropertyMappings(): array
{
return $this->propertyMappings;
}

/**
* @return array<int,PropertyMapping>
* @return list<PropertyMapping>
*/
public function getLazyPropertyMappings(): array
{
return $this->lazyPropertyMappings;
}

/**
* @return array<int,PropertyMapping>
* @return list<PropertyMapping>
*/
public function getEagerPropertyMappings(): array
{
return $this->eagerPropertyMappings;
}

/**
* @return array<int,PropertyMapping>
* @return list<PropertyMapping>
*/
public function getConstructorPropertyMappings(): array
{
return $this->constructorPropertyMappings;
}

/**
* @return array<int,PropertyMapping>
* @return list<PropertyMapping>
*/
public function getAllPropertyMappings(): array
{
Expand Down Expand Up @@ -288,7 +288,7 @@ public function targetAllowsDynamicProperties(): bool
}

/**
* @return array<int,string>
* @return list<string>
*/
public function getSourceProperties(): array
{
Expand Down

0 comments on commit 38b18e3

Please sign in to comment.