From 4e3e8bd63fd99e326e086703a8451392549c60f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Sat, 7 Oct 2023 13:25:15 +0200 Subject: [PATCH] Stop extending proxy factory from doctrine/common Extending it no longer serves any purpose. --- UPGRADE.md | 8 ++ lib/Doctrine/ORM/Proxy/ProxyFactory.php | 150 ++---------------------- phpstan-baseline.neon | 7 +- psalm-baseline.xml | 22 ---- 4 files changed, 16 insertions(+), 171 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 6f60fc3a528..96e47564a41 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,13 @@ # Upgrade to 3.0 +## BC BREAK: `Doctrine\ORM\Proxy\ProxyFactory` no longer extends abstract factory from `doctrine/common` + +It is no longer possible to call methods, constants or properties inherited +from that class on a `ProxyFactory` instance. + +`Doctrine\ORM\Proxy\ProxyFactory::createProxyDefinition()` and +`Doctrine\ORM\Proxy\ProxyFactory::resetUninitializedProxy()` are removed as well. + ## BC BREAK: lazy ghosts are enabled unconditionally `Doctrine\ORM\Configuration::setLazyGhostObjectEnabled()` and diff --git a/lib/Doctrine/ORM/Proxy/ProxyFactory.php b/lib/Doctrine/ORM/Proxy/ProxyFactory.php index 0bca76ca1e2..f04bd8c6625 100644 --- a/lib/Doctrine/ORM/Proxy/ProxyFactory.php +++ b/lib/Doctrine/ORM/Proxy/ProxyFactory.php @@ -5,9 +5,6 @@ namespace Doctrine\ORM\Proxy; use Closure; -use Doctrine\Common\Proxy\AbstractProxyFactory; -use Doctrine\Common\Proxy\Proxy as CommonProxy; -use Doctrine\Common\Proxy\ProxyDefinition; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityNotFoundException; use Doctrine\ORM\ORMInvalidArgumentException; @@ -18,7 +15,6 @@ use Doctrine\Persistence\Proxy; use ReflectionProperty; use Symfony\Component\VarExporter\ProxyHelper; -use Throwable; use function array_combine; use function array_flip; @@ -53,7 +49,7 @@ /** * This factory is used to create proxy objects for entities at runtime. */ -class ProxyFactory extends AbstractProxyFactory +class ProxyFactory { /** * Never autogenerate a proxy and rely that it was generated by some @@ -168,9 +164,12 @@ public function __construct( } /** - * {@inheritDoc} + * @param class-string $className + * @param array $identifier + * + * @return InternalProxy */ - public function getProxy($className, array $identifier) + public function getProxy(string $className, array $identifier) { $proxyFactory = $this->proxyFactories[$className] ?? $this->getProxyFactory($className); @@ -187,7 +186,7 @@ public function getProxy($className, array $identifier) * * @return int Number of generated proxies. */ - public function generateProxyClasses(array $classes, $proxyDir = null) + public function generateProxyClasses(array $classes, $proxyDir = null): int { $generated = 0; @@ -207,16 +206,6 @@ public function generateProxyClasses(array $classes, $proxyDir = null) return $generated; } - /** - * {@inheritDoc} - * - * @deprecated ProxyFactory::resetUninitializedProxy() is deprecated and will be removed in version 3.0 of doctrine/orm. - */ - public function resetUninitializedProxy(CommonProxy $proxy) - { - return parent::resetUninitializedProxy($proxy); - } - protected function skipClass(ClassMetadata $metadata): bool { return $metadata->isMappedSuperclass @@ -224,91 +213,6 @@ protected function skipClass(ClassMetadata $metadata): bool || $metadata->getReflectionClass()->isAbstract(); } - /** - * {@inheritDoc} - * - * @deprecated ProxyFactory::createProxyDefinition() is deprecated and will be removed in version 3.0 of doctrine/orm. - */ - protected function createProxyDefinition($className): ProxyDefinition - { - $classMetadata = $this->em->getClassMetadata($className); - $entityPersister = $this->uow->getEntityPersister($className); - - $initializer = $this->createInitializer($classMetadata, $entityPersister); - $cloner = $this->createCloner($classMetadata, $entityPersister); - - return new ProxyDefinition( - self::generateProxyClassName($className, $this->proxyNs), - $classMetadata->getIdentifierFieldNames(), - $classMetadata->getReflectionProperties(), - $initializer, - $cloner, - ); - } - - /** - * Creates a closure capable of initializing a proxy - * - * @deprecated ProxyFactory::createInitializer() is deprecated and will be removed in version 3.0 of doctrine/orm. - * - * @psalm-return Closure(CommonProxy):void - * - * @throws EntityNotFoundException - */ - private function createInitializer(ClassMetadata $classMetadata, EntityPersister $entityPersister): Closure - { - $wakeupProxy = $classMetadata->getReflectionClass()->hasMethod('__wakeup'); - - return function (CommonProxy $proxy) use ($entityPersister, $classMetadata, $wakeupProxy): void { - $initializer = $proxy->__getInitializer(); - $cloner = $proxy->__getCloner(); - - $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); - - if ($wakeupProxy) { - $proxy->__wakeup(); - } - - $identifier = $classMetadata->getIdentifierValues($proxy); - - try { - $entity = $entityPersister->loadById($identifier, $proxy); - } catch (Throwable $exception) { - $proxy->__setInitializer($initializer); - $proxy->__setCloner($cloner); - $proxy->__setInitialized(false); - - throw $exception; - } - - if ($entity === null) { - $proxy->__setInitializer($initializer); - $proxy->__setCloner($cloner); - $proxy->__setInitialized(false); - - throw EntityNotFoundException::fromClassNameAndIdentifier( - $classMetadata->getName(), - $this->identifierFlattener->flattenIdentifier($classMetadata, $identifier), - ); - } - }; - } - /** * Creates a closure capable of initializing a proxy * @@ -345,46 +249,6 @@ private function createLazyInitializer(ClassMetadata $classMetadata, EntityPersi }; } - /** - * Creates a closure capable of finalizing state a cloned proxy - * - * @deprecated ProxyFactory::createCloner() is deprecated and will be removed in version 3.0 of doctrine/orm. - * - * @psalm-return Closure(CommonProxy):void - * - * @throws EntityNotFoundException - */ - private function createCloner(ClassMetadata $classMetadata, EntityPersister $entityPersister): Closure - { - return function (CommonProxy $proxy) use ($entityPersister, $classMetadata): void { - if ($proxy->__isInitialized()) { - return; - } - - $proxy->__setInitialized(true); - $proxy->__setInitializer(null); - - $class = $entityPersister->getClassMetadata(); - $identifier = $classMetadata->getIdentifierValues($proxy); - $original = $entityPersister->loadById($identifier); - - if ($original === null) { - throw EntityNotFoundException::fromClassNameAndIdentifier( - $classMetadata->getName(), - $this->identifierFlattener->flattenIdentifier($classMetadata, $identifier), - ); - } - - foreach ($class->getReflectionProperties() as $property) { - if (! $class->hasField($property->name) && ! $class->hasAssociation($property->name)) { - continue; - } - - $property->setValue($proxy, $property->getValue($original)); - } - }; - } - private function getProxyFileName(string $className, string $baseDirectory): string { $baseDirectory = $baseDirectory ?: $this->proxyDir; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 31b43e55fc0..e76366ac6fc 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -190,11 +190,6 @@ parameters: count: 1 path: lib/Doctrine/ORM/Proxy/ProxyFactory.php - - - message: "#^Call to an undefined method Doctrine\\\\Common\\\\Proxy\\\\Proxy\\:\\:__wakeup\\(\\)\\.$#" - count: 1 - path: lib/Doctrine/ORM/Proxy/ProxyFactory.php - - message: "#^Call to an undefined static method Doctrine\\\\ORM\\\\Proxy\\\\ProxyFactory\\:\\:createLazyGhost\\(\\)\\.$#" count: 1 @@ -212,7 +207,7 @@ parameters: - message: "#^Parameter \\#1 \\$class of method Doctrine\\\\ORM\\\\Utility\\\\IdentifierFlattener\\:\\:flattenIdentifier\\(\\) expects Doctrine\\\\ORM\\\\Mapping\\\\ClassMetadata, Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata given\\.$#" - count: 3 + count: 1 path: lib/Doctrine/ORM/Proxy/ProxyFactory.php - diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 6f4c80c5c96..6ea5468a0fa 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -738,16 +738,7 @@ $classMetadata - $classMetadata - $classMetadata - - createCloner - createInitializer - - - getReflectionProperties()]]> - Closure @@ -758,9 +749,6 @@ isEmbeddedClass]]> isMappedSuperclass]]> - - name]]> - proxyFactories[$className] = $proxyFactory]]> @@ -770,20 +758,10 @@ $i - - name]]> - - - getValue - setValue - 4]]> - - __wakeup -