Skip to content

Commit

Permalink
Skip non mapped field with default values when generating proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Mar 3, 2022
1 parent 3f61700 commit 47540d6
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Proxy/Factory/StaticProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
use Doctrine\Persistence\NotifyPropertyChanged;
use ProxyManager\Factory\LazyLoadingGhostFactory;
use ProxyManager\Proxy\GhostObjectInterface;
use ReflectionClass;
use ReflectionProperty;

use function array_filter;
use function array_key_exists;
use function array_merge;
use function count;

/**
Expand Down Expand Up @@ -139,6 +142,19 @@ private function createInitializer(
* @return array<int, string>
*/
private function skippedFieldsFqns(ClassMetadata $metadata): array
{
$skippedIdFieldsFqns = $this->getIdFieldsFqns($metadata);
$skippedNonMappedFieldsFqns = $this->getNonMappedPropertiesWithDefaultValueFqns($metadata);

return array_merge($skippedIdFieldsFqns, $skippedNonMappedFieldsFqns);
}

/**
* @param ClassMetadata<object> $metadata
*
* @return array<int, string>
*/
private function getIdFieldsFqns(ClassMetadata $metadata): array
{
$idFieldFqcns = [];

Expand All @@ -149,6 +165,35 @@ private function skippedFieldsFqns(ClassMetadata $metadata): array
return $idFieldFqcns;
}

/**
* @param ClassMetadata<object> $metadata
*
* @return array<int, string>
*/
private function getNonMappedPropertiesWithDefaultValueFqns(ClassMetadata $metadata): array
{
$defaultProperties = $metadata->getReflectionClass()->getDefaultProperties();
$nonMappedFieldFqcns = [];

$reflectionClass = new ReflectionClass($metadata->getName());

foreach ($reflectionClass->getProperties() as $property) {
$propertyName = $property->getName();

if ($metadata->hasField($propertyName)) {
continue;
}

if ($property->getType() === null || ! array_key_exists($propertyName, $defaultProperties)) {
continue;
}

$nonMappedFieldFqcns[] = $this->propertyFqcn($property);
}

return $nonMappedFieldFqcns;
}

private function propertyFqcn(ReflectionProperty $property): string
{
if ($property->isPrivate()) {
Expand Down

0 comments on commit 47540d6

Please sign in to comment.