Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria committed Jun 9, 2024
1 parent e83d5c5 commit de43702
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
5 changes: 4 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ parameters:
- '#Interface must be located in "Contract" or "Contracts" namespace#'
- '#Dynamic call to static method Kalnoy\\Nestedset\\QueryBuilder<.*>::select\(\).#'
- '#Dynamic call to static method Kalnoy\\Nestedset\\QueryBuilder<.*>::from\(\).#'
- '#Dynamic call to static method Kalnoy\\Nestedset\\QueryBuilder<.*>::whereRaw\(\).#'
# - '#Dynamic call to static method Kalnoy\\Nestedset\\QueryBuilder<.*>::whereRaw\(\).#'
- '#Dynamic call to static method Kalnoy\\Nestedset\\QueryBuilder<.*>::whereNested\(\).#'
- '#Dynamic call to static method Kalnoy\\Nestedset\\QueryBuilder<.*>::whereIn\(\).#'
- '#Parameter .* \$models .* of method .*::initRelation\(\) should be contravariant with parameter \$models .* of method .*::initRelation\(\)#'
- '#Parameter .* \$models .* of method .*::addEagerConstraints\(\) should be contravariant with parameter \$models .* of method .*::addEagerConstraints\(\)#'
- '#Parameter .* \$models .* of method .*::match\(\) should be contravariant with parameter \$models .* of method .*::match\(\)#'
- '#Parameter .* \$query .* of method .*::getRelationExistenceQuery\(\) should be contravariant with parameter \$query .* of method .*::getRelationExistenceQuery\(\)#'
- '#Parameter .* \$parentQuery .* of method .*::getRelationExistenceQuery\(\) should be contravariant with parameter \$parentQuery .* of method .*::getRelationExistenceQuery\(\)#'
- '#Parameter .* \$results .* of method .*::match\(\) should be contravariant with parameter \$results .* of method .*::match\(\)#'
27 changes: 14 additions & 13 deletions src/BaseRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ abstract protected function relationExistenceCondition(string $hash, string $tab

/**
* @param EloquentBuilder<NodeModel> $query
* @param EloquentBuilder<NodeModel> $parent
* @param EloquentBuilder<NodeModel> $parentQuery
* @param mixed $columns
*
* @return EloquentBuilder<NodeModel>
* @return QueryBuilder<Tmodelkey,Tmodel>
*/
public function getRelationExistenceQuery(EloquentBuilder $query, EloquentBuilder $parent,
public function getRelationExistenceQuery(EloquentBuilder $query, EloquentBuilder $parentQuery,
$columns = ['*']
) {
$query = $this->getParent()->replicate()->newScopedQuery()->select($columns);
Expand All @@ -107,7 +107,7 @@ public function getRelationExistenceQuery(EloquentBuilder $query, EloquentBuilde
$grammar->wrap($this->parent->getLftName()),
$grammar->wrap($this->parent->getRgtName()));

return $query->whereRaw($condition);
return $query->whereRaw($condition); /** @phpstan-ignore-line */
}

/**
Expand Down Expand Up @@ -148,7 +148,7 @@ public function getResults()
/**
* Set the constraints for an eager load of the relation.
*
* @param array<int,Tmodel> $models
* @param array<int,NodeModel> $models
*
* @return void
*/
Expand All @@ -173,15 +173,16 @@ public function addEagerConstraints(array $models)
/**
* Match the eagerly loaded results to their parents.
*
* @param array<int,Tmodel> $models
* @param EloquentCollection $results
* @param array<int,NodeModel> $models
* @param EloquentCollection<int,NodeModel> $results
* @param string $relation
*
* @return array<int,Tmodel>
* @return array<int,NodeModel>
*/
public function match(array $models, EloquentCollection $results, $relation)
{
foreach ($models as $model) {
/** @disregard P1006 */
$related = $this->matchForModel($model, $results);

$model->setRelation($relation, $related);
Expand All @@ -191,18 +192,18 @@ public function match(array $models, EloquentCollection $results, $relation)
}

/**
* @param Tmodel $model
* @param EloquentCollection $results
* @param NodeModel $model
* @param EloquentCollection<int,NodeModel> $results
*
* @return Collection
* @return Collection<int,Tmodelkey,Tmodel>
*/
protected function matchForModel(Model $model, EloquentCollection $results)
{
/** @var Collection<int,Tmodel> */
/** @var Collection<int,Tmodelkey,Tmodel> */
$result = $this->related->newCollection();

/** @var Tmodel $related */
foreach ($results as $related) {
/** @disregard P1006 */
if ($this->matches($model, $related)) {
$result->push($related);
}
Expand Down
17 changes: 9 additions & 8 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function toTree($root = false)
/**
* @param mixed $root
*
* @return int|string
* @return Tmodelkey
*/
protected function getRootNodeId($root = false)
{
Expand Down Expand Up @@ -129,31 +129,32 @@ protected function getRootNodeId($root = false)
*/
public function toFlatTree($root = false): Collection
{
/** @Var Collection<TKey,Tmodelkey,Tmodel> */
$result = new Collection();

if ($this->isEmpty()) {
return $result;
return $result; /** @phpstan-ignore-line */
}

/** @var Node */
/** @var NodeModel */
$first = $this->first();
/** @var Collection<int|string,TModel> */
/** @var Collection<TKey,Tmodelkey,NodeModel> */
$groupedNodes = $this->groupBy($first->getParentIdName());

return $result->flattenTree($groupedNodes, $this->getRootNodeId($root));
return $result->flattenTree($groupedNodes, $this->getRootNodeId($root)); /** @phpstan-ignore-line */
}

/**
* Flatten a tree into a non recursive array.
*
* @param Collection<int|string,Tmodelkey,Tmodel> $groupedNodes
* @param int|string $parentId
* @param Collection<TKey,Tmodelkey,Tmodel> $groupedNodes
* @param Tmodelkey $parentId
*
* @return Collection<TKey,Tmodelkey,Tmodel>
*/
protected function flattenTree(Collection $groupedNodes, $parentId): Collection
{
/** @var array<int,TModel> */
/** @var array<int,NodeModel> */
$nodes = $groupedNodes->get($parentId, []);
foreach ($nodes as $node) {
$this->push($node);
Expand Down
8 changes: 4 additions & 4 deletions src/DescendantsRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public function addConstraints()
}

/**
* @param QueryBuilder $query
* @param Model $model
* @param QueryBuilder<Tmodelkey,Tmodel> $query
* @param NodeModel $model
*/
protected function addEagerConstraint($query, $model)
{
$query->orWhereDescendantOf($model);
}

/**
* @param Model&Node $model
* @param Node $related
* @param NodeModel $model
* @param NodeModel $related
*
* @return bool
*/
Expand Down
10 changes: 5 additions & 5 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class QueryBuilder extends Builder
*
* @since 2.0
*
* @param Tmodelkey $id
* @param Tmodelkey|NodeModel $id
* @param bool $required
*
* @return array<int,int>
Expand All @@ -57,7 +57,7 @@ public function getNodeData(mixed $id, $required = false)
*
* @since 2.0
*
* @param Tmodelkey $id
* @param Tmodelkey|NodeModel $id
* @param bool $required
*
* @return array<int,int>
Expand Down Expand Up @@ -212,7 +212,7 @@ public function orWhereNodeBetween(array $values)
*
* @since 2.0
*
* @param Tmodelkey $id
* @param Tmodelkey|NodeModel $id
* @param string $boolean
* @param bool $not
* @param bool $andSelf
Expand All @@ -238,7 +238,7 @@ public function whereDescendantOf(mixed $id, $boolean = 'and', $not = false,
}

/**
* @param Tmodelkey $id
* @param Tmodelkey|NodeModel $id
*
* @return QueryBuilder<Tmodelkey,Tmodel>
*/
Expand All @@ -248,7 +248,7 @@ public function whereNotDescendantOf(mixed $id)
}

/**
* @param Tmodelkey $id
* @param Tmodelkey|NodeModel $id
*
* @return QueryBuilder<Tmodelkey,Tmodel>
*/
Expand Down

0 comments on commit de43702

Please sign in to comment.