Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: ProxyGenerator incompatible with typed properties and lazy loading #887

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 46 additions & 4 deletions lib/Doctrine/Common/Proxy/ProxyGenerator.php
Expand Up @@ -641,13 +641,12 @@ public function __sleep()
continue;
}

$allProperties[] = $prop->isPrivate()
? "\0" . $prop->getDeclaringClass()->getName() . "\0" . $prop->getName()
: $prop->getName();
$allProperties[] = $this->getPropertyName($prop);
}

$lazyPublicProperties = $this->getLazyLoadedPublicPropertiesNames($class);
$protectedProperties = array_diff($allProperties, $lazyPublicProperties);
$lazyTypedProperties = $this->getLazyLoadedTypedPropertiesNames($class);
$protectedProperties = array_diff($allProperties, $lazyPublicProperties, $lazyTypedProperties);

foreach ($allProperties as &$property) {
$property = var_export($property, true);
Expand Down Expand Up @@ -905,6 +904,49 @@ private function getLazyLoadedPublicPropertiesNames(ClassMetadata $class) : arra
return $properties;
}

/**
* Generates the list of typed properties.
*
* @param \Doctrine\Persistence\Mapping\ClassMetadata $class
*
* @return array<int, string>
*/
private function getLazyLoadedTypedPropertiesNames(ClassMetadata $class) : array
{
$properties = [];

if (PHP_VERSION_ID < 70400) {
return $properties;
}

foreach ($class->getReflectionClass()->getProperties() as $prop) {
if ($prop->hasType() === false) {
continue;
}

$name = $prop->getName();
if (($class->hasField($name) || $class->hasAssociation($name)) && ! $class->isIdentifier($name)) {
$properties[] = $this->getPropertyName($prop);
}
}

return $properties;
}

/**
* @param \ReflectionProperty $property
*
* @return string
*/
private function getPropertyName(\ReflectionProperty $property) : string
{
if ($property->isPrivate()) {
return "\0" . $property->getDeclaringClass()->getName() . "\0" . $property->getName();
}

return $property->getName();
}

/**
* Generates the list of default values of public properties.
*
Expand Down