Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Metadata/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Patchlevel\Hydrator\Metadata;

use ReflectionClass;
use ReflectionParameter;

/**
* @phpstan-type serialized array{
Expand All @@ -23,6 +24,9 @@ final class ClassMetadata
/** @var array<string, PropertyMetadata> */
public readonly array $properties;

/** @var array<string, ReflectionParameter>|null */
private array|null $promotedConstructorDefaults = null;

/**
* @param ReflectionClass<T> $reflection
* @param list<PropertyMetadata> $properties
Expand Down Expand Up @@ -62,6 +66,32 @@ public function newInstance(): object
return $this->reflection->newInstanceWithoutConstructor();
}

/** @return array<string, ReflectionParameter> */
public function promotedConstructorDefaults(): array
{
if ($this->promotedConstructorDefaults !== null) {
return $this->promotedConstructorDefaults;
}

$constructor = $this->reflection->getConstructor();

if (!$constructor) {
return $this->promotedConstructorDefaults = [];
}

$result = [];

foreach ($constructor->getParameters() as $parameter) {
if (!$parameter->isPromoted() || !$parameter->isDefaultValueAvailable()) {
continue;
}

$result[$parameter->getName()] = $parameter;
}

return $this->promotedConstructorDefaults = $result;
}

/** @return serialized */
public function __serialize(): array
{
Expand Down
32 changes: 1 addition & 31 deletions src/Middleware/TransformMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Patchlevel\Hydrator\Metadata\ClassMetadata;
use Patchlevel\Hydrator\NormalizationFailure;
use Patchlevel\Hydrator\TypeMismatch;
use ReflectionParameter;
use Throwable;
use TypeError;

Expand Down Expand Up @@ -43,9 +42,7 @@
continue;
}

if ($constructorParameters === null) {
$constructorParameters = $this->promotedConstructorParametersWithDefaultValue($metadata);
}
$constructorParameters ??= $metadata->promotedConstructorDefaults();

Check warning on line 45 in src/Middleware/TransformMiddleware.php

View workflow job for this annotation

GitHub Actions / Mutation tests on diff (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "AssignCoalesce": @@ @@ continue; } - $constructorParameters ??= $metadata->promotedConstructorDefaults(); + $constructorParameters = $metadata->promotedConstructorDefaults(); if (!array_key_exists($propertyMetadata->propertyName, $constructorParameters)) { continue;

if (!array_key_exists($propertyMetadata->propertyName, $constructorParameters)) {
continue;
Expand Down Expand Up @@ -138,31 +135,4 @@

return $data;
}

/** @return array<string, ReflectionParameter> */
private function promotedConstructorParametersWithDefaultValue(ClassMetadata $metadata): array
{
$constructor = $metadata->reflection->getConstructor();

if (!$constructor) {
return [];
}

$parameters = $constructor->getParameters();
$result = [];

foreach ($parameters as $parameter) {
if (!$parameter->isPromoted()) {
continue;
}

if (!$parameter->isDefaultValueAvailable()) {
continue;
}

$result[$parameter->getName()] = $parameter;
}

return $result;
}
}
Loading