Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed May 23, 2020
1 parent 86dfe2c commit 107ddf9
Show file tree
Hide file tree
Showing 30 changed files with 288 additions and 82 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -35,7 +35,7 @@
"nette/tester": "~2.1",
"marc-mabe/php-enum": "~3.0",
"mockery/mockery": "~1.2",
"phpstan/phpstan": "0.12.20",
"phpstan/phpstan": "0.12.23",
"phpstan/phpstan-deprecation-rules": "0.12.2",
"phpstan/phpstan-nette": "0.12.6",
"tracy/tracy": "~2.3"
Expand Down
4 changes: 2 additions & 2 deletions src/Bridges/NetteDI/PhpDocRepositoryFinder.php
Expand Up @@ -59,7 +59,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 +83,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
10 changes: 9 additions & 1 deletion 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 Down Expand Up @@ -33,10 +34,17 @@ public function hasRepository(string $className): bool
}


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

Expand Down
14 changes: 9 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 @@ -27,8 +31,8 @@ class ArrayCollection implements ICollection
public $onEntityFetch = [];

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

Expand All @@ -41,7 +45,7 @@ class ArrayCollection implements ICollection
/** @var null|Iterator<IEntity> */
protected $fetchIterator;

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

/** @var ArrayCollectionHelper */
Expand All @@ -67,8 +71,8 @@ class ArrayCollection implements ICollection


/**
* @param IEntity[] $entities
* @phpstan-param list<IEntity> $entities
* @param E[] $entities
* @phpstan-param list<E> $entities
*/
public function __construct(array $entities, IRepository $repository)
{
Expand Down
6 changes: 5 additions & 1 deletion src/Collection/EmptyCollection.php
Expand Up @@ -10,7 +10,11 @@
use Nextras\Orm\NoResultException;


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
9 changes: 8 additions & 1 deletion src/Collection/ICollection.php
Expand Up @@ -14,7 +14,8 @@


/**
* @extends IteratorAggregate<int, IEntity>
* @phpstan-template E of IEntity
* @extends IteratorAggregate<int, E>
*/
interface ICollection extends IteratorAggregate, Countable
{
Expand Down Expand Up @@ -43,6 +44,7 @@ interface ICollection extends IteratorAggregate, Countable
* Limits collection via {@see ICollection::findBy()} and returns the first entity (or null).
*
* @phpstan-param array<string, mixed>|array<mixed> $conds
* @phpstan-return E|null
*/
public function getBy(array $conds): ?IEntity;

Expand All @@ -54,13 +56,15 @@ public function getBy(array $conds): ?IEntity;
*
* @phpstan-param array<string, mixed>|array<mixed> $conds
* @throws NoResultException
* @phpstan-return E
*/
public function getByChecked(array $conds): IEntity;


/**
* Returns entity by primary value, null if none found.
* @param mixed $id
* @phpstan-return E|null
*/
public function getById($id): ?IEntity;

Expand All @@ -69,6 +73,7 @@ public function getById($id): ?IEntity;
* Returns entity by primary value, throws if none found.
* @param mixed $id
* @throws NoResultException
* @phpstan-return E
*/
public function getByIdChecked($id): IEntity;

Expand Down Expand Up @@ -159,13 +164,15 @@ public function limitBy(int $limit, int $offset = null): ICollection;

/**
* Fetches the first row.
* @phpstan-return E|null
*/
public function fetch(): ?IEntity;


/**
* Fetches all records.
* @return IEntity[]
* @phpstan-return array<E>
*/
public function fetchAll();

Expand Down
1 change: 1 addition & 0 deletions src/Entity/Reflection/MetadataParser.php
Expand Up @@ -526,6 +526,7 @@ protected function processRelationshipEntityProperty(PropertyMetadata $property,
}
}

/** @var class-string<\Nextras\Orm\Entity\IEntity> $entity */
$entity = Reflection::expandClassName($class, $this->currentReflection);
if (!isset($this->entityClassesMap[$entity])) {
throw new InvalidModifierDefinitionException("Relationship {{$modifier}} in {$this->currentReflection->name}::\${$property->name} points to unknown '{$entity}' entity. Don't forget to return it in IRepository::getEntityClassNames() and register its repository.");
Expand Down
7 changes: 5 additions & 2 deletions src/Entity/Reflection/PropertyRelationshipMetadata.php
Expand Up @@ -12,11 +12,14 @@ class PropertyRelationshipMetadata

/**
* @var string
* @phpstan-var class-string<\Nextras\Orm\Repository\IRepository>
* @phpstan-var class-string<\Nextras\Orm\Repository\IRepository<\Nextras\Orm\Entity\IEntity>>
*/
public $repository;

/** @var string */
/**
* @var string
* @phpstan-var class-string<\Nextras\Orm\Entity\IEntity>
*/
public $entity;

/** @var EntityMetadata */
Expand Down
17 changes: 17 additions & 0 deletions src/Mapper/IMapper.php
Expand Up @@ -9,46 +9,62 @@
use Nextras\Orm\Repository\IRepository;


/**
* @phpstan-template E of IEntity
*/
interface IMapper
{
/**
* Returns all entities.
* @phpstan-return ICollection<E>
*/
public function findAll(): ICollection;


/**
* Creates collection with HasOne mapper.
* @phpstan-return ICollection<IEntity>
*/
public function createCollectionManyHasOne(PropertyMetadata $metadata): ICollection;


/**
* Creates collection with OneHasOneDirected mapper.
* @phpstan-return ICollection<IEntity>
*/
public function createCollectionOneHasOne(PropertyMetadata $metadata): ICollection;


/**
* Creates collection with ManyHasMany mapper.
* @phpstan-param IMapper<IEntity>
* @phpstan-return ICollection<IEntity>
*/
public function createCollectionManyHasMany(IMapper $sourceMapper, PropertyMetadata $metadata): ICollection;


/**
* Creates collection with OneHasMany mapper.
* @phpstan-return ICollection<IEntity>
*/
public function createCollectionOneHasMany(PropertyMetadata $metadata): ICollection;


/**
* @phpstan-param IRepository<E> $repository
*/
public function setRepository(IRepository $repository): void;


/**
* @phpstan-return IRepository<E>
*/
public function getRepository(): IRepository;


/**
* Persist entity and return new id.
* @phpstan-param E $entity
* @return mixed
* @internal
* @see IRepository::persist()
Expand All @@ -57,6 +73,7 @@ public function persist(IEntity $entity);


/**
* @phpstan-param E $entity
* @see IRepository::remove()
*/
public function remove(IEntity $entity): void;
Expand Down
2 changes: 2 additions & 0 deletions src/Mapper/IRelationshipMapper.php
Expand Up @@ -12,13 +12,15 @@ interface IRelationshipMapper
{
/**
* Returns iterator.
* @phpstan-param ICollection<IEntity> $collection
* @return Iterator<IEntity>
*/
public function getIterator(IEntity $parent, ICollection $collection): Iterator;


/**
* Returns iterator's counts.
* @phpstan-param ICollection<IEntity> $collection
*/
public function getIteratorCount(IEntity $parent, ICollection $collection): int;

Expand Down
11 changes: 8 additions & 3 deletions src/Mapper/Memory/ArrayMapper.php
Expand Up @@ -20,14 +20,18 @@
use function assert;


/**
* @phpstan-template E of IEntity
* @phpstan-implements IMapper<E>
*/
abstract class ArrayMapper implements IMapper
{
use MapperRepositoryTrait;


/**
* @var IEntity[]|null[]|null
* @phpstan-var array<int|string, IEntity|null>|null
* @phpstan-var array<int|string, E|null>|null
*/
protected $data;

Expand Down Expand Up @@ -57,7 +61,8 @@ public function findAll(): ICollection


/**
* @phpstan-param list<IEntity> $data
* @phpstan-param list<E> $data
* @phpstan-return ICollection<E>
*/
public function toCollection(array $data): ICollection
{
Expand Down Expand Up @@ -230,7 +235,7 @@ protected function initializeData(): void


/**
* @phpstan-return list<IEntity>
* @phpstan-return list<E>
*/
protected function getData(): array
{
Expand Down
6 changes: 5 additions & 1 deletion src/Mapper/Memory/Conventions/Conventions.php
Expand Up @@ -12,7 +12,10 @@ class Conventions implements IConventions
use SmartObject;


/** @var IMapper */
/**
* @var IMapper
* @phpstan-var IMapper<\Nextras\Orm\Entity\IEntity>
*/
private $mapper;

/**
Expand All @@ -24,6 +27,7 @@ class Conventions implements IConventions

/**
* @param string[] $primaryKeys
* @phpstan-param IMapper<\Nextras\Orm\Entity\IEntity> $mapper
* @phpstan-param list<string> $primaryKeys
*/
public function __construct(IMapper $mapper, array $primaryKeys)
Expand Down
8 changes: 4 additions & 4 deletions src/Mapper/Memory/RelationshipMapperManyHasMany.php
Expand Up @@ -3,6 +3,7 @@
namespace Nextras\Orm\Mapper\Memory;


use Countable;
use Iterator;
use Nextras\Orm\Collection\EntityIterator;
use Nextras\Orm\Collection\ICollection;
Expand Down Expand Up @@ -38,9 +39,6 @@ public function clearCache(): void
}


/**
* @return EntityIterator
*/
public function getIterator(IEntity $parent, ICollection $collection): Iterator
{
assert($this->metadata->relationship !== null);
Expand All @@ -67,7 +65,9 @@ public function getIterator(IEntity $parent, ICollection $collection): Iterator

public function getIteratorCount(IEntity $parent, ICollection $collection): int
{
return count($this->getIterator($parent, $collection));
$iterator = $this->getIterator($parent, $collection);
assert($iterator instanceof Countable);
return count($iterator);
}


Expand Down
3 changes: 3 additions & 0 deletions src/Mapper/Memory/RelationshipMapperOneHasMany.php
Expand Up @@ -21,6 +21,9 @@ class RelationshipMapperOneHasMany implements IRelationshipMapper
protected $joinStorageKey;


/**
* @param ArrayMapper<> $targetMapper
*/
public function __construct(ArrayMapper $targetMapper, PropertyMetadata $metadata)
{
assert($metadata->relationship !== null);
Expand Down

0 comments on commit 107ddf9

Please sign in to comment.