From 597460d5ea40989cf6b54cc0000d04b6b1f88f3c Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Tue, 22 Feb 2022 00:47:00 +0100 Subject: [PATCH] Add native types to caches (#9508) --- UPGRADE.md | 9 ++ lib/Doctrine/ORM/Cache.php | 86 ++----------- .../ORM/Cache/AssociationCacheEntry.php | 12 +- lib/Doctrine/ORM/Cache/CacheConfiguration.php | 71 +++------- lib/Doctrine/ORM/Cache/CacheException.php | 40 +----- lib/Doctrine/ORM/Cache/CacheFactory.php | 38 ++---- lib/Doctrine/ORM/Cache/CacheKey.php | 3 +- .../ORM/Cache/CollectionCacheEntry.php | 6 +- lib/Doctrine/ORM/Cache/CollectionCacheKey.php | 16 +-- lib/Doctrine/ORM/Cache/CollectionHydrator.php | 8 +- lib/Doctrine/ORM/Cache/DefaultCache.php | 121 ++++-------------- .../ORM/Cache/DefaultCacheFactory.php | 57 +++------ .../ORM/Cache/DefaultCollectionHydrator.php | 25 ++-- lib/Doctrine/ORM/Cache/DefaultQueryCache.php | 83 ++++-------- lib/Doctrine/ORM/Cache/EntityCacheEntry.php | 16 +-- lib/Doctrine/ORM/Cache/EntityCacheKey.php | 7 +- lib/Doctrine/ORM/Cache/Lock.php | 20 +-- .../AbstractCollectionPersister.php | 49 ------- lib/Doctrine/ORM/Cache/QueryCache.php | 17 +-- lib/Doctrine/ORM/Cache/QueryCacheEntry.php | 12 +- lib/Doctrine/ORM/Cache/QueryCacheKey.php | 13 +- .../ORM/Cache/QueryCacheValidator.php | 4 +- .../ORM/Cache/Region/DefaultRegion.php | 14 +- .../ORM/Cache/Region/FileLockRegion.php | 10 +- .../ORM/Cache/RegionsConfiguration.php | 84 +++--------- .../ORM/Cache/TimestampCacheEntry.php | 18 +-- lib/Doctrine/ORM/Cache/TimestampCacheKey.php | 4 +- .../Cache/TimestampQueryCacheValidator.php | 8 +- psalm-baseline.xml | 32 +---- .../Tests/ORM/Cache/AbstractRegionTest.php | 11 +- .../Tests/ORM/Cache/CacheConfigTest.php | 3 +- .../ORM/Cache/DefaultCacheFactoryTest.php | 11 +- .../Tests/ORM/Cache/DefaultCacheTest.php | 7 +- .../Cache/DefaultCollectionHydratorTest.php | 3 +- .../Tests/ORM/Cache/DefaultQueryCacheTest.php | 27 ++-- .../Tests/ORM/Cache/DefaultRegionTest.php | 3 +- .../Tests/ORM/Cache/FileLockRegionTest.php | 7 +- .../SecondLevelCacheAbstractTest.php | 23 ++-- .../SecondLevelCacheConcurrentTest.php | 7 +- 39 files changed, 238 insertions(+), 747 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index f85e1af28de..31966567bde 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,14 @@ # Upgrade to 3.0 +## BC BREAK: Remove helper methods from `AbstractCollectionPersister` + +The following protected methods of +`Doctrine\ORM\Cache\Persister\Collection\AbstractCollectionPersister` +have been removed. + +* `evictCollectionCache()` +* `evictElementCache()` + ## BC BREAK: `Doctrine\ORM\Query\TreeWalkerChainIterator` This class has been removed without replacement. diff --git a/lib/Doctrine/ORM/Cache.php b/lib/Doctrine/ORM/Cache.php index 52c3903a0bb..6e34f747839 100644 --- a/lib/Doctrine/ORM/Cache.php +++ b/lib/Doctrine/ORM/Cache.php @@ -38,127 +38,69 @@ interface Cache */ public const MODE_REFRESH = 4; - /** - * @param string $className The entity class. - * - * @return Region|null - */ - public function getEntityCacheRegion($className); + public function getEntityCacheRegion(string $className): ?Region; - /** - * @param string $className The entity class. - * @param string $association The field name that represents the association. - * - * @return Region|null - */ - public function getCollectionCacheRegion($className, $association); + public function getCollectionCacheRegion(string $className, string $association): ?Region; /** * Determine whether the cache contains data for the given entity "instance". - * - * @param string $className The entity class. - * @param mixed $identifier The entity identifier - * - * @return bool true if the underlying cache contains corresponding data; false otherwise. */ - public function containsEntity($className, $identifier); + public function containsEntity(string $className, mixed $identifier): bool; /** * Evicts the entity data for a particular entity "instance". - * - * @param string $className The entity class. - * @param mixed $identifier The entity identifier. - * - * @return void */ - public function evictEntity($className, $identifier); + public function evictEntity(string $className, mixed $identifier): void; /** * Evicts all entity data from the given region. - * - * @param string $className The entity metadata. - * - * @return void */ - public function evictEntityRegion($className); + public function evictEntityRegion(string $className): void; /** * Evict data from all entity regions. - * - * @return void */ - public function evictEntityRegions(); + public function evictEntityRegions(): void; /** * Determine whether the cache contains data for the given collection. - * - * @param string $className The entity class. - * @param string $association The field name that represents the association. - * @param mixed $ownerIdentifier The identifier of the owning entity. - * - * @return bool true if the underlying cache contains corresponding data; false otherwise. */ - public function containsCollection($className, $association, $ownerIdentifier); + public function containsCollection(string $className, string $association, mixed $ownerIdentifier): bool; /** * Evicts the cache data for the given identified collection instance. - * - * @param string $className The entity class. - * @param string $association The field name that represents the association. - * @param mixed $ownerIdentifier The identifier of the owning entity. - * - * @return void */ - public function evictCollection($className, $association, $ownerIdentifier); + public function evictCollection(string $className, string $association, mixed $ownerIdentifier): void; /** * Evicts all entity data from the given region. - * - * @param string $className The entity class. - * @param string $association The field name that represents the association. - * - * @return void */ - public function evictCollectionRegion($className, $association); + public function evictCollectionRegion(string $className, string $association): void; /** * Evict data from all collection regions. - * - * @return void */ - public function evictCollectionRegions(); + public function evictCollectionRegions(): void; /** * Determine whether the cache contains data for the given query. - * - * @param string $regionName The cache name given to the query. - * - * @return bool true if the underlying cache contains corresponding data; false otherwise. */ - public function containsQuery($regionName); + public function containsQuery(string $regionName): bool; /** * Evicts all cached query results under the given name, or default query cache if the region name is NULL. - * - * @param string|null $regionName The cache name associated to the queries being cached. - * - * @return void */ - public function evictQueryRegion($regionName = null); + public function evictQueryRegion(?string $regionName = null): void; /** * Evict data from all query regions. - * - * @return void */ - public function evictQueryRegions(); + public function evictQueryRegions(): void; /** * Get query cache by region name or create a new one if none exist. * * @param string|null $regionName Query cache region name, or default query cache if the region name is NULL. - * - * @return QueryCache The Query Cache associated with the region name. */ - public function getQueryCache($regionName = null); + public function getQueryCache(?string $regionName = null): QueryCache; } diff --git a/lib/Doctrine/ORM/Cache/AssociationCacheEntry.php b/lib/Doctrine/ORM/Cache/AssociationCacheEntry.php index f10618fb2b9..810ecde1d3f 100644 --- a/lib/Doctrine/ORM/Cache/AssociationCacheEntry.php +++ b/lib/Doctrine/ORM/Cache/AssociationCacheEntry.php @@ -15,23 +15,21 @@ class AssociationCacheEntry implements CacheEntry * @readonly Public only for performance reasons, it should be considered immutable. * @var array */ - public $identifier; + public array $identifier; /** * The entity class name * * @readonly Public only for performance reasons, it should be considered immutable. - * @var string * @psalm-var class-string */ - public $class; + public string $class; /** - * @param string $class The entity class. * @param array $identifier The entity identifier. * @psalm-param class-string $class */ - public function __construct($class, array $identifier) + public function __construct(string $class, array $identifier) { $this->class = $class; $this->identifier = $identifier; @@ -43,10 +41,8 @@ public function __construct($class, array $identifier) * This method allow Doctrine\Common\Cache\PhpFileCache compatibility * * @param array $values array containing property values - * - * @return AssociationCacheEntry */ - public static function __set_state(array $values) + public static function __set_state(array $values): self { return new self($values['class'], $values['identifier']); } diff --git a/lib/Doctrine/ORM/Cache/CacheConfiguration.php b/lib/Doctrine/ORM/Cache/CacheConfiguration.php index f3ad9a937a4..99417792beb 100644 --- a/lib/Doctrine/ORM/Cache/CacheConfiguration.php +++ b/lib/Doctrine/ORM/Cache/CacheConfiguration.php @@ -11,88 +11,49 @@ */ class CacheConfiguration { - /** @var CacheFactory|null */ - private $cacheFactory; + private ?CacheFactory $cacheFactory = null; + private ?RegionsConfiguration $regionsConfig = null; + private ?CacheLogger $cacheLogger = null; + private ?QueryCacheValidator $queryValidator = null; - /** @var RegionsConfiguration|null */ - private $regionsConfig; - - /** @var CacheLogger|null */ - private $cacheLogger; - - /** @var QueryCacheValidator|null */ - private $queryValidator; - - /** - * @return CacheFactory|null - */ - public function getCacheFactory() + public function getCacheFactory(): ?CacheFactory { return $this->cacheFactory; } - /** - * @return void - */ - public function setCacheFactory(CacheFactory $factory) + public function setCacheFactory(CacheFactory $factory): void { $this->cacheFactory = $factory; } - /** - * @return CacheLogger|null - */ - public function getCacheLogger() + public function getCacheLogger(): ?CacheLogger { return $this->cacheLogger; } - /** - * @return void - */ - public function setCacheLogger(CacheLogger $logger) + public function setCacheLogger(CacheLogger $logger): void { $this->cacheLogger = $logger; } - /** - * @return RegionsConfiguration - */ - public function getRegionsConfiguration() + public function getRegionsConfiguration(): RegionsConfiguration { - if ($this->regionsConfig === null) { - $this->regionsConfig = new RegionsConfiguration(); - } - - return $this->regionsConfig; + return $this->regionsConfig ??= new RegionsConfiguration(); } - /** - * @return void - */ - public function setRegionsConfiguration(RegionsConfiguration $regionsConfig) + public function setRegionsConfiguration(RegionsConfiguration $regionsConfig): void { $this->regionsConfig = $regionsConfig; } - /** - * @return QueryCacheValidator - */ - public function getQueryValidator() + public function getQueryValidator(): QueryCacheValidator { - if ($this->queryValidator === null) { - $this->queryValidator = new TimestampQueryCacheValidator( - $this->cacheFactory->getTimestampRegion() - ); - } - - return $this->queryValidator; + return $this->queryValidator ??= new TimestampQueryCacheValidator( + $this->cacheFactory->getTimestampRegion() + ); } - /** - * @return void - */ - public function setQueryValidator(QueryCacheValidator $validator) + public function setQueryValidator(QueryCacheValidator $validator): void { $this->queryValidator = $validator; } diff --git a/lib/Doctrine/ORM/Cache/CacheException.php b/lib/Doctrine/ORM/Cache/CacheException.php index b4db89b9ed5..b42209580dc 100644 --- a/lib/Doctrine/ORM/Cache/CacheException.php +++ b/lib/Doctrine/ORM/Cache/CacheException.php @@ -14,49 +14,13 @@ */ class CacheException extends LogicException implements ORMException { - /** - * @param string $sourceEntity - * @param string $fieldName - * - * @return CacheException - */ - public static function updateReadOnlyCollection($sourceEntity, $fieldName) + public static function updateReadOnlyCollection(string $sourceEntity, string $fieldName): self { return new self(sprintf('Cannot update a readonly collection "%s#%s"', $sourceEntity, $fieldName)); } - /** - * @deprecated This method is not used anymore. - * - * @param string $entityName - * - * @return CacheException - */ - public static function updateReadOnlyEntity($entityName) - { - return new self(sprintf('Cannot update a readonly entity "%s"', $entityName)); - } - - /** - * @param string $entityName - * - * @return CacheException - */ - public static function nonCacheableEntity($entityName) + public static function nonCacheableEntity(string $entityName): self { return new self(sprintf('Entity "%s" not configured as part of the second-level cache.', $entityName)); } - - /** - * @deprecated This method is not used anymore. - * - * @param string $entityName - * @param string $field - * - * @return CacheException - */ - public static function nonCacheableEntityAssociation($entityName, $field) - { - return new self(sprintf('Entity association field "%s#%s" not configured as part of the second-level cache.', $entityName, $field)); - } } diff --git a/lib/Doctrine/ORM/Cache/CacheFactory.php b/lib/Doctrine/ORM/Cache/CacheFactory.php index 60bd582c1d9..405ad86b74c 100644 --- a/lib/Doctrine/ORM/Cache/CacheFactory.php +++ b/lib/Doctrine/ORM/Cache/CacheFactory.php @@ -19,69 +19,47 @@ interface CacheFactory { /** * Build an entity persister for the given entity metadata. - * - * @param EntityManagerInterface $em The entity manager. - * @param EntityPersister $persister The entity persister that will be cached. - * @param ClassMetadata $metadata The entity metadata. - * - * @return CachedEntityPersister */ - public function buildCachedEntityPersister(EntityManagerInterface $em, EntityPersister $persister, ClassMetadata $metadata); + public function buildCachedEntityPersister(EntityManagerInterface $em, EntityPersister $persister, ClassMetadata $metadata): CachedEntityPersister; /** * Build a collection persister for the given relation mapping. * * @param mixed[] $mapping The association mapping. - * - * @return CachedCollectionPersister */ - public function buildCachedCollectionPersister(EntityManagerInterface $em, CollectionPersister $persister, array $mapping); + public function buildCachedCollectionPersister(EntityManagerInterface $em, CollectionPersister $persister, array $mapping): CachedCollectionPersister; /** * Build a query cache based on the given region name - * - * @param string|null $regionName The region name. - * - * @return QueryCache The built query cache. */ - public function buildQueryCache(EntityManagerInterface $em, $regionName = null); + public function buildQueryCache(EntityManagerInterface $em, ?string $regionName = null): QueryCache; /** * Build an entity hydrator - * - * @return EntityHydrator The built entity hydrator. */ - public function buildEntityHydrator(EntityManagerInterface $em, ClassMetadata $metadata); + public function buildEntityHydrator(EntityManagerInterface $em, ClassMetadata $metadata): EntityHydrator; /** * Build a collection hydrator * * @param mixed[] $mapping The association mapping. - * - * @return CollectionHydrator The built collection hydrator. */ - public function buildCollectionHydrator(EntityManagerInterface $em, array $mapping); + public function buildCollectionHydrator(EntityManagerInterface $em, array $mapping): CollectionHydrator; /** * Build a cache region * * @param array $cache The cache configuration. - * - * @return Region The cache region. */ - public function getRegion(array $cache); + public function getRegion(array $cache): Region; /** * Build timestamp cache region - * - * @return TimestampRegion The timestamp region. */ - public function getTimestampRegion(); + public function getTimestampRegion(): TimestampRegion; /** * Build \Doctrine\ORM\Cache - * - * @return Cache */ - public function createCache(EntityManagerInterface $entityManager); + public function createCache(EntityManagerInterface $entityManager): Cache; } diff --git a/lib/Doctrine/ORM/Cache/CacheKey.php b/lib/Doctrine/ORM/Cache/CacheKey.php index 5f4305d6865..47466c063fa 100644 --- a/lib/Doctrine/ORM/Cache/CacheKey.php +++ b/lib/Doctrine/ORM/Cache/CacheKey.php @@ -14,7 +14,6 @@ abstract class CacheKey * Unique identifier * * @readonly Public only for performance reasons, it should be considered immutable. - * @var string */ - public $hash; + public string $hash; } diff --git a/lib/Doctrine/ORM/Cache/CollectionCacheEntry.php b/lib/Doctrine/ORM/Cache/CollectionCacheEntry.php index fe3eb3d5ae2..e5904bc44da 100644 --- a/lib/Doctrine/ORM/Cache/CollectionCacheEntry.php +++ b/lib/Doctrine/ORM/Cache/CollectionCacheEntry.php @@ -15,7 +15,7 @@ class CollectionCacheEntry implements CacheEntry * @readonly Public only for performance reasons, it should be considered immutable. * @var CacheKey[] */ - public $identifiers; + public array $identifiers; /** * @param CacheKey[] $identifiers List of entity identifiers hold by the collection @@ -31,10 +31,8 @@ public function __construct(array $identifiers) * This method allows for Doctrine\Common\Cache\PhpFileCache compatibility * * @param array $values array containing property values - * - * @return CollectionCacheEntry */ - public static function __set_state(array $values) + public static function __set_state(array $values): CollectionCacheEntry { return new self($values['identifiers']); } diff --git a/lib/Doctrine/ORM/Cache/CollectionCacheKey.php b/lib/Doctrine/ORM/Cache/CollectionCacheKey.php index ec2c35db994..eb2414f83bd 100644 --- a/lib/Doctrine/ORM/Cache/CollectionCacheKey.php +++ b/lib/Doctrine/ORM/Cache/CollectionCacheKey.php @@ -20,38 +20,34 @@ class CollectionCacheKey extends CacheKey * @readonly Public only for performance reasons, it should be considered immutable. * @var array */ - public $ownerIdentifier; + public array $ownerIdentifier; /** * The owner entity class * * @readonly Public only for performance reasons, it should be considered immutable. - * @var string * @psalm-var class-string */ - public $entityClass; + public string $entityClass; /** * The association name * * @readonly Public only for performance reasons, it should be considered immutable. - * @var string */ - public $association; + public string $association; /** - * @param string $entityClass The entity class. - * @param string $association The field name that represents the association. * @param array $ownerIdentifier The identifier of the owning entity. * @psalm-param class-string $entityClass */ - public function __construct($entityClass, $association, array $ownerIdentifier) + public function __construct(string $entityClass, string $association, array $ownerIdentifier) { ksort($ownerIdentifier); $this->ownerIdentifier = $ownerIdentifier; - $this->entityClass = (string) $entityClass; - $this->association = (string) $association; + $this->entityClass = $entityClass; + $this->association = $association; $this->hash = str_replace('\\', '.', strtolower($entityClass)) . '_' . implode(' ', $ownerIdentifier) . '__' . $association; } } diff --git a/lib/Doctrine/ORM/Cache/CollectionHydrator.php b/lib/Doctrine/ORM/Cache/CollectionHydrator.php index 5d327a3198f..db8ba112542 100644 --- a/lib/Doctrine/ORM/Cache/CollectionHydrator.php +++ b/lib/Doctrine/ORM/Cache/CollectionHydrator.php @@ -14,14 +14,12 @@ interface CollectionHydrator { /** - * @param array|mixed[]|Collection $collection The collection. - * - * @return CollectionCacheEntry + * @param mixed[]|Collection $collection The collection. */ - public function buildCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, $collection); + public function buildCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, array|Collection $collection): CollectionCacheEntry; /** * @return mixed[]|null */ - public function loadCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, CollectionCacheEntry $entry, PersistentCollection $collection); + public function loadCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, CollectionCacheEntry $entry, PersistentCollection $collection): ?array; } diff --git a/lib/Doctrine/ORM/Cache/DefaultCache.php b/lib/Doctrine/ORM/Cache/DefaultCache.php index 5ca9eab822e..2aaaa8e66ef 100644 --- a/lib/Doctrine/ORM/Cache/DefaultCache.php +++ b/lib/Doctrine/ORM/Cache/DefaultCache.php @@ -20,37 +20,27 @@ */ class DefaultCache implements Cache { - /** @var EntityManagerInterface */ - private $em; - - /** @var UnitOfWork */ - private $uow; - - /** @var CacheFactory */ - private $cacheFactory; + private UnitOfWork $uow; + private CacheFactory $cacheFactory; /** * @var QueryCache[] * @psalm-var array */ - private $queryCaches = []; + private array $queryCaches = []; - /** @var QueryCache|null */ - private $defaultQueryCache; + private ?QueryCache $defaultQueryCache = null; - public function __construct(EntityManagerInterface $em) - { - $this->em = $em; + public function __construct( + private EntityManagerInterface $em + ) { $this->uow = $em->getUnitOfWork(); $this->cacheFactory = $em->getConfiguration() ->getSecondLevelCacheConfiguration() ->getCacheFactory(); } - /** - * {@inheritdoc} - */ - public function getEntityCacheRegion($className) + public function getEntityCacheRegion(string $className): ?Region { $metadata = $this->em->getClassMetadata($className); $persister = $this->uow->getEntityPersister($metadata->rootEntityName); @@ -62,10 +52,7 @@ public function getEntityCacheRegion($className) return $persister->getCacheRegion(); } - /** - * {@inheritdoc} - */ - public function getCollectionCacheRegion($className, $association) + public function getCollectionCacheRegion(string $className, string $association): ?Region { $metadata = $this->em->getClassMetadata($className); $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); @@ -77,10 +64,7 @@ public function getCollectionCacheRegion($className, $association) return $persister->getCacheRegion(); } - /** - * {@inheritdoc} - */ - public function containsEntity($className, $identifier) + public function containsEntity(string $className, mixed $identifier): bool { $metadata = $this->em->getClassMetadata($className); $persister = $this->uow->getEntityPersister($metadata->rootEntityName); @@ -92,10 +76,7 @@ public function containsEntity($className, $identifier) return $persister->getCacheRegion()->contains($this->buildEntityCacheKey($metadata, $identifier)); } - /** - * {@inheritdoc} - */ - public function evictEntity($className, $identifier) + public function evictEntity(string $className, mixed $identifier): void { $metadata = $this->em->getClassMetadata($className); $persister = $this->uow->getEntityPersister($metadata->rootEntityName); @@ -107,10 +88,7 @@ public function evictEntity($className, $identifier) $persister->getCacheRegion()->evict($this->buildEntityCacheKey($metadata, $identifier)); } - /** - * {@inheritdoc} - */ - public function evictEntityRegion($className) + public function evictEntityRegion(string $className): void { $metadata = $this->em->getClassMetadata($className); $persister = $this->uow->getEntityPersister($metadata->rootEntityName); @@ -122,10 +100,7 @@ public function evictEntityRegion($className) $persister->getCacheRegion()->evictAll(); } - /** - * {@inheritdoc} - */ - public function evictEntityRegions() + public function evictEntityRegions(): void { $metadatas = $this->em->getMetadataFactory()->getAllMetadata(); @@ -140,10 +115,7 @@ public function evictEntityRegions() } } - /** - * {@inheritdoc} - */ - public function containsCollection($className, $association, $ownerIdentifier) + public function containsCollection(string $className, string $association, mixed $ownerIdentifier): bool { $metadata = $this->em->getClassMetadata($className); $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); @@ -155,10 +127,7 @@ public function containsCollection($className, $association, $ownerIdentifier) return $persister->getCacheRegion()->contains($this->buildCollectionCacheKey($metadata, $association, $ownerIdentifier)); } - /** - * {@inheritdoc} - */ - public function evictCollection($className, $association, $ownerIdentifier) + public function evictCollection(string $className, string $association, mixed $ownerIdentifier): void { $metadata = $this->em->getClassMetadata($className); $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); @@ -170,10 +139,7 @@ public function evictCollection($className, $association, $ownerIdentifier) $persister->getCacheRegion()->evict($this->buildCollectionCacheKey($metadata, $association, $ownerIdentifier)); } - /** - * {@inheritdoc} - */ - public function evictCollectionRegion($className, $association) + public function evictCollectionRegion(string $className, string $association): void { $metadata = $this->em->getClassMetadata($className); $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); @@ -185,10 +151,7 @@ public function evictCollectionRegion($className, $association) $persister->getCacheRegion()->evictAll(); } - /** - * {@inheritdoc} - */ - public function evictCollectionRegions() + public function evictCollectionRegions(): void { $metadatas = $this->em->getMetadataFactory()->getAllMetadata(); @@ -209,18 +172,12 @@ public function evictCollectionRegions() } } - /** - * {@inheritdoc} - */ - public function containsQuery($regionName) + public function containsQuery(string $regionName): bool { return isset($this->queryCaches[$regionName]); } - /** - * {@inheritdoc} - */ - public function evictQueryRegion($regionName = null) + public function evictQueryRegion(?string $regionName = null): void { if ($regionName === null && $this->defaultQueryCache !== null) { $this->defaultQueryCache->clear(); @@ -233,10 +190,7 @@ public function evictQueryRegion($regionName = null) } } - /** - * {@inheritdoc} - */ - public function evictQueryRegions() + public function evictQueryRegions(): void { $this->getQueryCache()->clear(); @@ -245,27 +199,16 @@ public function evictQueryRegions() } } - /** - * {@inheritdoc} - */ - public function getQueryCache($regionName = null) + public function getQueryCache(?string $regionName = null): QueryCache { if ($regionName === null) { - return $this->defaultQueryCache ?: - $this->defaultQueryCache = $this->cacheFactory->buildQueryCache($this->em); - } - - if (! isset($this->queryCaches[$regionName])) { - $this->queryCaches[$regionName] = $this->cacheFactory->buildQueryCache($this->em, $regionName); + return $this->defaultQueryCache ??= $this->cacheFactory->buildQueryCache($this->em); } - return $this->queryCaches[$regionName]; + return $this->queryCaches[$regionName] ??= $this->cacheFactory->buildQueryCache($this->em, $regionName); } - /** - * @param mixed $identifier The entity identifier. - */ - private function buildEntityCacheKey(ClassMetadata $metadata, $identifier): EntityCacheKey + private function buildEntityCacheKey(ClassMetadata $metadata, mixed $identifier): EntityCacheKey { if (! is_array($identifier)) { $identifier = $this->toIdentifierArray($metadata, $identifier); @@ -274,13 +217,10 @@ private function buildEntityCacheKey(ClassMetadata $metadata, $identifier): Enti return new EntityCacheKey($metadata->rootEntityName, $identifier); } - /** - * @param mixed $ownerIdentifier The identifier of the owning entity. - */ private function buildCollectionCacheKey( ClassMetadata $metadata, string $association, - $ownerIdentifier + mixed $ownerIdentifier ): CollectionCacheKey { if (! is_array($ownerIdentifier)) { $ownerIdentifier = $this->toIdentifierArray($metadata, $ownerIdentifier); @@ -290,18 +230,13 @@ private function buildCollectionCacheKey( } /** - * @param mixed $identifier The entity identifier. - * * @return array */ - private function toIdentifierArray(ClassMetadata $metadata, $identifier): array + private function toIdentifierArray(ClassMetadata $metadata, mixed $identifier): array { if (is_object($identifier) && $this->em->getMetadataFactory()->hasMetadataFor(ClassUtils::getClass($identifier))) { - $identifier = $this->uow->getSingleIdentifierValue($identifier); - - if ($identifier === null) { - throw ORMInvalidArgumentException::invalidIdentifierBindingEntity(); - } + $identifier = $this->uow->getSingleIdentifierValue($identifier) + ?? throw ORMInvalidArgumentException::invalidIdentifierBindingEntity(); } return [$metadata->identifier[0] => $identifier]; diff --git a/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php b/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php index 9ebc5a62bf9..2ff5949cb10 100644 --- a/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php +++ b/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php @@ -5,9 +5,11 @@ namespace Doctrine\ORM\Cache; use Doctrine\ORM\Cache; +use Doctrine\ORM\Cache\Persister\Collection\CachedCollectionPersister; use Doctrine\ORM\Cache\Persister\Collection\NonStrictReadWriteCachedCollectionPersister; use Doctrine\ORM\Cache\Persister\Collection\ReadOnlyCachedCollectionPersister; use Doctrine\ORM\Cache\Persister\Collection\ReadWriteCachedCollectionPersister; +use Doctrine\ORM\Cache\Persister\Entity\CachedEntityPersister; use Doctrine\ORM\Cache\Persister\Entity\NonStrictReadWriteCachedEntityPersister; use Doctrine\ORM\Cache\Persister\Entity\ReadOnlyCachedEntityPersister; use Doctrine\ORM\Cache\Persister\Entity\ReadWriteCachedEntityPersister; @@ -44,44 +46,27 @@ public function __construct(RegionsConfiguration $cacheConfig, CacheItemPoolInte $this->regionsConfig = $cacheConfig; } - /** - * @param string $fileLockRegionDirectory - * - * @return void - */ - public function setFileLockRegionDirectory($fileLockRegionDirectory) + public function setFileLockRegionDirectory(string $fileLockRegionDirectory): void { - $this->fileLockRegionDirectory = (string) $fileLockRegionDirectory; + $this->fileLockRegionDirectory = $fileLockRegionDirectory; } - /** - * @return string - */ - public function getFileLockRegionDirectory() + public function getFileLockRegionDirectory(): string { return $this->fileLockRegionDirectory; } - /** - * @return void - */ - public function setRegion(Region $region) + public function setRegion(Region $region): void { $this->regions[$region->getName()] = $region; } - /** - * @return void - */ - public function setTimestampRegion(TimestampRegion $region) + public function setTimestampRegion(TimestampRegion $region): void { $this->timestampRegion = $region; } - /** - * {@inheritdoc} - */ - public function buildCachedEntityPersister(EntityManagerInterface $em, EntityPersister $persister, ClassMetadata $metadata) + public function buildCachedEntityPersister(EntityManagerInterface $em, EntityPersister $persister, ClassMetadata $metadata): CachedEntityPersister { assert($metadata->cache !== null); $region = $this->getRegion($metadata->cache); @@ -109,7 +94,7 @@ public function buildCachedEntityPersister(EntityManagerInterface $em, EntityPer /** * {@inheritdoc} */ - public function buildCachedCollectionPersister(EntityManagerInterface $em, CollectionPersister $persister, array $mapping) + public function buildCachedCollectionPersister(EntityManagerInterface $em, CollectionPersister $persister, array $mapping): CachedCollectionPersister { $usage = $mapping['cache']['usage']; $region = $this->getRegion($mapping['cache']); @@ -133,10 +118,7 @@ public function buildCachedCollectionPersister(EntityManagerInterface $em, Colle throw new InvalidArgumentException(sprintf('Unrecognized access strategy type [%s]', $usage)); } - /** - * {@inheritdoc} - */ - public function buildQueryCache(EntityManagerInterface $em, $regionName = null) + public function buildQueryCache(EntityManagerInterface $em, ?string $regionName = null): QueryCache { return new DefaultQueryCache( $em, @@ -152,15 +134,12 @@ public function buildQueryCache(EntityManagerInterface $em, $regionName = null) /** * {@inheritdoc} */ - public function buildCollectionHydrator(EntityManagerInterface $em, array $mapping) + public function buildCollectionHydrator(EntityManagerInterface $em, array $mapping): CollectionHydrator { return new DefaultCollectionHydrator($em); } - /** - * {@inheritdoc} - */ - public function buildEntityHydrator(EntityManagerInterface $em, ClassMetadata $metadata) + public function buildEntityHydrator(EntityManagerInterface $em, ClassMetadata $metadata): EntityHydrator { return new DefaultEntityHydrator($em); } @@ -168,7 +147,7 @@ public function buildEntityHydrator(EntityManagerInterface $em, ClassMetadata $m /** * {@inheritdoc} */ - public function getRegion(array $cache) + public function getRegion(array $cache): Region { if (isset($this->regions[$cache['region']])) { return $this->regions[$cache['region']]; @@ -196,10 +175,7 @@ public function getRegion(array $cache) return $this->regions[$cache['region']] = $region; } - /** - * {@inheritdoc} - */ - public function getTimestampRegion() + public function getTimestampRegion(): TimestampRegion { if ($this->timestampRegion === null) { $name = Cache::DEFAULT_TIMESTAMP_REGION_NAME; @@ -211,10 +187,7 @@ public function getTimestampRegion() return $this->timestampRegion; } - /** - * {@inheritdoc} - */ - public function createCache(EntityManagerInterface $entityManager) + public function createCache(EntityManagerInterface $entityManager): Cache { return new DefaultCache($entityManager); } diff --git a/lib/Doctrine/ORM/Cache/DefaultCollectionHydrator.php b/lib/Doctrine/ORM/Cache/DefaultCollectionHydrator.php index 08924680f1f..9ea7729a74b 100644 --- a/lib/Doctrine/ORM/Cache/DefaultCollectionHydrator.php +++ b/lib/Doctrine/ORM/Cache/DefaultCollectionHydrator.php @@ -4,6 +4,7 @@ namespace Doctrine\ORM\Cache; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Cache\Persister\CachedPersister; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; @@ -18,28 +19,18 @@ */ class DefaultCollectionHydrator implements CollectionHydrator { - /** @var EntityManagerInterface */ - private $em; - - /** @var UnitOfWork */ - private $uow; + private UnitOfWork $uow; /** @var array */ - private static $hints = [Query::HINT_CACHE_ENABLED => true]; + private static array $hints = [Query::HINT_CACHE_ENABLED => true]; - /** - * @param EntityManagerInterface $em The entity manager. - */ - public function __construct(EntityManagerInterface $em) - { - $this->em = $em; + public function __construct( + private EntityManagerInterface $em + ) { $this->uow = $em->getUnitOfWork(); } - /** - * {@inheritdoc} - */ - public function buildCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, $collection) + public function buildCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, array|Collection $collection): CollectionCacheEntry { $data = []; @@ -53,7 +44,7 @@ public function buildCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key /** * {@inheritdoc} */ - public function loadCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, CollectionCacheEntry $entry, PersistentCollection $collection) + public function loadCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, CollectionCacheEntry $entry, PersistentCollection $collection): ?array { $assoc = $metadata->associationMappings[$key->association]; $targetPersister = $this->uow->getEntityPersister($assoc['targetEntity']); diff --git a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php index 2ca00cfe10f..3eff193d9fa 100644 --- a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php +++ b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php @@ -32,34 +32,19 @@ */ class DefaultQueryCache implements QueryCache { - /** @var EntityManagerInterface */ - private $em; - - /** @var UnitOfWork */ - private $uow; - - /** @var Region */ - private $region; - - /** @var QueryCacheValidator */ - private $validator; - - /** @var CacheLogger|null */ - protected $cacheLogger; + private UnitOfWork $uow; + private QueryCacheValidator $validator; + protected ?CacheLogger $cacheLogger = null; /** @var array */ - private static $hints = [Query::HINT_CACHE_ENABLED => true]; + private static array $hints = [Query::HINT_CACHE_ENABLED => true]; - /** - * @param EntityManagerInterface $em The entity manager. - * @param Region $region The query region. - */ - public function __construct(EntityManagerInterface $em, Region $region) - { + public function __construct( + private EntityManagerInterface $em, + private Region $region + ) { $cacheConfig = $em->getConfiguration()->getSecondLevelCacheConfiguration(); - $this->em = $em; - $this->region = $region; $this->uow = $em->getUnitOfWork(); $this->cacheLogger = $cacheConfig->getCacheLogger(); $this->validator = $cacheConfig->getQueryValidator(); @@ -68,7 +53,7 @@ public function __construct(EntityManagerInterface $em, Region $region) /** * {@inheritdoc} */ - public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []) + public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []): ?array { if (! ($key->cacheMode & Cache::MODE_GET)) { return null; @@ -109,16 +94,12 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = [] $entityEntry = $entries[$index] ?? null; if (! $entityEntry instanceof EntityCacheEntry) { - if ($this->cacheLogger !== null) { - $this->cacheLogger->entityCacheMiss($regionName, $cacheKeys->identifiers[$index]); - } + $this->cacheLogger?->entityCacheMiss($regionName, $cacheKeys->identifiers[$index]); return null; } - if ($this->cacheLogger !== null) { - $this->cacheLogger->entityCacheHit($regionName, $cacheKeys->identifiers[$index]); - } + $this->cacheLogger?->entityCacheHit($regionName, $cacheKeys->identifiers[$index]); if (! $hasRelation) { $result[$index] = $this->uow->createEntity($entityEntry->class, $entityEntry->resolveAssociationEntries($this->em), self::$hints); @@ -140,9 +121,7 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = [] $assocEntry = $assocRegion->get($assocKey); if ($assocEntry === null) { - if ($this->cacheLogger !== null) { - $this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKey); - } + $this->cacheLogger?->entityCacheMiss($assocRegion->getName(), $assocKey); $this->uow->hydrationComplete(); @@ -151,9 +130,7 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = [] $data[$name] = $this->uow->createEntity($assocEntry->class, $assocEntry->resolveAssociationEntries($this->em), self::$hints); - if ($this->cacheLogger !== null) { - $this->cacheLogger->entityCacheHit($assocRegion->getName(), $assocKey); - } + $this->cacheLogger?->entityCacheHit($assocRegion->getName(), $assocKey); continue; } @@ -174,9 +151,7 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = [] $assocEntry = is_array($assocEntries) ? ($assocEntries[$assocIndex] ?? null) : null; if ($assocEntry === null) { - if ($this->cacheLogger !== null) { - $this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKeys->identifiers[$assocIndex]); - } + $this->cacheLogger?->entityCacheMiss($assocRegion->getName(), $assocKeys->identifiers[$assocIndex]); $this->uow->hydrationComplete(); @@ -187,9 +162,7 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = [] $collection->hydrateSet($assocIndex, $element); - if ($this->cacheLogger !== null) { - $this->cacheLogger->entityCacheHit($assocRegion->getName(), $assocKeys->identifiers[$assocIndex]); - } + $this->cacheLogger?->entityCacheHit($assocRegion->getName(), $assocKeys->identifiers[$assocIndex]); } $data[$name] = $collection; @@ -227,7 +200,7 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = [] /** * {@inheritdoc} */ - public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $hints = []) + public function put(QueryCacheKey $key, ResultSetMapping $rsm, mixed $result, array $hints = []): bool { if ($rsm->scalarMappings) { throw FeatureNotImplemented::scalarResults(); @@ -327,12 +300,11 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $h /** * @param array $assoc - * @param mixed $assocValue * * @return mixed[]|null * @psalm-return array{targetEntity: class-string, type: mixed, list?: array[], identifier?: array}|null */ - private function storeAssociationCache(QueryCacheKey $key, array $assoc, $assocValue): ?array + private function storeAssociationCache(QueryCacheKey $key, array $assoc, mixed $assocValue): ?array { $assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']); $assocMetadata = $assocPersister->getClassMetadata(); @@ -382,16 +354,13 @@ private function storeAssociationCache(QueryCacheKey $key, array $assoc, $assocV } /** - * @param object $entity - * - * @return mixed[]|object|null * @psalm-return list|object|null */ private function getAssociationValue( ResultSetMapping $rsm, string $assocAlias, - $entity - ) { + object $entity + ): array|object|null { $path = []; $alias = $assocAlias; @@ -412,13 +381,11 @@ private function getAssociationValue( } /** - * @param mixed $value * @psalm-param array $path * - * @return mixed[]|object|null * @psalm-return list|object|null */ - private function getAssociationPathValue($value, array $path) + private function getAssociationPathValue(mixed $value, array $path): array|object|null { $mapping = array_shift($path); $metadata = $this->em->getClassMetadata($mapping['class']); @@ -447,18 +414,12 @@ private function getAssociationPathValue($value, array $path) return $values; } - /** - * {@inheritdoc} - */ - public function clear() + public function clear(): bool { return $this->region->evictAll(); } - /** - * {@inheritdoc} - */ - public function getRegion() + public function getRegion(): Region { return $this->region; } diff --git a/lib/Doctrine/ORM/Cache/EntityCacheEntry.php b/lib/Doctrine/ORM/Cache/EntityCacheEntry.php index 6a769111c48..d8853545971 100644 --- a/lib/Doctrine/ORM/Cache/EntityCacheEntry.php +++ b/lib/Doctrine/ORM/Cache/EntityCacheEntry.php @@ -19,23 +19,21 @@ class EntityCacheEntry implements CacheEntry * @readonly Public only for performance reasons, it should be considered immutable. * @var array */ - public $data; + public array $data; /** * The entity class name * * @readonly Public only for performance reasons, it should be considered immutable. - * @var string * @psalm-var class-string */ - public $class; + public string $class; /** - * @param string $class The entity class. - * @param array $data The entity data. + * @param array $data The entity data. * @psalm-param class-string $class */ - public function __construct($class, array $data) + public function __construct(string $class, array $data) { $this->class = $class; $this->data = $data; @@ -47,10 +45,8 @@ public function __construct($class, array $data) * This method allow Doctrine\Common\Cache\PhpFileCache compatibility * * @param array $values array containing property values - * - * @return EntityCacheEntry */ - public static function __set_state(array $values) + public static function __set_state(array $values): self { return new self($values['class'], $values['data']); } @@ -60,7 +56,7 @@ public static function __set_state(array $values) * * @return array */ - public function resolveAssociationEntries(EntityManagerInterface $em) + public function resolveAssociationEntries(EntityManagerInterface $em): array { return array_map(static function ($value) use ($em) { if (! ($value instanceof AssociationCacheEntry)) { diff --git a/lib/Doctrine/ORM/Cache/EntityCacheKey.php b/lib/Doctrine/ORM/Cache/EntityCacheKey.php index 730914d6979..f6486969886 100644 --- a/lib/Doctrine/ORM/Cache/EntityCacheKey.php +++ b/lib/Doctrine/ORM/Cache/EntityCacheKey.php @@ -20,23 +20,22 @@ class EntityCacheKey extends CacheKey * @readonly Public only for performance reasons, it should be considered immutable. * @var array */ - public $identifier; + public array $identifier; /** * The entity class name * * @readonly Public only for performance reasons, it should be considered immutable. - * @var string * @psalm-var class-string */ - public $entityClass; + public string $entityClass; /** * @param string $entityClass The entity class name. In a inheritance hierarchy it should always be the root entity class. * @param array $identifier The entity identifier * @psalm-param class-string $entityClass */ - public function __construct($entityClass, array $identifier) + public function __construct(string $entityClass, array $identifier) { ksort($identifier); diff --git a/lib/Doctrine/ORM/Cache/Lock.php b/lib/Doctrine/ORM/Cache/Lock.php index a24ddb75555..928d56737ee 100644 --- a/lib/Doctrine/ORM/Cache/Lock.php +++ b/lib/Doctrine/ORM/Cache/Lock.php @@ -9,22 +9,16 @@ class Lock { - /** @var string */ - public $value; + public int $time; - /** @var int */ - public $time; - - public function __construct(string $value, ?int $time = null) - { - $this->value = $value; - $this->time = $time ?: time(); + public function __construct( + public string $value, + ?int $time = null + ) { + $this->time = $time ?? time(); } - /** - * @return Lock - */ - public static function createLockRead() + public static function createLockRead(): Lock { return new self(uniqid((string) time(), true)); } diff --git a/lib/Doctrine/ORM/Cache/Persister/Collection/AbstractCollectionPersister.php b/lib/Doctrine/ORM/Cache/Persister/Collection/AbstractCollectionPersister.php index 63d8179aa6e..96ff5112e54 100644 --- a/lib/Doctrine/ORM/Cache/Persister/Collection/AbstractCollectionPersister.php +++ b/lib/Doctrine/ORM/Cache/Persister/Collection/AbstractCollectionPersister.php @@ -7,10 +7,8 @@ use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Util\ClassUtils; -use Doctrine\Deprecations\Deprecation; use Doctrine\ORM\Cache\CollectionCacheKey; use Doctrine\ORM\Cache\CollectionHydrator; -use Doctrine\ORM\Cache\EntityCacheKey; use Doctrine\ORM\Cache\Logging\CacheLogger; use Doctrine\ORM\Cache\Persister\Entity\CachedEntityPersister; use Doctrine\ORM\Cache\Region; @@ -172,51 +170,4 @@ public function loadCriteria(PersistentCollection $collection, Criteria $criteri { return $this->persister->loadCriteria($collection, $criteria); } - - /** - * Clears cache entries related to the current collection - * - * @deprecated This method is not used anymore. - */ - protected function evictCollectionCache(PersistentCollection $collection): void - { - Deprecation::trigger( - 'doctrine/orm', - 'https://github.com/doctrine/orm/pull/9512', - 'The method %s() is deprecated and will be removed without replacement.' - ); - - $key = new CollectionCacheKey( - $this->sourceEntity->rootEntityName, - $this->association['fieldName'], - $this->uow->getEntityIdentifier($collection->getOwner()) - ); - - $this->region->evict($key); - - $this->cacheLogger?->collectionCachePut($this->regionName, $key); - } - - /** - * @deprecated This method is not used anymore. - * - * @psalm-param class-string $targetEntity - */ - protected function evictElementCache(string $targetEntity, object $element): void - { - Deprecation::trigger( - 'doctrine/orm', - 'https://github.com/doctrine/orm/pull/9512', - 'The method %s() is deprecated and will be removed without replacement.' - ); - - $targetPersister = $this->uow->getEntityPersister($targetEntity); - assert($targetPersister instanceof CachedEntityPersister); - $targetRegion = $targetPersister->getCacheRegion(); - $key = new EntityCacheKey($targetEntity, $this->uow->getEntityIdentifier($element)); - - $targetRegion->evict($key); - - $this->cacheLogger?->entityCachePut($targetRegion->getName(), $key); - } } diff --git a/lib/Doctrine/ORM/Cache/QueryCache.php b/lib/Doctrine/ORM/Cache/QueryCache.php index 890ee7a04c1..4e80a8040e4 100644 --- a/lib/Doctrine/ORM/Cache/QueryCache.php +++ b/lib/Doctrine/ORM/Cache/QueryCache.php @@ -12,28 +12,19 @@ */ interface QueryCache { - /** - * @return bool - */ - public function clear(); + public function clear(): bool; /** - * @param mixed $result * @param mixed[] $hints - * - * @return bool */ - public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $hints = []); + public function put(QueryCacheKey $key, ResultSetMapping $rsm, mixed $result, array $hints = []): bool; /** * @param mixed[] $hints * * @return mixed[]|null */ - public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []); + public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []): ?array; - /** - * @return Region - */ - public function getRegion(); + public function getRegion(): Region; } diff --git a/lib/Doctrine/ORM/Cache/QueryCacheEntry.php b/lib/Doctrine/ORM/Cache/QueryCacheEntry.php index 0bd18afc7b6..27e6e83c899 100644 --- a/lib/Doctrine/ORM/Cache/QueryCacheEntry.php +++ b/lib/Doctrine/ORM/Cache/QueryCacheEntry.php @@ -17,21 +17,19 @@ class QueryCacheEntry implements CacheEntry * @readonly Public only for performance reasons, it should be considered immutable. * @var array */ - public $result; + public array $result; /** * Time creation of this cache entry * * @readonly Public only for performance reasons, it should be considered immutable. - * @var float */ - public $time; + public float $time; /** * @param array $result - * @param float|null $time */ - public function __construct($result, $time = null) + public function __construct(array $result, ?float $time = null) { $this->result = $result; $this->time = $time ?: microtime(true); @@ -39,10 +37,8 @@ public function __construct($result, $time = null) /** * @param array $values - * - * @return QueryCacheEntry */ - public static function __set_state(array $values) + public static function __set_state(array $values): self { return new self($values['result'], $values['time']); } diff --git a/lib/Doctrine/ORM/Cache/QueryCacheKey.php b/lib/Doctrine/ORM/Cache/QueryCacheKey.php index a378c39c1c6..728ce3d5f43 100644 --- a/lib/Doctrine/ORM/Cache/QueryCacheKey.php +++ b/lib/Doctrine/ORM/Cache/QueryCacheKey.php @@ -15,24 +15,19 @@ class QueryCacheKey extends CacheKey * Cache key lifetime * * @readonly Public only for performance reasons, it should be considered immutable. - * @var int */ - public $lifetime; + public int $lifetime; /** * Cache mode * * @readonly Public only for performance reasons, it should be considered immutable. - * @var int * @psalm-var Cache::MODE_* */ - public $cacheMode; + public int $cacheMode; - /** - * @readonly Public only for performance reasons, it should be considered immutable. - * @var TimestampCacheKey|null - */ - public $timestampKey; + /** @readonly Public only for performance reasons, it should be considered immutable. */ + public ?TimestampCacheKey $timestampKey = null; /** * @psalm-param Cache::MODE_* $cacheMode diff --git a/lib/Doctrine/ORM/Cache/QueryCacheValidator.php b/lib/Doctrine/ORM/Cache/QueryCacheValidator.php index 8ef61299a5b..8a0d39f6469 100644 --- a/lib/Doctrine/ORM/Cache/QueryCacheValidator.php +++ b/lib/Doctrine/ORM/Cache/QueryCacheValidator.php @@ -11,8 +11,6 @@ interface QueryCacheValidator { /** * Checks if the query entry is valid - * - * @return bool */ - public function isValid(QueryCacheKey $key, QueryCacheEntry $entry); + public function isValid(QueryCacheKey $key, QueryCacheEntry $entry): bool; } diff --git a/lib/Doctrine/ORM/Cache/Region/DefaultRegion.php b/lib/Doctrine/ORM/Cache/Region/DefaultRegion.php index b4706714949..baca09481a2 100644 --- a/lib/Doctrine/ORM/Cache/Region/DefaultRegion.php +++ b/lib/Doctrine/ORM/Cache/Region/DefaultRegion.php @@ -26,15 +26,11 @@ class DefaultRegion implements Region private const REGION_KEY_SEPARATOR = '_'; private const REGION_PREFIX = 'DC2_REGION_'; - private string $name; - private int $lifetime = 0; - private CacheItemPoolInterface $cacheItemPool; - - public function __construct(string $name, CacheItemPoolInterface $cacheItemPool, int $lifetime = 0) - { - $this->cacheItemPool = $cacheItemPool; - $this->name = $name; - $this->lifetime = $lifetime; + public function __construct( + private string $name, + private CacheItemPoolInterface $cacheItemPool, + private int $lifetime = 0 + ) { } public function getName(): string diff --git a/lib/Doctrine/ORM/Cache/Region/FileLockRegion.php b/lib/Doctrine/ORM/Cache/Region/FileLockRegion.php index 3171e03d88a..b9264796036 100644 --- a/lib/Doctrine/ORM/Cache/Region/FileLockRegion.php +++ b/lib/Doctrine/ORM/Cache/Region/FileLockRegion.php @@ -92,18 +92,12 @@ private function getLockFileName(CacheKey $key): string return $this->directory . DIRECTORY_SEPARATOR . $key->hash . '.' . self::LOCK_EXTENSION; } - /** - * @return string|false - */ - private function getLockContent(string $filename) + private function getLockContent(string $filename): string|false { return @file_get_contents($filename); } - /** - * @return int|false - */ - private function getLockTime(string $filename) + private function getLockTime(string $filename): int|false { return @fileatime($filename); } diff --git a/lib/Doctrine/ORM/Cache/RegionsConfiguration.php b/lib/Doctrine/ORM/Cache/RegionsConfiguration.php index b566e51f5b0..a7691f04ef5 100644 --- a/lib/Doctrine/ORM/Cache/RegionsConfiguration.php +++ b/lib/Doctrine/ORM/Cache/RegionsConfiguration.php @@ -10,102 +10,54 @@ class RegionsConfiguration { /** @var array */ - private $lifetimes = []; + private array $lifetimes = []; /** @var array */ - private $lockLifetimes = []; + private array $lockLifetimes = []; - /** @var int */ - private $defaultLifetime; - - /** @var int */ - private $defaultLockLifetime; - - /** - * @param int $defaultLifetime - * @param int $defaultLockLifetime - */ - public function __construct($defaultLifetime = 3600, $defaultLockLifetime = 60) - { - $this->defaultLifetime = (int) $defaultLifetime; - $this->defaultLockLifetime = (int) $defaultLockLifetime; + public function __construct( + private int $defaultLifetime = 3600, + private int $defaultLockLifetime = 60 + ) { } - /** - * @return int - */ - public function getDefaultLifetime() + public function getDefaultLifetime(): int { return $this->defaultLifetime; } - /** - * @param int $defaultLifetime - * - * @return void - */ - public function setDefaultLifetime($defaultLifetime) + public function setDefaultLifetime(int $defaultLifetime): void { - $this->defaultLifetime = (int) $defaultLifetime; + $this->defaultLifetime = $defaultLifetime; } - /** - * @return int - */ - public function getDefaultLockLifetime() + public function getDefaultLockLifetime(): int { return $this->defaultLockLifetime; } - /** - * @param int $defaultLockLifetime - * - * @return void - */ - public function setDefaultLockLifetime($defaultLockLifetime) + public function setDefaultLockLifetime(int $defaultLockLifetime): void { - $this->defaultLockLifetime = (int) $defaultLockLifetime; + $this->defaultLockLifetime = $defaultLockLifetime; } - /** - * @param string $regionName - * - * @return int - */ - public function getLifetime($regionName) + public function getLifetime(string $regionName): int { return $this->lifetimes[$regionName] ?? $this->defaultLifetime; } - /** - * @param string $name - * @param int $lifetime - * - * @return void - */ - public function setLifetime($name, $lifetime) + public function setLifetime(string $name, int $lifetime): void { - $this->lifetimes[$name] = (int) $lifetime; + $this->lifetimes[$name] = $lifetime; } - /** - * @param string $regionName - * - * @return int - */ - public function getLockLifetime($regionName) + public function getLockLifetime(string $regionName): int { return $this->lockLifetimes[$regionName] ?? $this->defaultLockLifetime; } - /** - * @param string $name - * @param int $lifetime - * - * @return void - */ - public function setLockLifetime($name, $lifetime) + public function setLockLifetime(string $name, int $lifetime): void { - $this->lockLifetimes[$name] = (int) $lifetime; + $this->lockLifetimes[$name] = $lifetime; } } diff --git a/lib/Doctrine/ORM/Cache/TimestampCacheEntry.php b/lib/Doctrine/ORM/Cache/TimestampCacheEntry.php index cf67dd82248..f946de46ff2 100644 --- a/lib/Doctrine/ORM/Cache/TimestampCacheEntry.php +++ b/lib/Doctrine/ORM/Cache/TimestampCacheEntry.php @@ -11,18 +11,12 @@ */ class TimestampCacheEntry implements CacheEntry { - /** - * @readonly Public only for performance reasons, it should be considered immutable. - * @var float - */ - public $time; + /** @readonly Public only for performance reasons, it should be considered immutable. */ + public float $time; - /** - * @param float|null $time - */ - public function __construct($time = null) + public function __construct(?float $time = null) { - $this->time = $time ? (float) $time : microtime(true); + $this->time = $time ?? microtime(true); } /** @@ -31,10 +25,8 @@ public function __construct($time = null) * This method allow Doctrine\Common\Cache\PhpFileCache compatibility * * @param array $values array containing property values - * - * @return TimestampCacheEntry */ - public static function __set_state(array $values) + public static function __set_state(array $values): TimestampCacheEntry { return new self($values['time']); } diff --git a/lib/Doctrine/ORM/Cache/TimestampCacheKey.php b/lib/Doctrine/ORM/Cache/TimestampCacheKey.php index fb8b44aa01f..7142b1a8512 100644 --- a/lib/Doctrine/ORM/Cache/TimestampCacheKey.php +++ b/lib/Doctrine/ORM/Cache/TimestampCacheKey.php @@ -12,8 +12,8 @@ class TimestampCacheKey extends CacheKey /** * @param string $space Result cache id */ - public function __construct($space) + public function __construct(string $space) { - $this->hash = (string) $space; + $this->hash = $space; } } diff --git a/lib/Doctrine/ORM/Cache/TimestampQueryCacheValidator.php b/lib/Doctrine/ORM/Cache/TimestampQueryCacheValidator.php index 133a17056fb..9ff50f86248 100644 --- a/lib/Doctrine/ORM/Cache/TimestampQueryCacheValidator.php +++ b/lib/Doctrine/ORM/Cache/TimestampQueryCacheValidator.php @@ -8,18 +8,14 @@ class TimestampQueryCacheValidator implements QueryCacheValidator { - /** @var TimestampRegion */ - private $timestampRegion; + private TimestampRegion $timestampRegion; public function __construct(TimestampRegion $timestampRegion) { $this->timestampRegion = $timestampRegion; } - /** - * {@inheritdoc} - */ - public function isValid(QueryCacheKey $key, QueryCacheEntry $entry) + public function isValid(QueryCacheKey $key, QueryCacheEntry $entry): bool { if ($this->regionUpdated($key, $entry)) { return false; diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 9e222a114ec..4eb1de50abd 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -31,12 +31,6 @@ getTimestampRegion - - - (string) $association - (string) $entityClass - - ! $association['type'] @@ -53,9 +47,6 @@ $this->fileLockRegionDirectory - - (string) $fileLockRegionDirectory - @@ -101,8 +92,7 @@ $entry->identifiers - - $collection->getOwner() + $collection->getOwner() @@ -173,26 +163,6 @@ lock - - - (int) $defaultLifetime - (int) $defaultLifetime - (int) $defaultLockLifetime - (int) $defaultLockLifetime - (int) $lifetime - (int) $lifetime - - - - - (float) $time - - - - - (string) $space - - $timestamp->time diff --git a/tests/Doctrine/Tests/ORM/Cache/AbstractRegionTest.php b/tests/Doctrine/Tests/ORM/Cache/AbstractRegionTest.php index 140a4910ebb..5648b3b70a6 100644 --- a/tests/Doctrine/Tests/ORM/Cache/AbstractRegionTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/AbstractRegionTest.php @@ -19,14 +19,9 @@ */ abstract class AbstractRegionTest extends OrmFunctionalTestCase { - /** - * @var Region - * @psalm-var TRegion - */ - protected $region; - - /** @var CacheItemPoolInterface */ - protected $cacheItemPool; + /** @psalm-var TRegion */ + protected Region $region; + protected CacheItemPoolInterface $cacheItemPool; protected function setUp(): void { diff --git a/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php b/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php index 041da75629a..8621e9349ce 100644 --- a/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php @@ -18,8 +18,7 @@ */ class CacheConfigTest extends DoctrineTestCase { - /** @var CacheConfiguration */ - private $config; + private CacheConfiguration $config; protected function setUp(): void { diff --git a/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php b/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php index d918aeaecd3..7437aab925d 100644 --- a/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/DefaultCacheFactoryTest.php @@ -37,13 +37,10 @@ class DefaultCacheFactoryTest extends OrmTestCase { /** @var CacheFactory&MockObject */ - private $factory; + private CacheFactory $factory; - /** @var EntityManagerMock */ - private $em; - - /** @var RegionsConfiguration */ - private $regionsConfig; + private EntityManagerMock $em; + private RegionsConfiguration $regionsConfig; protected function setUp(): void { @@ -54,7 +51,7 @@ protected function setUp(): void $this->regionsConfig = new RegionsConfiguration(); $arguments = [$this->regionsConfig, $this->getSharedSecondLevelCache()]; $this->factory = $this->getMockBuilder(DefaultCacheFactory::class) - ->setMethods(['getRegion']) + ->onlyMethods(['getRegion']) ->setConstructorArgs($arguments) ->getMock(); } diff --git a/tests/Doctrine/Tests/ORM/Cache/DefaultCacheTest.php b/tests/Doctrine/Tests/ORM/Cache/DefaultCacheTest.php index 2c0a7cc9199..4c6c6d6f07f 100644 --- a/tests/Doctrine/Tests/ORM/Cache/DefaultCacheTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/DefaultCacheTest.php @@ -25,11 +25,8 @@ */ class DefaultCacheTest extends OrmTestCase { - /** @var DefaultCache */ - private $cache; - - /** @var EntityManagerMock */ - private $em; + private DefaultCache $cache; + private EntityManagerMock $em; protected function setUp(): void { diff --git a/tests/Doctrine/Tests/ORM/Cache/DefaultCollectionHydratorTest.php b/tests/Doctrine/Tests/ORM/Cache/DefaultCollectionHydratorTest.php index ca188471a99..b134ba1568b 100644 --- a/tests/Doctrine/Tests/ORM/Cache/DefaultCollectionHydratorTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/DefaultCollectionHydratorTest.php @@ -21,8 +21,7 @@ */ class DefaultCollectionHydratorTest extends OrmFunctionalTestCase { - /** @var DefaultCollectionHydrator */ - private $structure; + private DefaultCollectionHydrator $structure; protected function setUp(): void { diff --git a/tests/Doctrine/Tests/ORM/Cache/DefaultQueryCacheTest.php b/tests/Doctrine/Tests/ORM/Cache/DefaultQueryCacheTest.php index eddbfd114e6..9025852a0bc 100644 --- a/tests/Doctrine/Tests/ORM/Cache/DefaultQueryCacheTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/DefaultQueryCacheTest.php @@ -35,14 +35,9 @@ */ class DefaultQueryCacheTest extends OrmTestCase { - /** @var DefaultQueryCache */ - private $queryCache; - - /** @var EntityManagerMock */ - private $em; - - /** @var CacheRegionMock */ - private $region; + private DefaultQueryCache $queryCache; + private EntityManagerMock $em; + private CacheRegionMock $region; protected function setUp(): void { @@ -678,19 +673,13 @@ public function testNotCacheableEntityException(): void class CacheFactoryDefaultQueryCacheTest extends Cache\DefaultCacheFactory { - /** @var DefaultQueryCache */ - private $queryCache; - - /** @var CacheRegionMock */ - private $region; - - public function __construct(DefaultQueryCache $queryCache, CacheRegionMock $region) - { - $this->queryCache = $queryCache; - $this->region = $region; + public function __construct( + private DefaultQueryCache $queryCache, + private CacheRegionMock $region + ) { } - public function buildQueryCache(EntityManagerInterface $em, $regionName = null): DefaultQueryCache + public function buildQueryCache(EntityManagerInterface $em, ?string $regionName = null): DefaultQueryCache { return $this->queryCache; } diff --git a/tests/Doctrine/Tests/ORM/Cache/DefaultRegionTest.php b/tests/Doctrine/Tests/ORM/Cache/DefaultRegionTest.php index 01f2d2212e8..d8a3eefd838 100644 --- a/tests/Doctrine/Tests/ORM/Cache/DefaultRegionTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/DefaultRegionTest.php @@ -5,7 +5,6 @@ namespace Doctrine\Tests\ORM\Cache; use Doctrine\ORM\Cache\CollectionCacheEntry; -use Doctrine\ORM\Cache\Region; use Doctrine\ORM\Cache\Region\DefaultRegion; use Doctrine\Tests\Mocks\CacheEntryMock; use Doctrine\Tests\Mocks\CacheKeyMock; @@ -18,7 +17,7 @@ */ class DefaultRegionTest extends AbstractRegionTest { - protected function createRegion(): Region + protected function createRegion(): DefaultRegion { return new DefaultRegion('default.region.test', $this->cacheItemPool); } diff --git a/tests/Doctrine/Tests/ORM/Cache/FileLockRegionTest.php b/tests/Doctrine/Tests/ORM/Cache/FileLockRegionTest.php index 944b03ff1df..ede2b193f33 100644 --- a/tests/Doctrine/Tests/ORM/Cache/FileLockRegionTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/FileLockRegionTest.php @@ -6,7 +6,6 @@ use Doctrine\ORM\Cache\CacheKey; use Doctrine\ORM\Cache\Lock; -use Doctrine\ORM\Cache\Region; use Doctrine\ORM\Cache\Region\DefaultRegion; use Doctrine\ORM\Cache\Region\FileLockRegion; use Doctrine\Tests\Mocks\CacheEntryMock; @@ -31,11 +30,11 @@ /** * @extends AbstractRegionTest * @group DDC-2183 + * @extends AbstractRegionTest */ class FileLockRegionTest extends AbstractRegionTest { - /** @var string */ - protected $directory; + protected string $directory; public function tearDown(): void { @@ -51,7 +50,7 @@ private function getFileName(FileLockRegion $region, CacheKey $key): string return $reflection->invoke($region, $key); } - protected function createRegion(): Region + protected function createRegion(): FileLockRegion { $this->directory = sys_get_temp_dir() . '/doctrine_lock_' . uniqid(); diff --git a/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheAbstractTest.php b/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheAbstractTest.php index 5091e1b4bf1..3ef5a3c2064 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheAbstractTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheAbstractTest.php @@ -29,37 +29,36 @@ abstract class SecondLevelCacheAbstractTest extends OrmFunctionalTestCase { /** @psalm-var list */ - protected $people = []; + protected array $people = []; /** @psalm-var list
*/ - protected $addresses = []; + protected array $addresses = []; /** @psalm-var list */ - protected $countries = []; + protected array $countries = []; /** @psalm-var list */ - protected $states = []; + protected array $states = []; /** @psalm-var list */ - protected $cities = []; + protected array $cities = []; /** @psalm-var list */ - protected $travels = []; + protected array $travels = []; /** @psalm-var list */ - protected $travelers = []; + protected array $travelers = []; /** @psalm-var list */ - protected $attractions = []; + protected array $attractions = []; /** @psalm-var list */ - protected $attractionsInfo = []; + protected array $attractionsInfo = []; /** @psalm-var list */ - protected $travelersWithProfile = []; + protected array $travelersWithProfile = []; - /** @var Cache */ - protected $cache; + protected Cache $cache; protected function setUp(): void { diff --git a/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheConcurrentTest.php b/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheConcurrentTest.php index 4ddfd206b21..101bbfb7f42 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheConcurrentTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheConcurrentTest.php @@ -23,11 +23,8 @@ */ class SecondLevelCacheConcurrentTest extends SecondLevelCacheAbstractTest { - /** @var CacheFactorySecondLevelCacheConcurrentTest */ - private $cacheFactory; - - /** @var ClassMetadata */ - private $countryMetadata; + private CacheFactorySecondLevelCacheConcurrentTest $cacheFactory; + private ClassMetadata $countryMetadata; protected function setUp(): void {