From 4df7699ced33efdfab08d46e4135293b5dbbb138 Mon Sep 17 00:00:00 2001 From: Andreas Schempp Date: Sun, 6 Dec 2015 14:56:36 +0100 Subject: [PATCH] Reduce code duplication in ProxyFactory::createInitialized The only difference between the ~30 lines in the IF-statement is that the __wakup method is called on the proxy object. --- lib/Doctrine/ORM/Proxy/ProxyFactory.php | 46 ++++--------------------- 1 file changed, 7 insertions(+), 39 deletions(-) diff --git a/lib/Doctrine/ORM/Proxy/ProxyFactory.php b/lib/Doctrine/ORM/Proxy/ProxyFactory.php index c6f1cbd3d..72f161eab 100644 --- a/lib/Doctrine/ORM/Proxy/ProxyFactory.php +++ b/lib/Doctrine/ORM/Proxy/ProxyFactory.php @@ -123,45 +123,9 @@ class ProxyFactory extends AbstractProxyFactory */ private function createInitializer(ClassMetadata $classMetadata, EntityPersister $entityPersister) { - if ($classMetadata->getReflectionClass()->hasMethod('__wakeup')) { - return function (BaseProxy $proxy) use ($entityPersister, $classMetadata) { - $initializer = $proxy->__getInitializer(); - $cloner = $proxy->__getCloner(); + $wakeupProxy = $classMetadata->getReflectionClass()->hasMethod('__wakeup'); - $proxy->__setInitializer(null); - $proxy->__setCloner(null); - - if ($proxy->__isInitialized()) { - return; - } - - $properties = $proxy->__getLazyProperties(); - - foreach ($properties as $propertyName => $property) { - if ( ! isset($proxy->$propertyName)) { - $proxy->$propertyName = $properties[$propertyName]; - } - } - - $proxy->__setInitialized(true); - $proxy->__wakeup(); - - $identifier = $classMetadata->getIdentifierValues($proxy); - - if (null === $entityPersister->loadById($identifier, $proxy)) { - $proxy->__setInitializer($initializer); - $proxy->__setCloner($cloner); - $proxy->__setInitialized(false); - - throw EntityNotFoundException::fromClassNameAndIdentifier( - $classMetadata->getName(), - $this->identifierFlattener->flattenIdentifier($classMetadata, $identifier) - ); - } - }; - } - - return function (BaseProxy $proxy) use ($entityPersister, $classMetadata) { + return function (BaseProxy $proxy) use ($entityPersister, $classMetadata, $wakeupProxy) { $initializer = $proxy->__getInitializer(); $cloner = $proxy->__getCloner(); @@ -175,13 +139,17 @@ class ProxyFactory extends AbstractProxyFactory $properties = $proxy->__getLazyProperties(); foreach ($properties as $propertyName => $property) { - if (!isset($proxy->$propertyName)) { + if ( ! isset($proxy->$propertyName)) { $proxy->$propertyName = $properties[$propertyName]; } } $proxy->__setInitialized(true); + if ($wakeupProxy) { + $proxy->__wakeup(); + } + $identifier = $classMetadata->getIdentifierValues($proxy); if (null === $entityPersister->loadById($identifier, $proxy)) {