Skip to content

Commit

Permalink
Merge branch 'phpstan11'
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Feb 4, 2019
2 parents 7114bac + 6c25f64 commit e37c87e
Show file tree
Hide file tree
Showing 20 changed files with 87 additions and 51 deletions.
5 changes: 0 additions & 5 deletions .phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +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\.#'
# we need a local mute
- '#Cannot call method \w+\(\) on Nextras\\Dbal\\Result\\Result\|null\.#'
- '#Cannot call method \w+\(\) on Nextras\\Dbal\\Result\\Row\|null\.#'
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ before_script:

# Install Nette Tester
- phpenv config-rm xdebug.ini || true
- if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest --no-interaction; fi
- if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest --prefer-stable --no-interaction; fi
- if [ "$dependencies" = "highest" ]; then composer update --no-interaction; fi

script:
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"nette/caching": "~2.5 || ~3.0@rc",
"nette/utils": "~2.5 || ~3.0@rc",
"nette/tokenizer": "~2.3 || ~3.0@rc",
"nextras/dbal": "~3.0"
"nextras/dbal": "~3.0 || ~3.1@dev"
},
"require-dev": {
"nette/bootstrap": "~2.4 || ~3.0@rc",
Expand All @@ -33,8 +33,8 @@
"nette/tester": "~2.1",
"marc-mabe/php-enum": "~3.0",
"mockery/mockery": "~1.2",
"phpstan/phpstan-shim": "0.10.7",
"phpstan/phpstan-nette": "0.10.1",
"phpstan/phpstan-shim": "0.11.1",
"phpstan/phpstan-nette": "0.11",
"tracy/tracy": "~2.3"
},
"autoload": {
Expand Down
7 changes: 5 additions & 2 deletions src/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,11 @@ public function onLoad(array $data)
}


public function onRefresh(array $data, bool $isPartial = false)
public function onRefresh(?array $data, bool $isPartial = false)
{
if ($data === null) {
throw new InvalidStateException('Refetching data failed. Entity is not present in storage anymore.');
}
if ($isPartial) {
foreach ($data as $name => $value) {
$this->data[$name] = $value;
Expand Down Expand Up @@ -448,7 +451,7 @@ private function &internalGetValue(PropertyMetadata $metadata, string $name)
} else {
$value = $this->data[$name];
}
if (!isset($value) && !$metadata->isNullable) {
if ($value === null && !$metadata->isNullable) {
$class = get_class($this);
throw new InvalidStateException("Property {$class}::\${$name} is not set.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/IEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function onLoad(array $data);


/** @internal */
public function onRefresh(array $data, bool $isPartial = false);
public function onRefresh(?array $data, bool $isPartial = false);


/** @internal */
Expand Down
16 changes: 12 additions & 4 deletions src/Mapper/Dbal/DbalMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,12 @@ protected function processPostgreAutoupdate(IEntity $entity, array $args)
$args[] = 'RETURNING %ex';
$args[] = $this->getAutoupdateReselectExpression();
$row = $this->connection->queryArgs($args)->fetch();
$data = $this->getStorageReflection()->convertStorageToEntity($row->toArray());
$entity->onRefresh($data, true);
if ($row === null) {
$entity->onRefresh(null, true);
} else {
$data = $this->getStorageReflection()->convertStorageToEntity($row->toArray());
$entity->onRefresh($data, true);
}
}


Expand All @@ -342,8 +346,12 @@ protected function processMySQLAutoupdate(IEntity $entity, array $args)
$this->getTableName(),
$primary
)->fetch();
$data = $this->getStorageReflection()->convertStorageToEntity($row->toArray());
$entity->onRefresh($data, true);
if ($row === null) {
$entity->onRefresh(null, true);
} else {
$data = $this->getStorageReflection()->convertStorageToEntity($row->toArray());
$entity->onRefresh($data, true);
}
}


Expand Down
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
5 changes: 3 additions & 2 deletions src/Mapper/Dbal/StorageReflection/StorageReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,22 @@ public function addModifier(string $storageKey, string $saveModifier): StorageRe

protected function findManyHasManyPrimaryColumns($joinTable, $sourceTable, $targetTable): array
{
$sourceId = $targetId = null;
$useFQN = strpos($sourceTable, '.') !== false;
$keys = $this->platform->getForeignKeys($joinTable);
foreach ($keys as $column => $meta) {
$table = $useFQN
? $meta['ref_table']
: preg_replace('#^(.*\.)?(.*)$#', '$2', $meta['ref_table']);

if ($table === $sourceTable && !isset($sourceId)) {
if ($table === $sourceTable && $sourceId === null) {
$sourceId = $column;
} elseif ($table === $targetTable) {
$targetId = $column;
}
}

if (!isset($sourceId, $targetId)) {
if ($sourceId === null || $targetId === null) {
throw new InvalidStateException("No primary keys detected for many has many '{$joinTable}' join table.");
}

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
9 changes: 5 additions & 4 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 All @@ -39,11 +40,9 @@ public function clearCache()
}


/**
* @return EntityIterator
*/
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 All @@ -52,6 +51,8 @@ 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);
}
}
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
Loading

0 comments on commit e37c87e

Please sign in to comment.