Skip to content

Commit

Permalink
Fix private identifiers of parent classes ignored in skippedProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
simonberger committed Jan 3, 2023
1 parent c70dd51 commit 05e054c
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions lib/Doctrine/ORM/Proxy/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
use Doctrine\ORM\Utility\IdentifierFlattener;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Proxy;
use ReflectionClass;
use Symfony\Component\VarExporter\ProxyHelper;
use Symfony\Component\VarExporter\VarExporter;

use function array_flip;
use function array_merge;
use function str_replace;
use function strpos;
use function substr;
Expand Down Expand Up @@ -311,10 +313,39 @@ private function generateUseLazyGhostTrait(ClassMetadata $class): string

private function generateSkippedProperties(ClassMetadata $class): string
{
$skippedProperties = ['__isCloning' => true];
$identifiers = array_flip($class->getIdentifierFieldNames());
$skippedProperties = array_merge(
['__isCloning' => true],
$this->collectClassSkippedProperties(
$class->getReflectionClass(),
$class,
array_flip($class->getIdentifierFieldNames())
)
);

uksort($skippedProperties, 'strnatcmp');

$code = VarExporter::export($skippedProperties);

return str_replace([VarExporter::export($class->getName()), "\n"], ['parent::class', "\n "], $code);
}

/**
* @psalm-param array<string, int> $identifiers
* @psalm-return array<string, true>
*/
private function collectClassSkippedProperties(
ReflectionClass $refClass,
ClassMetadata $class,
array $identifiers,
bool $processPrivatePropertiesOnly = false
): array {
$skippedProperties = [];
foreach ($refClass->getProperties() as $property) {
if ($processPrivatePropertiesOnly && ! $property->isPrivate()) {
continue;
}

foreach ($class->getReflectionClass()->getProperties() as $property) {
$name = $property->getName();

if ($property->isStatic() || (($class->hasField($name) || $class->hasAssociation($name)) && ! isset($identifiers[$name]))) {
Expand All @@ -326,13 +357,20 @@ private function generateSkippedProperties(ClassMetadata $class): string
$skippedProperties[$prefix . $name] = true;
}

uksort($skippedProperties, 'strnatcmp');

$code = VarExporter::export($skippedProperties);
$code = str_replace(VarExporter::export($class->getName()), 'parent::class', $code);
$code = str_replace("\n", "\n ", $code);
$parentClass = $refClass->getParentClass();
if ($parentClass !== false) {
return array_merge(
$skippedProperties,
$this->collectClassSkippedProperties(
$parentClass,
$class,
$identifiers,
true
)
);
}

return $code;
return $skippedProperties;
}

private function generateSerializeImpl(ClassMetadata $class): string
Expand Down

0 comments on commit 05e054c

Please sign in to comment.