Skip to content

Commit

Permalink
phpstan: proper null checks for metadata relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Feb 4, 2019
1 parent 212b15b commit 6c25f64
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 29 deletions.
2 changes: 0 additions & 2 deletions .phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ includes:

parameters:
ignoreErrors:
# not convenient to mark every place with unnecessary check
- '#Cannot access property \$\w+ on Nextras\\Orm\\Entity\\Reflection\\PropertyRelationshipMetadata\|null\.#'
# https://github.com/phpstan/phpstan/issues/587
- '#Constructor of class Nextras\\Orm\\Bridges\\NetteDI\\DIRepositoryFinder has an unused parameter \$modelClass\.#'
1 change: 1 addition & 0 deletions src/Mapper/Dbal/RelationshipMapperManyHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ public function remove(IEntity $parent, array $remove)

protected function buildList(IEntity $parent, array $entries): array
{
assert($this->metadata->relationship !== null);
if (!$this->metadata->relationship->isMain) {
throw new LogicException('ManyHasMany relationship has to be persisted in the primary mapper.');
}
Expand Down
11 changes: 8 additions & 3 deletions src/Mapper/Dbal/RelationshipMapperOneHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Entity\IEntityHasPreloadContainer;
use Nextras\Orm\Entity\Reflection\PropertyMetadata;
use Nextras\Orm\Entity\Reflection\PropertyRelationshipMetadata;
use Nextras\Orm\Mapper\IRelationshipMapper;


Expand All @@ -27,6 +28,9 @@ class RelationshipMapperOneHasMany implements IRelationshipMapper
/** @var PropertyMetadata */
protected $metadata;

/** @var PropertyRelationshipMetadata */
protected $metadataRelationship;

/** @var DbalMapper */
protected $targetMapper;

Expand All @@ -47,6 +51,7 @@ public function __construct(IConnection $connection, DbalMapper $targetMapper, P
$this->connection = $connection;
$this->targetMapper = $targetMapper;
$this->metadata = $metadata;
$this->metadataRelationship = $metadata->relationship;
$this->joinStorageKey = $targetMapper->getStorageReflection()->convertEntityToStorageKey($metadata->relationship->property);
}

Expand Down Expand Up @@ -103,7 +108,7 @@ protected function fetchByOnePassStrategy(QueryBuilder $builder, array $values):
$result = $this->connection->queryArgs($builder->getQuerySql(), $builder->getQueryParameters());
$entities = [];

$property = $this->metadata->relationship->property;
$property = $this->metadataRelationship->property;
assert($property !== null);

while (($data = $result->fetch())) {
Expand All @@ -122,7 +127,7 @@ protected function fetchByTwoPassStrategy(QueryBuilder $builder, array $values):
$builder = clone $builder;
$targetPrimaryKey = array_map(function ($key) {
return $this->targetMapper->getStorageReflection()->convertEntityToStorageKey($key);
}, $this->metadata->relationship->entityMetadata->getPrimaryKey());
}, $this->metadataRelationship->entityMetadata->getPrimaryKey());
$isComposite = count($targetPrimaryKey) !== 1;

foreach (array_unique(array_merge($targetPrimaryKey, [$this->joinStorageKey])) as $key) {
Expand Down Expand Up @@ -172,7 +177,7 @@ protected function fetchByTwoPassStrategy(QueryBuilder $builder, array $values):
foreach ($map as $joiningStorageKey => $primaryValues) {
foreach ($primaryValues as $primaryValue) {
$entity = $entitiesResult[$primaryValue];
$entities[$entity->getRawValue($this->metadata->relationship->property)][] = $entity;
$entities[$entity->getRawValue($this->metadataRelationship->property)][] = $entity;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Mapper/Memory/RelationshipMapperManyHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class RelationshipMapperManyHasMany implements IRelationshipMapperManyHasMany

public function __construct(PropertyMetadata $metadata, ArrayMapper $mapper)
{
assert($metadata->relationship !== null);
$this->metadata = $metadata;
$this->mapper = $mapper;
}
Expand All @@ -43,6 +42,7 @@ public function clearCache()
*/
public function getIterator(IEntity $parent, ICollection $collection): Iterator
{
assert($this->metadata->relationship !== null);
if ($this->metadata->relationship->isMain) {
$relationshipData = $this->mapper->getRelationshipDataStorage($this->metadata->name);
$id = $parent->getValue('id');
Expand Down
2 changes: 2 additions & 0 deletions src/Mapper/Memory/RelationshipMapperOneHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Nextras\Orm\Collection\ICollection;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Entity\Reflection\PropertyMetadata;
use Nextras\Orm\Entity\Reflection\PropertyRelationshipMetadata;
use Nextras\Orm\Mapper\IRelationshipMapper;


Expand Down Expand Up @@ -41,6 +42,7 @@ public function clearCache()

public function getIterator(IEntity $parent, ICollection $collection): Iterator
{
assert($this->metadata->relationship !== null);
$className = $this->metadata->relationship->entityMetadata->className;
$data = $collection->findBy(["$className->{$this->joinStorageKey}->id" => $parent->getValue('id')])->fetchAll();
return new EntityIterator($data);
Expand Down
11 changes: 8 additions & 3 deletions src/Relationships/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Nextras\Orm\Collection\ICollection;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Entity\Reflection\PropertyMetadata;
use Nextras\Orm\Entity\Reflection\PropertyRelationshipMetadata;
use Nextras\Orm\InvalidStateException;
use Nextras\Orm\Mapper\IRelationshipMapper;
use Nextras\Orm\Repository\IRepository;
Expand All @@ -30,6 +31,9 @@ abstract class HasMany implements IRelationshipCollection
/** @var PropertyMetadata */
protected $metadata;

/** @var PropertyRelationshipMetadata */
protected $metadataRelationship;

/** @var ICollection|null */
protected $collection;

Expand Down Expand Up @@ -62,6 +66,7 @@ public function __construct(PropertyMetadata $metadata)
{
assert($metadata->relationship !== null);
$this->metadata = $metadata;
$this->metadataRelationship = $metadata->relationship;
}


Expand Down Expand Up @@ -339,7 +344,7 @@ public function __clone()
protected function getTargetRepository(): IRepository
{
if (!$this->targetRepository) {
$this->targetRepository = $this->parent->getRepository()->getModel()->getRepository($this->metadata->relationship->repository);
$this->targetRepository = $this->parent->getRepository()->getModel()->getRepository($this->metadataRelationship->repository);
}

return $this->targetRepository;
Expand All @@ -360,8 +365,8 @@ protected function getRelationshipMapper()

protected function applyDefaultOrder(ICollection $collection)
{
if ($this->metadata->relationship->order !== null) {
return $collection->orderBy($this->metadata->relationship->order);
if ($this->metadataRelationship->order !== null) {
return $collection->orderBy($this->metadataRelationship->order);
} else {
return $collection;
}
Expand Down
9 changes: 7 additions & 2 deletions src/Relationships/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Nextras\Orm\Collection\ICollection;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Entity\Reflection\PropertyMetadata;
use Nextras\Orm\Entity\Reflection\PropertyRelationshipMetadata;
use Nextras\Orm\InvalidArgumentException;
use Nextras\Orm\Mapper\IRelationshipMapper;
use Nextras\Orm\NullValueException;
Expand All @@ -29,6 +30,9 @@ abstract class HasOne implements IRelationshipContainer
/** @var PropertyMetadata */
protected $metadata;

/** @var PropertyRelationshipMetadata */
protected $metadataRelationship;

/** @var ICollection */
protected $collection;

Expand All @@ -55,6 +59,7 @@ public function __construct(PropertyMetadata $metadata)
{
assert($metadata->relationship !== null);
$this->metadata = $metadata;
$this->metadataRelationship = $metadata->relationship;
}


Expand Down Expand Up @@ -195,7 +200,7 @@ protected function getPrimaryValue()
protected function getTargetRepository(): IRepository
{
if (!$this->targetRepository) {
$this->targetRepository = $this->parent->getRepository()->getModel()->getRepository($this->metadata->relationship->repository);
$this->targetRepository = $this->parent->getRepository()->getModel()->getRepository($this->metadataRelationship->repository);
}

return $this->targetRepository;
Expand All @@ -216,7 +221,7 @@ protected function createEntity($entity, bool $allowNull)
{
if ($entity instanceof IEntity) {
if ($this->parent->isAttached()) {
$repository = $this->parent->getRepository()->getModel()->getRepository($this->metadata->relationship->repository);
$repository = $this->parent->getRepository()->getModel()->getRepository($this->metadataRelationship->repository);
$repository->attach($entity);

} elseif ($entity->isAttached()) {
Expand Down
16 changes: 8 additions & 8 deletions src/Relationships/ManyHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function doPersist()
$this->isModified = false;
$this->collection = null;

if ($this->metadata->relationship->isMain) {
if ($this->metadataRelationship->isMain) {
$this->getRelationshipMapper()->clearCache();
$this->getRelationshipMapper()->remove($this->parent, $toRemove);
$this->getRelationshipMapper()->add($this->parent, $toAdd);
Expand All @@ -60,7 +60,7 @@ protected function modify(): void

protected function createCollection(): ICollection
{
if ($this->metadata->relationship->isMain) {
if ($this->metadataRelationship->isMain) {
$mapperOne = $this->parent->getRepository()->getMapper();
$mapperTwo = $this->getTargetRepository()->getMapper();
} else {
Expand All @@ -71,11 +71,11 @@ protected function createCollection(): ICollection
$collection = $mapperOne->createCollectionManyHasMany($mapperTwo, $this->metadata);
$collection = $collection->setRelationshipParent($this->parent);
$collection->subscribeOnEntityFetch(function (Traversable $entities) {
if (!$this->metadata->relationship->property) {
if (!$this->metadataRelationship->property) {
return;
}
foreach ($entities as $entity) {
$entity->getProperty($this->metadata->relationship->property)->trackEntity($this->parent);
$entity->getProperty($this->metadataRelationship->property)->trackEntity($this->parent);
$this->trackEntity($entity);
}
});
Expand All @@ -85,11 +85,11 @@ protected function createCollection(): ICollection

protected function updateRelationshipAdd(IEntity $entity): void
{
if (!$this->metadata->relationship->property) {
if (!$this->metadataRelationship->property) {
return;
}

$otherSide = $entity->getProperty($this->metadata->relationship->property);
$otherSide = $entity->getProperty($this->metadataRelationship->property);
assert($otherSide instanceof ManyHasMany);
$otherSide->collection = null;
$otherSide->toAdd[spl_object_hash($this->parent)] = $this->parent;
Expand All @@ -99,11 +99,11 @@ protected function updateRelationshipAdd(IEntity $entity): void

protected function updateRelationshipRemove(IEntity $entity): void
{
if (!$this->metadata->relationship->property) {
if (!$this->metadataRelationship->property) {
return;
}

$otherSide = $entity->getProperty($this->metadata->relationship->property);
$otherSide = $entity->getProperty($this->metadataRelationship->property);
assert($otherSide instanceof ManyHasMany);
$otherSide->collection = null;
$otherSide->toRemove[spl_object_hash($this->parent)] = $this->parent;
Expand Down
4 changes: 2 additions & 2 deletions src/Relationships/ManyHasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function modify(): void

protected function updateRelationship(?IEntity $oldEntity, ?IEntity $newEntity, bool $allowNull): void
{
$key = $this->metadata->relationship->property;
$key = $this->metadataRelationship->property;
if (!$key) {
return;
}
Expand All @@ -49,7 +49,7 @@ protected function updateRelationship(?IEntity $oldEntity, ?IEntity $newEntity,

protected function initReverseRelationship(?IEntity $entity)
{
$key = $this->metadata->relationship->property;
$key = $this->metadataRelationship->property;
if (!$key || !$entity) {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Relationships/OneHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,24 @@ protected function createCollection(): ICollection

protected function updateRelationshipAdd(IEntity $entity): void
{
if (!$this->metadata->relationship->property) {
if (!$this->metadataRelationship->property) {
return;
}

$this->updatingReverseRelationship = true;
$entity->getProperty($this->metadata->relationship->property)->setInjectedValue($entity, $this->parent);
$entity->getProperty($this->metadataRelationship->property)->setInjectedValue($entity, $this->parent);
$this->updatingReverseRelationship = false;
}


protected function updateRelationshipRemove(IEntity $entity): void
{
if (!$this->metadata->relationship->property) {
if (!$this->metadataRelationship->property) {
return;
}

$this->updatingReverseRelationship = true;
$entity->getProperty($this->metadata->relationship->property)->setInjectedValue($entity, null);
$entity->getProperty($this->metadataRelationship->property)->setInjectedValue($entity, null);
$this->updatingReverseRelationship = false;
}
}
8 changes: 4 additions & 4 deletions src/Relationships/OneHasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function createCollection(): ICollection

public function getRawValue()
{
if ($this->primaryValue === null && $this->value === false && !$this->metadata->relationship->isMain) {
if ($this->primaryValue === null && $this->value === false && !$this->metadataRelationship->isMain) {
$this->getEntity(); // init the value
}
return parent::getRawValue();
Expand All @@ -33,15 +33,15 @@ public function getRawValue()
protected function modify(): void
{
$this->isModified = true;
if ($this->metadata->relationship->isMain) {
if ($this->metadataRelationship->isMain) {
$this->parent->setAsModified($this->metadata->name);
}
}


protected function updateRelationship(?IEntity $oldEntity, ?IEntity $newEntity, bool $allowNull): void
{
$key = $this->metadata->relationship->property;
$key = $this->metadataRelationship->property;
if (!$key) {
return;
}
Expand All @@ -59,7 +59,7 @@ protected function updateRelationship(?IEntity $oldEntity, ?IEntity $newEntity,

protected function initReverseRelationship(?IEntity $entity)
{
$key = $this->metadata->relationship->property;
$key = $this->metadataRelationship->property;
if (!$key || !$entity) {
return;
}
Expand Down

0 comments on commit 6c25f64

Please sign in to comment.