Skip to content

Commit

Permalink
more explicit PHP type definitions for properties
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Oct 11, 2024
1 parent 91cac38 commit bd794db
Show file tree
Hide file tree
Showing 45 changed files with 216 additions and 391 deletions.
7 changes: 2 additions & 5 deletions src/Bridges/NetteDI/DIRepositoryFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions src/Bridges/NetteDI/DependencyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
19 changes: 5 additions & 14 deletions src/Bridges/NetteDI/PhpDocRepositoryFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,12 @@

class PhpDocRepositoryFinder implements IRepositoryFinder
{
/** @var class-string<IModel> */
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;
}


Expand Down
14 changes: 4 additions & 10 deletions src/Bridges/NetteDI/RepositoryLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@

class RepositoryLoader implements IRepositoryLoader
{
/** @var Container */
private $container;

/** @var array<class-string<IRepository<IEntity>>, string> */
private $repositoryNamesMap;


/**
* @param array<class-string<IRepository<IEntity>>, 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;
}


Expand Down
35 changes: 14 additions & 21 deletions src/Collection/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,43 @@
class ArrayCollection implements ICollection, MemoryCollection
{
/** @var list<callable(\Traversable<E> $entities): void> */
public $onEntityFetch = [];
public array $onEntityFetch = [];

/** @var list<E> */
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<E>|null */
protected $fetchIterator;

/** @var IRepository<E> */
protected $repository;
protected Iterator|null $fetchIterator = null;

/** @var ArrayCollectionHelper */
protected $helper;
protected ArrayCollectionHelper|null $helper = null;

/** @var array<Closure(E): ArrayExpressionResult> */
protected $collectionFilter = [];
protected array $collectionFilter = [];

/** @var list<array{mixed, string}> */
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<E> $entities
* @param IRepository<E> $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;
}


Expand Down
47 changes: 15 additions & 32 deletions src/Collection/DbalCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,53 +28,36 @@
class DbalCollection implements ICollection
{
/** @var list<callable(\Traversable<E> $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<E>|null */
protected $fetchIterator;

/** @var DbalMapper<E> */
protected $mapper;

/** @var IConnection */
protected $connection;

/** @var QueryBuilder */
protected $queryBuilder;
protected Iterator|null $fetchIterator = null;

/** @var array<mixed> FindBy expressions for deferred processing */
protected $filtering = [];
protected array $filtering = [];

/** @var array<array{DbalExpressionResult, string}> OrderBy expression result & sorting direction */
protected $ordering = [];

/** @var DbalQueryBuilderHelper */
protected $helper;
protected array $ordering = [];
protected DbalQueryBuilderHelper|null $helper = null;

/** @var list<E>|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<E> $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;
}


Expand Down
3 changes: 1 addition & 2 deletions src/Collection/EmptyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/Collection/EntityIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
*/
class EntityIterator implements IEntityPreloadContainer, Iterator, Countable
{
/** @var int */
private $position = 0;
private int $position = 0;

/** @var list<IEntity> */
private $iteratable;
private array $iteratable;

/** @var array<string, list<mixed>> */
private $preloadCache;
private array $preloadCache = [];


/**
Expand Down
4 changes: 3 additions & 1 deletion src/Collection/Expression/ExpressionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions src/Collection/HasManyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,17 @@ class HasManyCollection implements ICollection
/** @var array<callable(\Traversable<E>):void> */
public $onEntityFetch = [];

/** @var IRepository<E> */
private $repository;

/** @var ICollection<E> */
private $storageCollection;
private ICollection $storageCollection;

/** @var MutableArrayCollection<E> */
private $inMemoryCollection;
private MutableArrayCollection $inMemoryCollection;

/** @var callable(): array{array<array-key, E>, array<array-key, E>} */
private $diffCallback;

/** @var Iterator<mixed, mixed>|null */
private $fetchIterator;
private ?Iterator $fetchIterator = null;


/**
Expand All @@ -49,12 +46,11 @@ class HasManyCollection implements ICollection
* @param callable():array{array<array-key, E>, array<array-key, E>} $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
Expand Down
15 changes: 6 additions & 9 deletions src/Collection/MultiEntityIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,22 @@
*/
class MultiEntityIterator implements IEntityPreloadContainer, Iterator, Countable
{
/** @var int */
private $position = 0;

/** @var array<int|string, list<IEntity>> */
private $data;
private int $position = 0;

/** @var list<IEntity> */
private $iterable;
private array $iterable;

/** @var array<string, list<mixed>> */
private $preloadCache;
private array $preloadCache;


/**
* @param array<int|string, list<IEntity>> $data
*/
public function __construct(array $data)
public function __construct(
private array $data,
)
{
$this->data = $data;
}


Expand Down
4 changes: 2 additions & 2 deletions src/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ abstract class AbstractEntity implements IEntity


/** @var IRepository<IEntity>|null */
private $repository;
private IRepository|null $repository = null;

/** @var array<string, bool> */
private $modified = [];
private array $modified = [];

/** @var mixed */
private $persistedId = null;
Expand Down
3 changes: 1 addition & 2 deletions src/Entity/Embeddable/Embeddable.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ abstract class Embeddable implements IEmbeddable
use ImmutableDataTrait;


/** @var IEntity|null */
protected $parentEntity;
protected IEntity|null $parentEntity = null;


/**
Expand Down
25 changes: 9 additions & 16 deletions src/Entity/Embeddable/EmbeddableContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, PropertyMetadata> */
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();
}

Expand Down
3 changes: 1 addition & 2 deletions src/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

class Entity extends AbstractEntity implements IEntityHasPreloadContainer
{
/** @var IEntityPreloadContainer|null */
private $preloadContainer;
private IEntityPreloadContainer|null $preloadContainer = null;


/**
Expand Down
Loading

0 comments on commit bd794db

Please sign in to comment.