type fixes

This commit is contained in:
Yuri Kuznetsov
2022-02-21 12:45:42 +02:00
parent d499719c08
commit 32abc4b554
6 changed files with 123 additions and 39 deletions
+12 -1
View File
@@ -70,12 +70,20 @@ class Util
return in_array(strtoupper($argument), ['NULL', 'TRUE', 'FALSE']);
}
/**
* @param string $expression
* @return string[]
*/
public static function getAllAttributesFromComplexExpression(string $expression): array
{
return self::getAllAttributesFromComplexExpressionImplementation($expression);
}
protected static function getAllAttributesFromComplexExpressionImplementation(
/**
* @param string[]|null $list
* @return string[]
*/
private static function getAllAttributesFromComplexExpressionImplementation(
string $expression,
?array &$list = null
): array {
@@ -113,6 +121,9 @@ class Util
return $list;
}
/**
* @return string[]
*/
static public function parseArgumentListFromFunctionContent(string $functionContent): array
{
$functionContent = trim($functionContent);
@@ -29,21 +29,35 @@
namespace Espo\ORM\Repository;
use Espo\ORM\{
Entity,
Query\Select,
};
use Espo\ORM\Entity;
use Espo\ORM\Query\Select;
interface HookMediator
{
/**
* @param array<string,mixed> $options
*/
public function beforeSave(Entity $entity, array $options): void;
/**
* @param array<string,mixed> $options
*/
public function afterSave(Entity $entity, array $options): void;
/**
* @param array<string,mixed> $options
*/
public function beforeRemove(Entity $entity, array $options): void;
/**
* @param array<string,mixed> $options
*/
public function afterRemove(Entity $entity, array $options): void;
/**
* @param array<string,mixed>|null $columnData Role values.
* @param array<string,mixed> $options
*/
public function beforeRelate(
Entity $entity,
string $relationName,
@@ -52,6 +66,10 @@ interface HookMediator
array $options
): void;
/**
* @param array<string,mixed>|null $columnData Role values.
* @param array<string,mixed> $options
*/
public function afterRelate(
Entity $entity,
string $relationName,
@@ -60,11 +78,23 @@ interface HookMediator
array $options
): void;
/**
* @param array<string,mixed> $options
*/
public function beforeUnrelate(Entity $entity, string $relationName, Entity $foreignEntity, array $options): void;
/**
* @param array<string,mixed> $options
*/
public function afterUnrelate(Entity $entity, string $relationName, Entity $foreignEntity, array $options): void;
/**
* @param array<string,mixed> $options
*/
public function beforeMassRelate(Entity $entity, string $relationName, Select $query, array $options): void;
/**
* @param array<string,mixed> $options
*/
public function afterMassRelate(Entity $entity, string $relationName, Select $query, array $options): void;
}
+48 -10
View File
@@ -149,9 +149,7 @@ class RDBRelation
/**
* Find related records.
*
* @phpstan-return iterable<Entity>&Collection
*
* @todo Fix phpstan-return after php5.4 to Collection<Entity> or remove.
* @return Collection<Entity>
*/
public function find(): Collection
{
@@ -205,7 +203,7 @@ class RDBRelation
* @param Join|string $target
* A relation name or table. A relation name should be in camelCase, a table in CamelCase.
* @param string|null $alias An alias.
* @param WhereItem|array|null $conditions Join conditions.
* @param WhereItem|array<mixed,mixed>|null $conditions Join conditions.
*/
public function join($target, ?string $alias = null, $conditions = null): Builder
{
@@ -218,7 +216,7 @@ class RDBRelation
* @param Join|string $target
* A relation name or table. A relation name should be in camelCase, a table in CamelCase.
* @param string|null $alias An alias.
* @param WhereItem|array|null $conditions Join conditions.
* @param WhereItem|array<mixed,mixed>|null $conditions Join conditions.
*/
public function leftJoin($target, ?string $alias = null, $conditions = null): Builder
{
@@ -249,8 +247,8 @@ class RDBRelation
* * `where(array $clause)`
* * `where(string $key, string $value)`
*
* @param WhereItem|array|string $clause A key or where clause.
* @param array|string|null $value A value. Omitted if the first argument is not string.
* @param WhereItem|array<mixed,mixed>|string $clause A key or where clause.
* @param mixed[]|scalar|null $value A value. Should be omitted if the first argument is not string.
*/
public function where($clause = [], $value = null): Builder
{
@@ -265,8 +263,8 @@ class RDBRelation
* * `having(array $clause)`
* * `having(string $key, string $value)`
*
* @param WhereItem|array|string $clause A key or where clause.
* @param array|string|null $value A value. Omitted if the first argument is not string.
* @param WhereItem|array<mixed,mixed>|string $clause A key or where clause.
* @param mixed[]|string|null $value A value. Should be omitted if the first argument is not string.
*/
public function having($clause = [], $value = null): Builder
{
@@ -339,6 +337,7 @@ class RDBRelation
/**
* @deprecated Use `group` method.
* @param Expression|Expression[]|string|string[] $groupBy
*/
public function groupBy($groupBy): Builder
{
@@ -351,7 +350,7 @@ class RDBRelation
* Usage example:
* `->columnsWhere(['column' => $value])`
*
* @param WhereItem|array $clause Where clause.
* @param WhereItem|array<mixed,mixed> $clause Where clause.
*/
public function columnsWhere($clause): Builder
{
@@ -435,6 +434,9 @@ class RDBRelation
/**
* Relate with an entity by ID.
*
* @param array<string,mixed>|null $columnData Role values.
* @param array<string,mixed> $options
*/
public function relateById(string $id, ?array $columnData = null, array $options = []): void
{
@@ -458,6 +460,8 @@ class RDBRelation
/**
* Unrelate from an entity by ID.
*
* @param array<string,mixed> $options
*/
public function unrelateById(string $id, array $options = []): void
{
@@ -481,6 +485,8 @@ class RDBRelation
/**
* Update relationship columns by ID. For many-to-many relationships.
*
* @param array<string,mixed> $columnData Role values.
*/
public function updateColumnsById(string $id, array $columnData): void
{
@@ -504,6 +510,9 @@ class RDBRelation
/**
* Relate with an entity.
*
* @param array<string,mixed>|null $columnData Role values.
* @param array<string,mixed> $options
*/
public function relate(Entity $entity, ?array $columnData = null, array $options = []): void
{
@@ -522,6 +531,8 @@ class RDBRelation
/**
* Unrelate from an entity.
*
* @param array<string,mixed> $options
*/
public function unrelate(Entity $entity, array $options = []): void
{
@@ -534,6 +545,11 @@ class RDBRelation
$this->afterUnrelate($entity, $options);
}
/**
* Mass-relate.
*
* @param array<string,mixed> $options
*/
public function massRelate(Select $query, array $options = []): void
{
if ($this->isBelongsToParentType()) {
@@ -553,6 +569,8 @@ class RDBRelation
/**
* Update relationship columns. For many-to-many relationships.
*
* @param array<string,mixed> $columnData Role values.
*/
public function updateColumns(Entity $entity, array $columnData): void
{
@@ -593,31 +611,51 @@ class RDBRelation
return $this->getMapper()->getRelationColumn($this->entity, $this->relationName, $id, $column);
}
/**
* @param array<string,mixed>|null $columnData Role values.
* @param array<string,mixed> $options
*/
private function beforeRelate(Entity $entity, ?array $columnData, array $options): void
{
$this->hookMediator->beforeRelate($this->entity, $this->relationName, $entity, $columnData, $options);
}
/**
* @param array<string,mixed>|null $columnData Role values.
* @param array<string,mixed> $options
*/
private function afterRelate(Entity $entity, ?array $columnData, array $options): void
{
$this->hookMediator->afterRelate($this->entity, $this->relationName, $entity, $columnData, $options);
}
/**
* @param array<string,mixed> $options
*/
private function beforeUnrelate(Entity $entity, array $options): void
{
$this->hookMediator->beforeUnrelate($this->entity, $this->relationName, $entity, $options);
}
/**
* @param array<string,mixed> $options
*/
private function afterUnrelate(Entity $entity, array $options): void
{
$this->hookMediator->afterUnrelate($this->entity, $this->relationName, $entity, $options);
}
/**
* @param array<string,mixed> $options
*/
private function beforeMassRelate(Select $query, array $options): void
{
$this->hookMediator->beforeMassRelate($this->entity, $this->relationName, $query, $options);
}
/**
* @param array<string,mixed> $options
*/
private function afterMassRelate(Select $query, array $options): void
{
$this->hookMediator->afterMassRelate($this->entity, $this->relationName, $query, $options);
@@ -51,24 +51,21 @@ use InvalidArgumentException;
*/
class RDBRelationSelectBuilder
{
private $entityManager;
private EntityManager $entityManager;
private $entity;
private Entity $entity;
private $foreignEntityType;
private string $foreignEntityType;
private $relationName;
private string $relationName;
private $relationType = null;
private ?string $relationType = null;
/**
* @var SelectBuilder
*/
private $builder = null;
private SelectBuilder $builder;
private $middleTableAlias = null;
private ?string $middleTableAlias = null;
private $returnSthCollection = false;
private bool $returnSthCollection = false;
public function __construct(
EntityManager $entityManager,
@@ -135,7 +132,7 @@ class RDBRelationSelectBuilder
* Usage example:
* `->columnsWhere(['column' => $value])`
*
* @param WhereItem|array $clause Where clause.
* @param WhereItem|array<mixed,mixed> $clause Where clause.
*/
public function columnsWhere($clause): self
{
@@ -158,6 +155,10 @@ class RDBRelationSelectBuilder
return $this;
}
/**
* @param array<mixed,mixed> $where
* @return array<mixed,mixed>
*/
protected function applyMiddleAliasToWhere(array $where): array
{
$transformedWhere = [];
@@ -200,14 +201,12 @@ class RDBRelationSelectBuilder
{
$query = $this->builder->build();
/** @var (iterable<Entity>&\Espo\ORM\SthCollection)|Entity */
$related = $this->getMapper()->selectRelated($this->entity, $this->relationName, $query);
if ($related instanceof Collection) {
return $this->handleReturnCollection($related);
}
/** @var iterable<Entity>&\Espo\ORM\EntityCollection $collection */
$collection = $this->entityManager->getCollectionFactory()->create($this->foreignEntityType);
$collection->setAsFetched();
@@ -249,7 +248,7 @@ class RDBRelationSelectBuilder
* @param Join|string $target
* A relation name or table. A relation name should be in camelCase, a table in CamelCase.
* @param string|null $alias An alias.
* @param WhereItem|array|null $conditions Join conditions.
* @param WhereItem|array<mixed,mixed>|null $conditions Join conditions.
*/
public function join($target, ?string $alias = null, $conditions = null): self
{
@@ -264,7 +263,7 @@ class RDBRelationSelectBuilder
* @param Join|string $target
* A relation name or table. A relation name should be in camelCase, a table in CamelCase.
* @param string|null $alias An alias.
* @param WhereItem|array|null $conditions Join conditions.
* @param WhereItem|array<mixed,mixed>|null $conditions Join conditions.
*/
public function leftJoin($target, ?string $alias = null, $conditions = null): self
{
@@ -301,8 +300,8 @@ class RDBRelationSelectBuilder
* * `where(array $clause)`
* * `where(string $key, string $value)`
*
* @param WhereItem|array|string $clause A key or where clause.
* @param array|string|null $value A value. Omitted if the first argument is not string.
* @param WhereItem|array<mixed,mixed>|string $clause A key or where clause.
* @param mixed[]|scalar|null $value A value. Should be omitted if the first argument is not string.
*/
public function where($clause = [], $value = null): self
{
@@ -331,8 +330,8 @@ class RDBRelationSelectBuilder
* * `having(array $clause)`
* * `having(string $key, string $value)`
*
* @param WhereItem|array|string $clause A key or where clause.
* @param array|string|null $value A value. Omitted if the first argument is not string.
* @param WhereItem|array<mixed,mixed>|string $clause A key or where clause.
* @param mixed[]|string|null $value A value. Should be omitted if the first argument is not string.
*/
public function having($clause = [], $value = null): self
{
@@ -415,6 +414,7 @@ class RDBRelationSelectBuilder
/**
* @deprecated Use `group` method.
* @param Expression|Expression[]|string|string[] $groupBy
*/
public function groupBy($groupBy): self
{
@@ -451,6 +451,10 @@ class RDBRelationSelectBuilder
return str_replace('@relation.', $alias . '.', $item);
}
/**
* @param array<mixed,mixed> $where
* @return array<mixed,mixed>
*/
protected function applyRelationAliasToWhereClause(array $where): array
{
if (!$this->isManyMany()) {
@@ -487,6 +491,7 @@ class RDBRelationSelectBuilder
}
/**
* @param Collection<Entity> $collection
* @phpstan-return Collection<Entity>
*/
protected function handleReturnCollection(Collection $collection): Collection
@@ -873,7 +873,7 @@ class RDBRepository implements Repository
* * `where(string $key, string $value)`
*
* @param WhereItem|array<scalar,mixed>|string $clause A key or where clause.
* @param mixed[]|scalar|null $value A value. Omitted if the first argument is not string.
* @param mixed[]|scalar|null $value A value. Should be omitted if the first argument is not string.
*
* @return RDBSelectBuilder<TEntity>
*/
@@ -891,7 +891,7 @@ class RDBRepository implements Repository
* * `having(string $key, string $value)`
*
* @param WhereItem|array<scalar,mixed>|string $clause A key or where clause.
* @param mixed[]|scalar|null $value A value. Omitted if the first argument is not string.
* @param mixed[]|scalar|null $value A value. Should be omitted if the first argument is not string.
*
* @phpstan-return RDBSelectBuilder<TEntity>
*/
@@ -282,7 +282,7 @@ class RDBSelectBuilder
* * `where(string $key, string $value)`
*
* @param WhereItem|array<mixed,mixed>|string $clause A key or where clause.
* @param mixed[]|scalar|null $value A value. Omitted if the first argument is not string.
* @param mixed[]|scalar|null $value A value. Should be omitted if the first argument is not string.
*
* @phpstan-return RDBSelectBuilder<TEntity>
*/
@@ -302,7 +302,7 @@ class RDBSelectBuilder
* * `having(string $key, string $value)`
*
* @param WhereItem|array<mixed,mixed>|string $clause A key or where clause.
* @param mixed[]|scalar|null $value A value. Omitted if the first argument is not string.
* @param mixed[]|scalar|null $value A value. Should be omitted if the first argument is not string.
*
* @phpstan-return RDBSelectBuilder<TEntity>
*/