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

Don't Generate Set or Unset on Proxy of Readonly Public Properties #957

Open
wants to merge 5 commits into
base: 3.2.x
Choose a base branch
from
Open
Changes from 1 commit
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
31 changes: 28 additions & 3 deletions lib/Doctrine/Common/Proxy/ProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ public function __construct(?\Closure $initializer = null, ?\Closure $cloner = n

$toUnset = array_map(static function (string $name): string {
return '$this->' . $name;
}, $this->getLazyLoadedPublicPropertiesNames($class));
}, $this->getWriteableLazyLoadedPublicPropertiesNames($class));

return $constructorImpl . ($toUnset === [] ? '' : ' unset(' . implode(', ', $toUnset) . ");\n")
. <<<'EOT'
Expand Down Expand Up @@ -591,7 +591,7 @@ public function {$returnReference}__get($parametersString)$returnTypeHint
*/
private function generateMagicSet(ClassMetadata $class)
{
$lazyPublicProperties = $this->getLazyLoadedPublicPropertiesNames($class);
$lazyPublicProperties = $this->getWriteableLazyLoadedPublicPropertiesNames($class);
$reflectionClass = $class->getReflectionClass();
$hasParentSet = false;
$inheritDoc = '';
Expand Down Expand Up @@ -808,7 +808,7 @@ private function generateWakeupImpl(ClassMetadata $class)
$hasParentWakeup = $reflectionClass->hasMethod('__wakeup');

$unsetPublicProperties = [];
foreach ($this->getLazyLoadedPublicPropertiesNames($class) as $lazyPublicProperty) {
foreach ($this->getWriteableLazyLoadedPublicPropertiesNames($class) as $lazyPublicProperty) {
$unsetPublicProperties[] = '$this->' . $lazyPublicProperty;
}

Expand Down Expand Up @@ -1005,6 +1005,31 @@ private function isShortIdentifierGetter($method, ClassMetadata $class)
return false;
}

/**
* Generates the list of public properties to be lazy loaded, that are writable.
*
* @return array<int, string>
greg0ire marked this conversation as resolved.
Show resolved Hide resolved
*/
public function getWriteableLazyLoadedPublicPropertiesNames(ClassMetadata $class): array
{
$properties = [];

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

if ((! $class->hasField($name) && ! $class->hasAssociation($name))
|| $class->isIdentifier($name)
|| (method_exists($property, 'isReadOnly') && $property->isReadOnly())
) {
continue;
}

$properties[] = $name;
}

return $properties;
}

/**
* Generates the list of public properties to be lazy loaded.
*
Expand Down