From bd794db75fa685123adaf714a27e85ae33691a19 Mon Sep 17 00:00:00 2001 From: Jan Skrasek Date: Sat, 12 Oct 2024 00:15:44 +0200 Subject: [PATCH] more explicit PHP type definitions for properties --- src/Bridges/NetteDI/DIRepositoryFinder.php | 7 +-- src/Bridges/NetteDI/DependencyProvider.php | 9 ++-- .../NetteDI/PhpDocRepositoryFinder.php | 19 ++------ src/Bridges/NetteDI/RepositoryLoader.php | 14 ++---- src/Collection/ArrayCollection.php | 35 ++++++-------- src/Collection/DbalCollection.php | 47 ++++++------------- src/Collection/EmptyCollection.php | 3 +- src/Collection/EntityIterator.php | 7 ++- .../Expression/ExpressionContext.php | 4 +- src/Collection/HasManyCollection.php | 12 ++--- src/Collection/MultiEntityIterator.php | 15 +++--- src/Entity/AbstractEntity.php | 4 +- src/Entity/Embeddable/Embeddable.php | 3 +- src/Entity/Embeddable/EmbeddableContainer.php | 25 ++++------ src/Entity/Entity.php | 3 +- src/Entity/ImmutableDataTrait.php | 6 +-- src/Entity/ImmutableValuePropertyWrapper.php | 8 ++-- src/Entity/Reflection/EntityMetadata.php | 13 ++--- src/Entity/Reflection/MetadataParser.php | 4 +- .../PropertyRelationshipMetadata.php | 23 ++++----- src/Entity/ToArrayConverter.php | 2 +- src/Mapper/Dbal/Conventions/Conventions.php | 10 ++-- .../Inflector/CamelCaseInflector.php | 2 +- .../Inflector/SnakeCaseInflector.php | 2 +- src/Mapper/Dbal/DbalMapper.php | 27 ++++------- src/Mapper/Dbal/DbalMapperCoordinator.php | 11 ++--- .../Dbal/RelationshipMapperManyHasMany.php | 42 +++++------------ .../Dbal/RelationshipMapperManyHasOne.php | 20 +++----- .../Dbal/RelationshipMapperOneHasMany.php | 29 ++++-------- src/Mapper/Memory/ArrayMapper.php | 11 ++--- src/Mapper/Memory/Conventions/Conventions.php | 9 ++-- .../Memory/RelationshipMapperManyHasMany.php | 12 ++--- .../Memory/RelationshipMapperManyHasOne.php | 8 +--- .../Memory/RelationshipMapperOneHasMany.php | 12 ++--- src/Model/MetadataStorage.php | 2 +- src/Model/Model.php | 37 ++++----------- src/Model/SimpleModelFactory.php | 19 ++------ src/Model/SimpleRepositoryLoader.php | 2 +- src/Relationships/HasMany.php | 6 +-- src/Relationships/HasOne.php | 6 +-- src/Repository/IdentityMap.php | 14 +++--- src/Repository/PersistenceHelper.php | 4 +- src/Repository/Repository.php | 46 ++++++++---------- src/TestHelper/EntityCreator.php | 9 ++-- src/TestHelper/TestMapper.php | 4 +- 45 files changed, 216 insertions(+), 391 deletions(-) diff --git a/src/Bridges/NetteDI/DIRepositoryFinder.php b/src/Bridges/NetteDI/DIRepositoryFinder.php index 045bdf8f..035cd463 100644 --- a/src/Bridges/NetteDI/DIRepositoryFinder.php +++ b/src/Bridges/NetteDI/DIRepositoryFinder.php @@ -13,11 +13,8 @@ class DIRepositoryFinder implements IRepositoryFinder { - /** @var ContainerBuilder */ - private $builder; - - /** @var OrmExtension */ - private $extension; + private ContainerBuilder $builder; + private OrmExtension $extension; // @phpstan-ignore-next-line https://github.com/phpstan/phpstan/issues/587 diff --git a/src/Bridges/NetteDI/DependencyProvider.php b/src/Bridges/NetteDI/DependencyProvider.php index ab4a7618..8ec6c4b0 100644 --- a/src/Bridges/NetteDI/DependencyProvider.php +++ b/src/Bridges/NetteDI/DependencyProvider.php @@ -10,13 +10,10 @@ class DependencyProvider implements IDependencyProvider { - /** @var Container */ - private $container; - - - public function __construct(Container $container) + public function __construct( + private readonly Container $container, + ) { - $this->container = $container; } diff --git a/src/Bridges/NetteDI/PhpDocRepositoryFinder.php b/src/Bridges/NetteDI/PhpDocRepositoryFinder.php index e18fdffc..64786259 100644 --- a/src/Bridges/NetteDI/PhpDocRepositoryFinder.php +++ b/src/Bridges/NetteDI/PhpDocRepositoryFinder.php @@ -16,21 +16,12 @@ class PhpDocRepositoryFinder implements IRepositoryFinder { - /** @var class-string */ - protected $modelClass; - - /** @var ContainerBuilder */ - protected $builder; - - /** @var OrmExtension */ - protected $extension; - - - public function __construct(string $modelClass, ContainerBuilder $containerBuilder, OrmExtension $extension) + public function __construct( + protected readonly string $modelClass, + protected readonly ContainerBuilder $builder, + protected readonly OrmExtension $extension, + ) { - $this->modelClass = $modelClass; - $this->builder = $containerBuilder; - $this->extension = $extension; } diff --git a/src/Bridges/NetteDI/RepositoryLoader.php b/src/Bridges/NetteDI/RepositoryLoader.php index 1d6a9a8e..2a9fafa9 100644 --- a/src/Bridges/NetteDI/RepositoryLoader.php +++ b/src/Bridges/NetteDI/RepositoryLoader.php @@ -11,20 +11,14 @@ class RepositoryLoader implements IRepositoryLoader { - /** @var Container */ - private $container; - - /** @var array>, string> */ - private $repositoryNamesMap; - - /** * @param array>, string> $repositoryNamesMap */ - public function __construct(Container $container, array $repositoryNamesMap) + public function __construct( + private readonly Container $container, + private readonly array $repositoryNamesMap, + ) { - $this->container = $container; - $this->repositoryNamesMap = $repositoryNamesMap; } diff --git a/src/Collection/ArrayCollection.php b/src/Collection/ArrayCollection.php index 4abb1e89..b5ab777d 100644 --- a/src/Collection/ArrayCollection.php +++ b/src/Collection/ArrayCollection.php @@ -27,50 +27,43 @@ class ArrayCollection implements ICollection, MemoryCollection { /** @var list $entities): void> */ - public $onEntityFetch = []; + public array $onEntityFetch = []; /** @var list */ - protected $data; + protected array $data; - /** @var IRelationshipMapper|null */ - protected $relationshipMapper; - - /** @var IEntity|null */ - protected $relationshipParent; + protected IRelationshipMapper|null $relationshipMapper = null; + protected IEntity|null $relationshipParent = null; /** @var Iterator|null */ - protected $fetchIterator; - - /** @var IRepository */ - protected $repository; + protected Iterator|null $fetchIterator = null; - /** @var ArrayCollectionHelper */ - protected $helper; + protected ArrayCollectionHelper|null $helper = null; /** @var array */ - protected $collectionFilter = []; + protected array $collectionFilter = []; /** @var list */ - protected $collectionSorter = []; + protected array $collectionSorter = []; /** @var null|array{int, int|null} */ - protected $collectionLimit; - - /** @var bool */ - protected $entityFetchEventTriggered = false; + protected ?array $collectionLimit = null; + protected bool $entityFetchEventTriggered = false; /** * @param list $entities * @param IRepository $repository */ - public function __construct(array $entities, IRepository $repository) + public function __construct( + array $entities, + protected readonly IRepository $repository, + ) { if (!Arrays::isList($entities)) { throw new InvalidArgumentException('Entities has to be passed as a list.'); } $this->data = $entities; - $this->repository = $repository; } diff --git a/src/Collection/DbalCollection.php b/src/Collection/DbalCollection.php index 3e06cbf3..72e6a500 100644 --- a/src/Collection/DbalCollection.php +++ b/src/Collection/DbalCollection.php @@ -28,53 +28,36 @@ class DbalCollection implements ICollection { /** @var list $entities): void> */ - public $onEntityFetch = []; + public array $onEntityFetch = []; - /** @var IRelationshipMapper|null */ - protected $relationshipMapper; - - /** @var IEntity|null */ - protected $relationshipParent; + protected IRelationshipMapper|null $relationshipMapper = null; + protected IEntity|null $relationshipParent = null; /** @var Iterator|null */ - protected $fetchIterator; - - /** @var DbalMapper */ - protected $mapper; - - /** @var IConnection */ - protected $connection; - - /** @var QueryBuilder */ - protected $queryBuilder; + protected Iterator|null $fetchIterator = null; /** @var array FindBy expressions for deferred processing */ - protected $filtering = []; + protected array $filtering = []; /** @var array OrderBy expression result & sorting direction */ - protected $ordering = []; - - /** @var DbalQueryBuilderHelper */ - protected $helper; + protected array $ordering = []; + protected DbalQueryBuilderHelper|null $helper = null; /** @var list|null */ - protected $result; - - /** @var int|null */ - protected $resultCount; - - /** @var bool */ - protected $entityFetchEventTriggered = false; + protected ?array $result = null; + protected ?int $resultCount = null; + protected bool $entityFetchEventTriggered = false; /** * @param DbalMapper $mapper */ - public function __construct(DbalMapper $mapper, IConnection $connection, QueryBuilder $queryBuilder) + public function __construct( + protected readonly DbalMapper $mapper, + protected readonly IConnection $connection, + protected QueryBuilder $queryBuilder, + ) { - $this->mapper = $mapper; - $this->connection = $connection; - $this->queryBuilder = $queryBuilder; } diff --git a/src/Collection/EmptyCollection.php b/src/Collection/EmptyCollection.php index d0e8f4c4..42a7a057 100644 --- a/src/Collection/EmptyCollection.php +++ b/src/Collection/EmptyCollection.php @@ -17,8 +17,7 @@ */ final class EmptyCollection implements ICollection, MemoryCollection { - /** @var IRelationshipMapper|null */ - private $relationshipMapper; + private ?IRelationshipMapper $relationshipMapper = null; public function getBy(array $conds): ?IEntity diff --git a/src/Collection/EntityIterator.php b/src/Collection/EntityIterator.php index f81dcf71..4c2ee693 100644 --- a/src/Collection/EntityIterator.php +++ b/src/Collection/EntityIterator.php @@ -16,14 +16,13 @@ */ class EntityIterator implements IEntityPreloadContainer, Iterator, Countable { - /** @var int */ - private $position = 0; + private int $position = 0; /** @var list */ - private $iteratable; + private array $iteratable; /** @var array> */ - private $preloadCache; + private array $preloadCache = []; /** diff --git a/src/Collection/Expression/ExpressionContext.php b/src/Collection/Expression/ExpressionContext.php index 5e1c0368..9cd78d3a 100644 --- a/src/Collection/Expression/ExpressionContext.php +++ b/src/Collection/Expression/ExpressionContext.php @@ -6,7 +6,9 @@ /** * @internal * - * Determines if the expression is processed for AND subtree, OR subtree or pure expression, e.g., a sorting expression. + * Determines if the expression is processed for AND subtree, OR subtree or as a pure expression, + * e.g., a sorting expression. + * * OR subtree requires more complex SQL construction, in other modes we may optimize the resulting SQL. */ enum ExpressionContext diff --git a/src/Collection/HasManyCollection.php b/src/Collection/HasManyCollection.php index 3e1e0d53..06302c0e 100644 --- a/src/Collection/HasManyCollection.php +++ b/src/Collection/HasManyCollection.php @@ -27,20 +27,17 @@ class HasManyCollection implements ICollection /** @var array):void> */ public $onEntityFetch = []; - /** @var IRepository */ - private $repository; - /** @var ICollection */ - private $storageCollection; + private ICollection $storageCollection; /** @var MutableArrayCollection */ - private $inMemoryCollection; + private MutableArrayCollection $inMemoryCollection; /** @var callable(): array{array, array} */ private $diffCallback; /** @var Iterator|null */ - private $fetchIterator; + private ?Iterator $fetchIterator = null; /** @@ -49,12 +46,11 @@ class HasManyCollection implements ICollection * @param callable():array{array, array} $diffCallback */ public function __construct( - IRepository $repository, + private readonly IRepository $repository, ICollection $innerCollection, callable $diffCallback, ) { - $this->repository = $repository; $this->storageCollection = $innerCollection; $this->diffCallback = $diffCallback; $this->inMemoryCollection = new MutableArrayCollection([], $repository); // @phpstan-ignore-line diff --git a/src/Collection/MultiEntityIterator.php b/src/Collection/MultiEntityIterator.php index b04b3434..b1c6b241 100644 --- a/src/Collection/MultiEntityIterator.php +++ b/src/Collection/MultiEntityIterator.php @@ -16,25 +16,22 @@ */ class MultiEntityIterator implements IEntityPreloadContainer, Iterator, Countable { - /** @var int */ - private $position = 0; - - /** @var array> */ - private $data; + private int $position = 0; /** @var list */ - private $iterable; + private array $iterable; /** @var array> */ - private $preloadCache; + private array $preloadCache; /** * @param array> $data */ - public function __construct(array $data) + public function __construct( + private array $data, + ) { - $this->data = $data; } diff --git a/src/Entity/AbstractEntity.php b/src/Entity/AbstractEntity.php index 01bdcf2b..ddcdbb96 100644 --- a/src/Entity/AbstractEntity.php +++ b/src/Entity/AbstractEntity.php @@ -23,10 +23,10 @@ abstract class AbstractEntity implements IEntity /** @var IRepository|null */ - private $repository; + private IRepository|null $repository = null; /** @var array */ - private $modified = []; + private array $modified = []; /** @var mixed */ private $persistedId = null; diff --git a/src/Entity/Embeddable/Embeddable.php b/src/Entity/Embeddable/Embeddable.php index 270cf7b1..3423f2ee 100644 --- a/src/Entity/Embeddable/Embeddable.php +++ b/src/Entity/Embeddable/Embeddable.php @@ -24,8 +24,7 @@ abstract class Embeddable implements IEmbeddable use ImmutableDataTrait; - /** @var IEntity|null */ - protected $parentEntity; + protected IEntity|null $parentEntity = null; /** diff --git a/src/Entity/Embeddable/EmbeddableContainer.php b/src/Entity/Embeddable/EmbeddableContainer.php index d6d4a543..57303f55 100644 --- a/src/Entity/Embeddable/EmbeddableContainer.php +++ b/src/Entity/Embeddable/EmbeddableContainer.php @@ -25,27 +25,20 @@ class EmbeddableContainer implements IPropertyContainer, IEntityAwareProperty use SmartObject; - /** @var PropertyMetadata */ - private $metadata; - - /** @var IEntity|null */ - private $entity; - - /** @var IEmbeddable|null */ - private $value; + private IEntity|null $entity = null; + private IEmbeddable|null $value = null; /** @var array */ - private $propertiesMetadata = []; - - /** @var string */ - private $instanceType; + private array $propertiesMetadata = []; + private string $instanceType; - public function __construct(PropertyMetadata $propertyMetadata) + public function __construct( + private readonly PropertyMetadata $metadata, + ) { - assert($propertyMetadata->args !== null); - $this->metadata = $propertyMetadata; - $this->instanceType = $propertyMetadata->args[EmbeddableContainer::class]['class']; + assert($metadata->args !== null); + $this->instanceType = $metadata->args[EmbeddableContainer::class]['class']; $this->propertiesMetadata = MetadataStorage::get($this->instanceType)->getProperties(); } diff --git a/src/Entity/Entity.php b/src/Entity/Entity.php index 53d85f48..a35f03b2 100644 --- a/src/Entity/Entity.php +++ b/src/Entity/Entity.php @@ -9,8 +9,7 @@ class Entity extends AbstractEntity implements IEntityHasPreloadContainer { - /** @var IEntityPreloadContainer|null */ - private $preloadContainer; + private IEntityPreloadContainer|null $preloadContainer = null; /** diff --git a/src/Entity/ImmutableDataTrait.php b/src/Entity/ImmutableDataTrait.php index 9ec400ad..fd6eb417 100644 --- a/src/Entity/ImmutableDataTrait.php +++ b/src/Entity/ImmutableDataTrait.php @@ -13,13 +13,13 @@ trait ImmutableDataTrait { /** @var EntityMetadata */ - protected $metadata; + protected EntityMetadata $metadata; /** @var array */ - private $data = []; + private array $data = []; /** @var array */ - private $validated = []; + private array $validated = []; public function &getValue(string $name) diff --git a/src/Entity/ImmutableValuePropertyWrapper.php b/src/Entity/ImmutableValuePropertyWrapper.php index a31142d8..4560b0be 100644 --- a/src/Entity/ImmutableValuePropertyWrapper.php +++ b/src/Entity/ImmutableValuePropertyWrapper.php @@ -11,13 +11,11 @@ abstract class ImmutableValuePropertyWrapper implements IPropertyContainer /** @var mixed */ protected $value; - /** @var PropertyMetadata */ - protected $propertyMetadata; - - public function __construct(PropertyMetadata $propertyMetadata) + public function __construct( + protected readonly PropertyMetadata $propertyMetadata, + ) { - $this->propertyMetadata = $propertyMetadata; } diff --git a/src/Entity/Reflection/EntityMetadata.php b/src/Entity/Reflection/EntityMetadata.php index 1605f86a..0c0abce4 100644 --- a/src/Entity/Reflection/EntityMetadata.php +++ b/src/Entity/Reflection/EntityMetadata.php @@ -15,23 +15,20 @@ class EntityMetadata { use SmartObject; - - /** @var class-string */ - private $className; - /** @var list */ - private $primaryKey = []; + private array $primaryKey = []; /** @var array */ - private $properties = []; + private array $properties = []; /** * @param class-string $className */ - public function __construct(string $className) + public function __construct( + private readonly string $className, + ) { - $this->className = $className; } diff --git a/src/Entity/Reflection/MetadataParser.php b/src/Entity/Reflection/MetadataParser.php index 2b920ffc..4c00eeea 100644 --- a/src/Entity/Reflection/MetadataParser.php +++ b/src/Entity/Reflection/MetadataParser.php @@ -1,5 +1,7 @@ */ - protected $modifiers = [ + protected array $modifiers = [ '1:1' => 'parseOneHasOneModifier', '1:m' => 'parseOneHasManyModifier', 'm:1' => 'parseManyHasOneModifier', diff --git a/src/Entity/Reflection/PropertyRelationshipMetadata.php b/src/Entity/Reflection/PropertyRelationshipMetadata.php index db51275d..4eb81812 100644 --- a/src/Entity/Reflection/PropertyRelationshipMetadata.php +++ b/src/Entity/Reflection/PropertyRelationshipMetadata.php @@ -15,26 +15,19 @@ class PropertyRelationshipMetadata public const MANY_HAS_MANY = 4; /** @var class-string> */ - public $repository; + public string $repository; /** @var class-string */ - public $entity; + public string $entity; - /** @var EntityMetadata */ - public $entityMetadata; - - /** @var string|null */ - public $property; - - /** @var bool */ - public $isMain = false; - - /** @var int */ - public $type; + public EntityMetadata $entityMetadata; + public string|null $property; + public bool $isMain = false; + public int $type; /** @var array|null */ - public $order; + public ?array $order = null; /** @var bool[] */ - public $cascade; + public array $cascade = []; } diff --git a/src/Entity/ToArrayConverter.php b/src/Entity/ToArrayConverter.php index b1ab5768..d112f688 100644 --- a/src/Entity/ToArrayConverter.php +++ b/src/Entity/ToArrayConverter.php @@ -40,7 +40,7 @@ class ToArrayConverter public const RELATIONSHIP_AS_ARRAY = 3; /** @var int Maximum recursion level. */ - public static $maxRecursionLevel = 3; + public static int $maxRecursionLevel = 3; /** diff --git a/src/Mapper/Dbal/Conventions/Conventions.php b/src/Mapper/Dbal/Conventions/Conventions.php index 2223e966..c7f30a95 100644 --- a/src/Mapper/Dbal/Conventions/Conventions.php +++ b/src/Mapper/Dbal/Conventions/Conventions.php @@ -41,11 +41,9 @@ class Conventions implements IConventions public string $manyHasManyStorageNamePattern = '%s_x_%s'; public string $embeddableSeparatorPattern = '_'; - protected IInflector $inflector; protected CachedPlatform $platform; - protected Table $storageTable; - protected EntityMetadata $entityMetadata; protected bool $storageNameWithSchema; + protected Table $storageTable; /** * @var array{ @@ -67,16 +65,14 @@ class Conventions implements IConventions * @param literal-string|Fqn $storageName */ public function __construct( - IInflector $inflector, + protected readonly IInflector $inflector, IConnection $connection, string|Fqn $storageName, - EntityMetadata $entityMetadata, + protected readonly EntityMetadata $entityMetadata, Cache $cache, ) { - $this->inflector = $inflector; $this->platform = new CachedPlatform($connection->getPlatform(), $cache->derive('orm.db_reflection')); - $this->entityMetadata = $entityMetadata; $this->storageNameWithSchema = $storageName instanceof Fqn; $this->storageTable = $this->findStorageTable($storageName); diff --git a/src/Mapper/Dbal/Conventions/Inflector/CamelCaseInflector.php b/src/Mapper/Dbal/Conventions/Inflector/CamelCaseInflector.php index 4750ead3..e65926a9 100644 --- a/src/Mapper/Dbal/Conventions/Inflector/CamelCaseInflector.php +++ b/src/Mapper/Dbal/Conventions/Inflector/CamelCaseInflector.php @@ -19,7 +19,7 @@ public function formatAsProperty(string $column): string public function formatAsRelationshipProperty(string $column): string { - if (substr($column, -2) === 'Id') { + if (str_ends_with($column, 'Id')) { $column = substr($column, 0, -2); } return $this->formatAsProperty($column); diff --git a/src/Mapper/Dbal/Conventions/Inflector/SnakeCaseInflector.php b/src/Mapper/Dbal/Conventions/Inflector/SnakeCaseInflector.php index f7f97b00..5a993897 100644 --- a/src/Mapper/Dbal/Conventions/Inflector/SnakeCaseInflector.php +++ b/src/Mapper/Dbal/Conventions/Inflector/SnakeCaseInflector.php @@ -22,7 +22,7 @@ public function formatAsProperty(string $column): string public function formatAsRelationshipProperty(string $column): string { - if (substr($column, -3) === '_id') { + if (str_ends_with($column, '_id')) { $column = substr($column, 0, -3); } return $this->formatAsProperty($column); diff --git a/src/Mapper/Dbal/DbalMapper.php b/src/Mapper/Dbal/DbalMapper.php index 1c9dfc20..8effe8da 100644 --- a/src/Mapper/Dbal/DbalMapper.php +++ b/src/Mapper/Dbal/DbalMapper.php @@ -36,40 +36,33 @@ */ abstract class DbalMapper implements IMapper { - /** @var IConnection */ - protected $connection; - /** * Database table name. * * The must be in unescaped raw form. If you need to pass a database name/schema name, - * define this property with Fqn instance. Use null for autodetection. + * define this property with Fqn instance. Use null for auto-detection. * * @var literal-string|Fqn|null */ protected string|Fqn|null $tableName = null; - /** @var Cache */ - protected $cache; + protected Cache $cache; + private IConventions|null $conventions = null; /** @var IRepository|null */ - private $repository; + private IRepository|null $repository = null; /** @var array */ - private $cacheRM = []; - - /** @var DbalMapperCoordinator */ - private $mapperCoordinator; - - /** @var IConventions */ - private $conventions; + private array $cacheRM = []; - public function __construct(IConnection $connection, DbalMapperCoordinator $mapperCoordinator, Cache $cache) + public function __construct( + protected readonly IConnection $connection, + protected readonly DbalMapperCoordinator $mapperCoordinator, + Cache $cache, + ) { $key = md5(Json::encode($connection->getConfig())); - $this->connection = $connection; - $this->mapperCoordinator = $mapperCoordinator; $this->cache = $cache->derive('orm.mapper.' . $key); } diff --git a/src/Mapper/Dbal/DbalMapperCoordinator.php b/src/Mapper/Dbal/DbalMapperCoordinator.php index 40108e01..98b01182 100644 --- a/src/Mapper/Dbal/DbalMapperCoordinator.php +++ b/src/Mapper/Dbal/DbalMapperCoordinator.php @@ -8,16 +8,13 @@ class DbalMapperCoordinator { - /** @var IConnection */ - private $connection; + private bool $transactionActive = false; - /** @var bool */ - private $transactionActive = false; - - public function __construct(IConnection $connection) + public function __construct( + private readonly IConnection $connection, + ) { - $this->connection = $connection; } diff --git a/src/Mapper/Dbal/RelationshipMapperManyHasMany.php b/src/Mapper/Dbal/RelationshipMapperManyHasMany.php index b52d29f2..d66e8aa3 100644 --- a/src/Mapper/Dbal/RelationshipMapperManyHasMany.php +++ b/src/Mapper/Dbal/RelationshipMapperManyHasMany.php @@ -30,58 +30,38 @@ class RelationshipMapperManyHasMany implements IRelationshipMapperManyHasMany { - /** @var IConnection */ - protected $connection; - - /** @var PropertyMetadata */ - protected $metadata; - protected string|Fqn $joinTable; - - /** @var string */ - protected $primaryKeyFrom; - - /** @var string */ - protected $primaryKeyTo; - - /** @var DbalMapper */ - protected $targetMapper; + protected string $primaryKeyFrom; + protected string $primaryKeyTo; /** @var array */ - protected $cacheEntityIterators; + protected array $cacheEntityIterators; /** @var array> */ - protected $cacheCounts; - - /** @var DbalMapperCoordinator */ - private $mapperCoordinator; + protected array $cacheCounts; /** - * @param DbalMapper $mapper + * @param DbalMapper $targetMapper * @param DbalMapper $sourceMapper */ public function __construct( - IConnection $connection, - DbalMapper $mapper, + protected readonly IConnection $connection, + protected readonly DbalMapper $targetMapper, DbalMapper $sourceMapper, - DbalMapperCoordinator $mapperCoordinator, - PropertyMetadata $metadata, + protected readonly DbalMapperCoordinator $mapperCoordinator, + protected readonly PropertyMetadata $metadata, ) { assert($metadata->relationship !== null); - $this->connection = $connection; - $this->targetMapper = $mapper; - $this->metadata = $metadata; - $this->mapperCoordinator = $mapperCoordinator; if ($metadata->relationship->isMain) { - $parameters = $sourceMapper->getManyHasManyParameters($metadata, $mapper); + $parameters = $sourceMapper->getManyHasManyParameters($metadata, $targetMapper); $this->joinTable = $parameters[0]; [$this->primaryKeyFrom, $this->primaryKeyTo] = $parameters[1]; } else { assert($metadata->relationship->property !== null); - $parameters = $mapper->getManyHasManyParameters( + $parameters = $targetMapper->getManyHasManyParameters( $metadata->relationship->entityMetadata->getProperty($metadata->relationship->property), $sourceMapper, ); diff --git a/src/Mapper/Dbal/RelationshipMapperManyHasOne.php b/src/Mapper/Dbal/RelationshipMapperManyHasOne.php index fe54f929..0a6d0d30 100644 --- a/src/Mapper/Dbal/RelationshipMapperManyHasOne.php +++ b/src/Mapper/Dbal/RelationshipMapperManyHasOne.php @@ -19,27 +19,19 @@ class RelationshipMapperManyHasOne implements IRelationshipMapper { - /** @var IConnection */ - protected $connection; - - /** @var PropertyMetadata */ - protected $metadata; - /** @var array */ - protected $cacheEntityIterators; - - /** @var DbalMapper */ - private $targetMapper; + protected array $cacheEntityIterators; /** * @param DbalMapper $targetMapper */ - public function __construct(IConnection $connection, DbalMapper $targetMapper, PropertyMetadata $metadata) + public function __construct( + protected readonly IConnection $connection, + protected readonly DbalMapper $targetMapper, + protected readonly PropertyMetadata $metadata, + ) { - $this->connection = $connection; - $this->metadata = $metadata; - $this->targetMapper = $targetMapper; } diff --git a/src/Mapper/Dbal/RelationshipMapperOneHasMany.php b/src/Mapper/Dbal/RelationshipMapperOneHasMany.php index 094c8997..d4ae1e35 100644 --- a/src/Mapper/Dbal/RelationshipMapperOneHasMany.php +++ b/src/Mapper/Dbal/RelationshipMapperOneHasMany.php @@ -29,38 +29,27 @@ class RelationshipMapperOneHasMany implements IRelationshipMapper { - /** @var IConnection */ - protected $connection; - - /** @var PropertyMetadata */ - protected $metadata; - - /** @var PropertyRelationshipMetadata */ - protected $metadataRelationship; - - /** @var DbalMapper */ - protected $targetMapper; - - /** @var string */ - protected $joinStorageKey; + protected PropertyRelationshipMetadata $metadataRelationship; + protected string $joinStorageKey; /** @var array */ - protected $cacheEntityIterators; + protected array $cacheEntityIterators; /** @var array> */ - protected $cacheCounts; + protected array $cacheCounts; /** * @param DbalMapper $targetMapper */ - public function __construct(IConnection $connection, DbalMapper $targetMapper, PropertyMetadata $metadata) + public function __construct( + protected readonly IConnection $connection, + protected readonly DbalMapper $targetMapper, + protected readonly PropertyMetadata $metadata, + ) { assert($metadata->relationship !== null); assert($metadata->relationship->property !== null); - $this->connection = $connection; - $this->targetMapper = $targetMapper; - $this->metadata = $metadata; $this->metadataRelationship = $metadata->relationship; $this->joinStorageKey = $targetMapper->getConventions() ->convertEntityToStorageKey($metadata->relationship->property); diff --git a/src/Mapper/Memory/ArrayMapper.php b/src/Mapper/Memory/ArrayMapper.php index 92744266..c71db417 100644 --- a/src/Mapper/Memory/ArrayMapper.php +++ b/src/Mapper/Memory/ArrayMapper.php @@ -27,16 +27,15 @@ abstract class ArrayMapper implements IMapper { /** @var array|null */ - protected $data; + protected array|null $data = null; /** @var array> */ - protected $relationshipData = []; + protected array $relationshipData = []; - /** @var IConventions */ - protected $conventions; + protected IConventions|null $conventions = null; /** @var IRepository|null */ - private $repository; + private IRepository|null $repository = null; /** @var resource|null */ static protected $lock; @@ -179,7 +178,7 @@ public function flush(): void { try { $this->lock(); - if ($this->data === null || $this->relationshipData === null) { + if ($this->data === null) { return; } $this->saveData([$this->data, $this->relationshipData]); diff --git a/src/Mapper/Memory/Conventions/Conventions.php b/src/Mapper/Memory/Conventions/Conventions.php index fba91bcf..1f3f795a 100644 --- a/src/Mapper/Memory/Conventions/Conventions.php +++ b/src/Mapper/Memory/Conventions/Conventions.php @@ -11,16 +11,13 @@ class Conventions implements IConventions use SmartObject; - /** @var list */ - private $primaryKeys; - - /** * @param list $primaryKeys */ - public function __construct(array $primaryKeys) + public function __construct( + private readonly array $primaryKeys, + ) { - $this->primaryKeys = $primaryKeys; } diff --git a/src/Mapper/Memory/RelationshipMapperManyHasMany.php b/src/Mapper/Memory/RelationshipMapperManyHasMany.php index d1abf17e..df2c01e2 100644 --- a/src/Mapper/Memory/RelationshipMapperManyHasMany.php +++ b/src/Mapper/Memory/RelationshipMapperManyHasMany.php @@ -16,17 +16,18 @@ class RelationshipMapperManyHasMany implements IRelationshipMapperManyHasMany { /** @var ArrayMapper */ - protected $mapper; - - /** @var PropertyMetadata */ - protected $metadata; + protected ArrayMapper $mapper; /** * @param ArrayMapper $mapper * @param ArrayMapper $sourceMapper */ - public function __construct(ArrayMapper $mapper, ArrayMapper $sourceMapper, PropertyMetadata $metadata) + public function __construct( + ArrayMapper $mapper, + ArrayMapper $sourceMapper, + protected readonly PropertyMetadata $metadata, + ) { assert($metadata->relationship !== null); if ($metadata->relationship->isMain) { @@ -34,7 +35,6 @@ public function __construct(ArrayMapper $mapper, ArrayMapper $sourceMapper, Prop } else { $this->mapper = $mapper; } - $this->metadata = $metadata; } diff --git a/src/Mapper/Memory/RelationshipMapperManyHasOne.php b/src/Mapper/Memory/RelationshipMapperManyHasOne.php index e068e7a8..d15b8c63 100644 --- a/src/Mapper/Memory/RelationshipMapperManyHasOne.php +++ b/src/Mapper/Memory/RelationshipMapperManyHasOne.php @@ -14,13 +14,9 @@ class RelationshipMapperManyHasOne implements IRelationshipMapper { - /** @var PropertyMetadata */ - protected $metadata; - - - public function __construct(PropertyMetadata $metadata) + public function __construct( + protected readonly PropertyMetadata $metadata,) { - $this->metadata = $metadata; } diff --git a/src/Mapper/Memory/RelationshipMapperOneHasMany.php b/src/Mapper/Memory/RelationshipMapperOneHasMany.php index e220a38d..d1a6737f 100644 --- a/src/Mapper/Memory/RelationshipMapperOneHasMany.php +++ b/src/Mapper/Memory/RelationshipMapperOneHasMany.php @@ -14,21 +14,19 @@ class RelationshipMapperOneHasMany implements IRelationshipMapper { - /** @var PropertyMetadata */ - protected $metadata; - - /** @var string */ - protected $joinStorageKey; + protected string $joinStorageKey; /** * @param ArrayMapper $targetMapper */ - public function __construct(ArrayMapper $targetMapper, PropertyMetadata $metadata) + public function __construct( + ArrayMapper $targetMapper, + protected readonly PropertyMetadata $metadata, + ) { assert($metadata->relationship !== null); assert($metadata->relationship->property !== null); - $this->metadata = $metadata; $this->joinStorageKey = $targetMapper->getConventions() ->convertEntityToStorageKey($metadata->relationship->property); } diff --git a/src/Model/MetadataStorage.php b/src/Model/MetadataStorage.php index e170e6b7..ab233302 100644 --- a/src/Model/MetadataStorage.php +++ b/src/Model/MetadataStorage.php @@ -21,7 +21,7 @@ class MetadataStorage { /** @var EntityMetadata[] */ - private static $metadata = []; + private static array $metadata = []; public static function get(string $className): EntityMetadata diff --git a/src/Model/Model.php b/src/Model/Model.php index c1103dff..e5a50591 100644 --- a/src/Model/Model.php +++ b/src/Model/Model.php @@ -22,25 +22,9 @@ class Model implements IModel { use SmartObject; - /** @var list */ public $onFlush = []; - /** @var IRepositoryLoader */ - private $loader; - - /** @var MetadataStorage */ - private $metadataStorage; - - /** - * @var array{ - * array>, true>, - * array>>, - * array, class-string>> - * } - */ - private $configuration; - /** * Creates repository list configuration. @@ -77,14 +61,11 @@ public static function getConfiguration(array $repositories): array * } $configuration */ public function __construct( - array $configuration, - IRepositoryLoader $repositoryLoader, - MetadataStorage $metadataStorage + private readonly array $configuration, + private readonly IRepositoryLoader $repositoryLoader, + private readonly MetadataStorage $metadataStorage ) { - $this->loader = $repositoryLoader; - $this->metadataStorage = $metadataStorage; - $this->configuration = $configuration; } @@ -121,7 +102,7 @@ public function getRepository(string $className): IRepository if (!isset($this->configuration[0][$className])) { throw new InvalidArgumentException("Repository '$className' does not exist."); } - $repository = $this->loader->getRepository($className); + $repository = $this->repositoryLoader->getRepository($className); return $repository; } @@ -154,7 +135,7 @@ public function persist(IEntity $entity, bool $withCascade = true): IEntity foreach ($queue as $object) { if ($object instanceof IEntity) { $repository = $this->configuration[2][get_class($object)]; - $this->loader->getRepository($repository)->doPersist($object); + $this->repositoryLoader->getRepository($repository)->doPersist($object); } elseif ($object instanceof IRelationshipCollection) { $object->doPersist(); } elseif ($object instanceof IRelationshipContainer) { @@ -180,7 +161,7 @@ public function remove(IEntity $entity, bool $withCascade = true): IEntity foreach ($queuePersist as $object) { if ($object instanceof IEntity) { $repository = $this->configuration[2][get_class($object)]; - $this->loader->getRepository($repository)->doPersist($object); + $this->repositoryLoader->getRepository($repository)->doPersist($object); } elseif ($object instanceof IRelationshipCollection) { $object->doPersist(); } elseif ($object instanceof IRelationshipContainer) { @@ -189,7 +170,7 @@ public function remove(IEntity $entity, bool $withCascade = true): IEntity } foreach ($queueRemove as $object) { $repository = $this->configuration[2][get_class($object)]; - $this->loader->getRepository($repository)->doRemove($object); + $this->repositoryLoader->getRepository($repository)->doRemove($object); } return $entity; } @@ -251,8 +232,8 @@ private function getLoadedRepositories(): array { $repositories = []; foreach (array_keys($this->configuration[0]) as $className) { - if ($this->loader->isCreated($className)) { - $repositories[] = $this->loader->getRepository($className); + if ($this->repositoryLoader->isCreated($className)) { + $repositories[] = $this->repositoryLoader->getRepository($className); } } diff --git a/src/Model/SimpleModelFactory.php b/src/Model/SimpleModelFactory.php index aa3d31ce..40558b78 100644 --- a/src/Model/SimpleModelFactory.php +++ b/src/Model/SimpleModelFactory.php @@ -13,25 +13,16 @@ class SimpleModelFactory { - /** @var Cache */ - private $cache; - - /** @var array> */ - private $repositories; - - /** @var IMetadataParserFactory|null */ - private $metadataParserFactory; - - /** * @template E of IEntity * @param array> $repositories */ - public function __construct(Cache $cache, array $repositories, IMetadataParserFactory $metadataParserFactory = null) + public function __construct( + private readonly Cache $cache, + private readonly array $repositories, + private readonly IMetadataParserFactory|null $metadataParserFactory = null, + ) { - $this->cache = $cache; - $this->repositories = $repositories; - $this->metadataParserFactory = $metadataParserFactory; } diff --git a/src/Model/SimpleRepositoryLoader.php b/src/Model/SimpleRepositoryLoader.php index 24b99ba6..d4aae9a2 100644 --- a/src/Model/SimpleRepositoryLoader.php +++ b/src/Model/SimpleRepositoryLoader.php @@ -11,7 +11,7 @@ class SimpleRepositoryLoader implements IRepositoryLoader { /** @var array>, IRepository> */ - private $repositories = []; + private array $repositories = []; /** diff --git a/src/Relationships/HasMany.php b/src/Relationships/HasMany.php index f6f72ab3..3f61fe30 100644 --- a/src/Relationships/HasMany.php +++ b/src/Relationships/HasMany.php @@ -52,15 +52,15 @@ abstract class HasMany implements IRelationshipCollection protected bool $isModified = false; protected bool $exposeCollection; - protected PropertyMetadata $metadata; protected PropertyRelationshipMetadata $metadataRelationship; protected ?IRelationshipMapper $relationshipMapper = null; - public function __construct(PropertyMetadata $metadata) + public function __construct( + protected readonly PropertyMetadata $metadata, + ) { assert($metadata->relationship !== null); - $this->metadata = $metadata; $this->metadataRelationship = $metadata->relationship; $this->exposeCollection = (bool) ($this->metadata->args[HasMany::class]['exposeCollection'] ?? false); } diff --git a/src/Relationships/HasOne.php b/src/Relationships/HasOne.php index 93dff880..078b46d1 100644 --- a/src/Relationships/HasOne.php +++ b/src/Relationships/HasOne.php @@ -48,14 +48,14 @@ abstract class HasOne implements IRelationshipContainer protected bool $updatingReverseRelationship = false; protected bool $isModified = false; - protected PropertyMetadata $metadata; protected PropertyRelationshipMetadata $metadataRelationship; - public function __construct(PropertyMetadata $metadata) + public function __construct( + protected readonly PropertyMetadata $metadata, + ) { assert($metadata->relationship !== null); - $this->metadata = $metadata; $this->metadataRelationship = $metadata->relationship; } diff --git a/src/Repository/IdentityMap.php b/src/Repository/IdentityMap.php index eaa4910e..65fe5a1b 100644 --- a/src/Repository/IdentityMap.php +++ b/src/Repository/IdentityMap.php @@ -22,25 +22,23 @@ */ class IdentityMap { - /** @var IRepository */ - private $repository; - /** @var array */ - private $entities = []; + private array $entities = []; /** @var array */ - private $entitiesForRefresh = []; + private array $entitiesForRefresh = []; /** @var array, ReflectionClass> */ - private $entityReflections; + private array $entityReflections = []; /** * @param IRepository $repository */ - public function __construct(IRepository $repository) + public function __construct( + private readonly IRepository $repository, + ) { - $this->repository = $repository; } diff --git a/src/Repository/PersistenceHelper.php b/src/Repository/PersistenceHelper.php index 7b2c7d2d..c718ff15 100644 --- a/src/Repository/PersistenceHelper.php +++ b/src/Repository/PersistenceHelper.php @@ -15,10 +15,10 @@ class PersistenceHelper { /** @var array|IRelationshipContainer> */ - protected static $inputQueue = []; + protected static array $inputQueue = []; /** @var array|IRelationshipContainer|true> */ - protected static $outputQueue = []; + protected static array $outputQueue = []; /** diff --git a/src/Repository/Repository.php b/src/Repository/Repository.php index 7b6520e8..4ee6d795 100644 --- a/src/Repository/Repository.php +++ b/src/Repository/Repository.php @@ -52,68 +52,60 @@ abstract class Repository implements IRepository { /** @var array */ - public $onBeforePersist = []; + public array $onBeforePersist = []; /** @var array */ - public $onAfterPersist = []; + public array $onAfterPersist = []; /** @var array */ - public $onBeforeInsert = []; + public array $onBeforeInsert = []; /** @var array */ - public $onAfterInsert = []; + public array $onAfterInsert = []; /** @var array */ - public $onBeforeUpdate = []; + public array $onBeforeUpdate = []; /** @var array */ - public $onAfterUpdate = []; + public array $onAfterUpdate = []; /** @var array */ - public $onBeforeRemove = []; + public array $onBeforeRemove = []; /** @var array */ - public $onAfterRemove = []; + public array $onAfterRemove = []; /** @var array */ - public $onFlush = []; - - /** @var IMapper */ - protected $mapper; + public array $onFlush = []; /** @var class-string|null */ - protected $entityClassName; + protected string|null $entityClassName = null; - /** @var IModel|null */ - private $model; + private ?IModel $model = null; + private ConditionParser|null $conditionParser = null; /** @var IdentityMap */ - private $identityMap; + private IdentityMap $identityMap; /** @var array */ - private $proxyMethods; + private array $proxyMethods = []; /** @var array{list, list} */ - private $entitiesToFlush = [[], []]; - - /** @var IDependencyProvider|null */ - private $dependencyProvider; + private array $entitiesToFlush = [[], []]; /** @var array Collection functions cache */ private array $collectionFunctions = []; - /** @var ConditionParser|null */ - private $conditionParser; - /** * @param IMapper $mapper */ - public function __construct(IMapper $mapper, IDependencyProvider $dependencyProvider = null) + public function __construct( + protected readonly IMapper $mapper, + protected readonly IDependencyProvider|null $dependencyProvider = null, + ) { - $this->mapper = $mapper; $this->mapper->setRepository($this); - $this->dependencyProvider = $dependencyProvider; /** @var IdentityMap $identityMap */ $identityMap = new IdentityMap($this); diff --git a/src/TestHelper/EntityCreator.php b/src/TestHelper/EntityCreator.php index 0b93fd33..41481466 100644 --- a/src/TestHelper/EntityCreator.php +++ b/src/TestHelper/EntityCreator.php @@ -14,13 +14,10 @@ class EntityCreator { - /** @var IModel */ - private $model; - - - public function __construct(IModel $model) + public function __construct( + private readonly IModel $model, + ) { - $this->model = $model; } diff --git a/src/TestHelper/TestMapper.php b/src/TestHelper/TestMapper.php index f95ebaae..10a8bda4 100644 --- a/src/TestHelper/TestMapper.php +++ b/src/TestHelper/TestMapper.php @@ -14,10 +14,10 @@ class TestMapper extends ArrayMapper { /** @var string */ - protected $storage = ''; + protected string $storage = ''; /** @var mixed[] array of callbacks */ - protected $methods = []; + protected array $methods = []; public function addMethod(string $name, callable $callback): void