Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge 404b47b into 4efc725
  • Loading branch information
hrach committed Sep 24, 2020
2 parents 4efc725 + 404b47b commit a7a426a
Show file tree
Hide file tree
Showing 77 changed files with 648 additions and 220 deletions.
4 changes: 2 additions & 2 deletions src/Bridges/NetteDI/DIRepositoryFinder.php
Expand Up @@ -59,7 +59,7 @@ public function beforeCompile(): ?array
);
}

/** @var class-string<IRepository> $class */
/** @var class-string<IRepository<\Nextras\Orm\Entity\IEntity>> $class */
$class = $serviceDefinition->getType();
$repositories[$name] = $class;
$repositoriesMap[$class] = $serviceName;
Expand All @@ -80,7 +80,7 @@ protected function getRepositoryName(string $serviceName, $serviceDefinition): s


/**
* @param array<class-string<IRepository>, string> $repositoriesMap
* @param array<class-string<IRepository<\Nextras\Orm\Entity\IEntity>>, string> $repositoriesMap
*/
protected function setupRepositoryLoader(array $repositoriesMap): void
{
Expand Down
4 changes: 2 additions & 2 deletions src/Bridges/NetteDI/IRepositoryFinder.php
Expand Up @@ -17,15 +17,15 @@ public function __construct(string $modelClass, ContainerBuilder $containerBuild
/**
* Load configuration DIC phase.
* Returns array of repositories or null if they are loaded in the other phase.
* @return array<string, class-string<\Nextras\Orm\Repository\IRepository>>
* @return array<string, class-string<\Nextras\Orm\Repository\IRepository<\Nextras\Orm\Entity\IEntity>>>
*/
public function loadConfiguration(): ?array;


/**
* Before compile DIC phase.
* Returns array of repositories or null if they are loaded in the other phase.
* @return array<string, class-string<\Nextras\Orm\Repository\IRepository>>
* @return array<string, class-string<\Nextras\Orm\Repository\IRepository<\Nextras\Orm\Entity\IEntity>>>
*/
public function beforeCompile(): ?array;
}
8 changes: 4 additions & 4 deletions src/Bridges/NetteDI/OrmExtension.php
Expand Up @@ -140,7 +140,7 @@ protected function setupMetadataParserFactory(): void


/**
* @param array<class-string<\Nextras\Orm\Entity\IEntity>, class-string<\Nextras\Orm\Repository\IRepository>> $entityClassMap
* @param array<class-string<\Nextras\Orm\Entity\IEntity>, class-string<\Nextras\Orm\Repository\IRepository<\Nextras\Orm\Entity\IEntity>>> $entityClassMap
*/
protected function setupMetadataStorage(array $entityClassMap): void
{
Expand All @@ -162,9 +162,9 @@ protected function setupMetadataStorage(array $entityClassMap): void

/**
* @phpstan-param array{
* array<class-string<\Nextras\Orm\Repository\IRepository>, true>,
* array<string, class-string<\Nextras\Orm\Repository\IRepository>>,
* array<class-string<\Nextras\Orm\Entity\IEntity>, class-string<\Nextras\Orm\Repository\IRepository>>
* array<class-string<\Nextras\Orm\Repository\IRepository<\Nextras\Orm\Entity\IEntity>>, true>,
* array<string, class-string<\Nextras\Orm\Repository\IRepository<\Nextras\Orm\Entity\IEntity>>>,
* array<class-string<\Nextras\Orm\Entity\IEntity>, class-string<\Nextras\Orm\Repository\IRepository<\Nextras\Orm\Entity\IEntity>>>
* } $repositoriesConfig
*/
protected function setupModel(string $modelClass, array $repositoriesConfig): void
Expand Down
7 changes: 4 additions & 3 deletions src/Bridges/NetteDI/PhpDocRepositoryFinder.php
Expand Up @@ -39,6 +39,7 @@ public function loadConfiguration(): ?array
{
$repositories = $this->findRepositories($this->modelClass);
$repositoriesMap = [];

foreach ($repositories as $repositoryName => $repositoryClass) {
$this->setupMapperService($repositoryName, $repositoryClass);
$this->setupRepositoryService($repositoryName, $repositoryClass);
Expand All @@ -59,7 +60,7 @@ public function beforeCompile(): ?array
/**
* @return array<string, string>
* @phpstan-param class-string<\Nextras\Orm\Model\IModel> $modelClass
* @phpstan-return array<string, class-string<IRepository>>
* @phpstan-return array<string, class-string<IRepository<\Nextras\Orm\Entity\IEntity>>>
*/
protected function findRepositories(string $modelClass): array
{
Expand All @@ -83,7 +84,7 @@ protected function findRepositories(string $modelClass): array
* @var string $name
*/
foreach ($matches as [, $type, $name]) {
/** @phpstan-var class-string<IRepository> $type */
/** @phpstan-var class-string<IRepository<\Nextras\Orm\Entity\IEntity>> $type */
$type = Reflection::expandClassName($type, $modelReflection);
if (!class_exists($type)) {
throw new RuntimeException("Repository '{$type}' does not exist.");
Expand Down Expand Up @@ -140,7 +141,7 @@ protected function setupRepositoryService(string $repositoryName, string $reposi


/**
* @param array<class-string<IRepository>, string> $repositoriesMap
* @param array<class-string<IRepository<\Nextras\Orm\Entity\IEntity>>, string> $repositoriesMap
*/
protected function setupRepositoryLoader(array $repositoriesMap): void
{
Expand Down
16 changes: 11 additions & 5 deletions src/Bridges/NetteDI/RepositoryLoader.php
Expand Up @@ -4,6 +4,7 @@


use Nette\DI\Container;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Model\IRepositoryLoader;
use Nextras\Orm\Repository\IRepository;

Expand All @@ -13,12 +14,12 @@ class RepositoryLoader implements IRepositoryLoader
/** @var Container */
private $container;

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


/**
* @param array<class-string<IRepository>, string> $repositoryNamesMap
* @param array<class-string<IRepository<\Nextras\Orm\Entity\IEntity>>, string> $repositoryNamesMap
*/
public function __construct(Container $container, array $repositoryNamesMap)
{
Expand All @@ -33,11 +34,16 @@ public function hasRepository(string $className): bool
}


/**
* Returns instance of repository.
* @phpstan-template R of IRepository<\Nextras\Orm\Entity\IEntity>
* @phpstan-param class-string<R> $className
* @phpstan-return R
*/
public function getRepository(string $className): IRepository
{
$repository = $this->container->getService($this->repositoryNamesMap[$className]);
assert($repository instanceof IRepository);
return $repository;
/** @phpstan-var R */
return $this->container->getService($this->repositoryNamesMap[$className]);
}


Expand Down
25 changes: 20 additions & 5 deletions src/Collection/ArrayCollection.php
Expand Up @@ -18,6 +18,10 @@
use function array_values;


/**
* @phpstan-template E of IEntity
* @phpstan-implements ICollection<E>
*/
class ArrayCollection implements ICollection
{
/**
Expand All @@ -28,7 +32,7 @@ class ArrayCollection implements ICollection

/**
* @var IEntity[]
* @phpstan-var list<IEntity>
* @phpstan-var list<E>
*/
protected $data;

Expand All @@ -38,18 +42,24 @@ class ArrayCollection implements ICollection
/** @var IEntity|null */
protected $relationshipParent;

/** @var null|Iterator<IEntity> */
/**
* @var Iterator<IEntity>|null
* @phpstan-var Iterator<E>|null
*/
protected $fetchIterator;

/** @var IRepository */
/**
* @var IRepository<IEntity>
* @phpstan-var IRepository<E>
*/
protected $repository;

/** @var ArrayCollectionHelper */
protected $helper;

/**
* @var Closure[]
* @phpstan-var list<Closure(IEntity): mixed>
* @phpstan-var list<Closure(E): mixed>
*/
protected $collectionFilter = [];

Expand All @@ -68,7 +78,8 @@ class ArrayCollection implements ICollection

/**
* @param IEntity[] $entities
* @phpstan-param list<IEntity> $entities
* @phpstan-param IRepository<E> $repository
* @phpstan-param list<E> $entities
*/
public function __construct(array $entities, IRepository $repository)
{
Expand Down Expand Up @@ -192,6 +203,9 @@ public function __call(string $name, array $args)
}


/**
* @phpstan-return Iterator<int, E>
*/
public function getIterator(): Iterator
{
if ($this->relationshipParent !== null && $this->relationshipMapper !== null) {
Expand All @@ -212,6 +226,7 @@ public function getIterator(): Iterator
$this->entityFetchEventTriggered = true;
}

/** @phpstan-var Iterator<E> */
return $entityIterator;
}

Expand Down
29 changes: 25 additions & 4 deletions src/Collection/DbalCollection.php
Expand Up @@ -17,6 +17,10 @@
use function is_array;


/**
* @phpstan-template E of IEntity
* @phpstan-implements ICollection<E>
*/
class DbalCollection implements ICollection
{
/**
Expand All @@ -31,10 +35,16 @@ class DbalCollection implements ICollection
/** @var IEntity|null */
protected $relationshipParent;

/** @var null|Iterator<IEntity> */
/**
* @var Iterator<IEntity>|null
* @phpstan-var Iterator<E>|null
*/
protected $fetchIterator;

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

/** @var IConnection */
Expand All @@ -47,8 +57,8 @@ class DbalCollection implements ICollection
protected $helper;

/**
* @var array|null
* @phpstan-var list<IEntity>|null
* @var IEntity[]|null
* @phpstan-var list<E>|null
*/
protected $result;

Expand All @@ -59,6 +69,9 @@ class DbalCollection implements ICollection
protected $entityFetchEventTriggered = false;


/**
* @phpstan-param DbalMapper<E> $mapper
*/
public function __construct(DbalMapper $mapper, IConnection $connection, QueryBuilder $queryBuilder)
{
$this->mapper = $mapper;
Expand Down Expand Up @@ -153,6 +166,10 @@ public function limitBy(int $limit, int $offset = null): ICollection
}


/**
* @inheritDoc
* @phpstan-return E|null
*/
public function fetch(): ?IEntity
{
if ($this->fetchIterator === null) {
Expand Down Expand Up @@ -193,6 +210,9 @@ public function __call(string $name, array $args)
}


/**
* @phpstan-return Iterator<int, E>
*/
public function getIterator(): Iterator
{
if ($this->relationshipParent !== null && $this->relationshipMapper !== null) {
Expand All @@ -214,6 +234,7 @@ public function getIterator(): Iterator
$this->entityFetchEventTriggered = true;
}

/** @phpstan-var Iterator<E> */
return $entityIterator;
}

Expand Down
9 changes: 8 additions & 1 deletion src/Collection/EmptyCollection.php
Expand Up @@ -10,7 +10,11 @@
use Nextras\Orm\Mapper\IRelationshipMapper;


class EmptyCollection implements ICollection
/**
* @phpstan-template E of IEntity
* @phpstan-implements ICollection<E>
*/
final class EmptyCollection implements ICollection
{
/** @var IRelationshipMapper|null */
private $relationshipMapper;
Expand Down Expand Up @@ -82,6 +86,9 @@ public function fetchPairs(string $key = null, string $value = null): array
}


/**
* @phpstan-return Iterator<int, E>
*/
public function getIterator(): Iterator
{
return new EmptyIterator();
Expand Down
27 changes: 21 additions & 6 deletions src/Collection/HasManyCollection.php
Expand Up @@ -18,23 +18,33 @@
use function spl_object_hash;


/**
* @phpstan-template E of IEntity
* @phpstan-implements ICollection<E>
*/
class HasManyCollection implements ICollection
{
/**
* @var array of callbacks with $entities argument
* @phpstan-var array<callable(\Traversable<IEntity>):void>
* @phpstan-var array<callable(\Traversable<E>):void>
*/
public $onEntityFetch = [];

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

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

/**
* @var callable A callback returning a list entities to add & remove.
* @phpstan-var callable(): array{array<string, IEntity>, array<string, IEntity>}
* @phpstan-var callable(): array{array<string, E>, array<string, E>}
*/
private $diffCallback;

Expand All @@ -43,7 +53,9 @@ class HasManyCollection implements ICollection


/**
* @phpstan-param callable():array{array<string, IEntity>, array<string, IEntity>} $diffCallback
* @phpstan-param IRepository<E> $repository
* @phpstan-param ICollection<E> $innerCollection
* @phpstan-param callable():array{array<string, E>, array<string, E>} $diffCallback
*/
public function __construct(
IRepository $repository,
Expand All @@ -53,7 +65,7 @@ public function __construct(
{
$this->storageCollection = $innerCollection;
$this->diffCallback = $diffCallback;
$this->inMemoryCollection = new MutableArrayCollection([], $repository);
$this->inMemoryCollection = new MutableArrayCollection([], $repository); // @phpstan-ignore-line
}


Expand Down Expand Up @@ -152,6 +164,9 @@ public function fetchPairs(string $key = null, string $value = null): array
}


/**
* @phpstan-return Iterator<E>
*/
public function getIterator(): Iterator
{
[$toAdd, $toRemove] = ($this->diffCallback)();
Expand Down
5 changes: 4 additions & 1 deletion src/Collection/Helpers/ArrayCollectionHelper.php
Expand Up @@ -27,10 +27,13 @@

class ArrayCollectionHelper
{
/** @var IRepository */
/** @var IRepository<IEntity> */
private $repository;


/**
* @param IRepository<IEntity> $repository
*/
public function __construct(IRepository $repository)
{
$this->repository = $repository;
Expand Down

0 comments on commit a7a426a

Please sign in to comment.