Skip to content

Commit

Permalink
more annottations
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria committed Jun 9, 2024
1 parent 11f72d1 commit e83d5c5
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 68 deletions.
15 changes: 11 additions & 4 deletions src/AncestorsRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
use Illuminate\Database\Eloquent\Model;

/**
* @template Tmodelkey
* @template Tmodel of Model
*
* @phpstan-type NodeModel Node<Tmodelkey,Tmodel>&Tmodel
*
* @disregard P1037
*
* @extends BaseRelation<Tmodelkey,Tmodel>
*/
class AncestorsRelation extends BaseRelation
{
Expand All @@ -25,8 +32,8 @@ public function addConstraints()
}

/**
* @param Model&Node $model
* @param Node $related
* @param NodeModel $model
* @param NodeModel $related
*
* @return bool
*/
Expand All @@ -36,8 +43,8 @@ protected function matches(Model $model, $related): bool
}

/**
* @param QueryBuilder $query
* @param Model $model
* @param QueryBuilder<Tmodelkey,Tmodel> $query
* @param NodeModel $model
*
* @return void
*/
Expand Down
36 changes: 19 additions & 17 deletions src/BaseRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@

/**
* @template Tmodelkey
* @template Tmodel of Model&Node<Tmodelkey,Model>
* @template Tmodel of Model
*
* @extends Relation<Tmodel>
* @phpstan-type NodeModel Node<Tmodelkey,Tmodel>&Tmodel
*
* @extends Relation<NodeModel>
*
* @property Tmodel $related
* @property Tmodel $parent
* @property NodeModel $related
* @property NodeModel $parent
*
* @method Tmodel getParent()
* @method NodeModel getParent()
*/
abstract class BaseRelation extends Relation
{
/**
* @var QueryBuilder
* @var QueryBuilder<Tmodelkey,Tmodel>
*/
protected $query;

/**
* @var Node&Model
* @var NodeModel
*/
protected $parent;

Expand All @@ -41,29 +43,29 @@ abstract class BaseRelation extends Relation
/**
* AncestorsRelation constructor.
*
* @param QueryBuilder $builder
* @param Model $model
* @param QueryBuilder<Tmodelkey,Tmodel> $builder
* @param NodeModel $model
*/
public function __construct(QueryBuilder $builder, Model $model)
{
if (!NestedSet::isNode($model)) {
throw new \InvalidArgumentException('Model must be node.');
}

/** @disregard P1006 */
parent::__construct($builder, $model);
}

/**
* @param Model&Node $model
* @param Node $related
* @param NodeModel $model
* @param NodeModel $related
*
* @return bool
*/
abstract protected function matches(Model&Node $model, Node $related): bool;

/**
* @param QueryBuilder $query
* @param Model $model
* @param QueryBuilder<Tmodelkey,Tmodel> $query
* @param NodeModel $model
*
* @return void
*/
Expand All @@ -80,11 +82,11 @@ abstract protected function addEagerConstraint($query, $model);
abstract protected function relationExistenceCondition(string $hash, string $table, string $lft, string $rgt): string;

/**
* @param EloquentBuilder $query
* @param EloquentBuilder $parent
* @param EloquentBuilder<NodeModel> $query
* @param EloquentBuilder<NodeModel> $parent
* @param mixed $columns
*
* @return EloquentBuilder
* @return EloquentBuilder<NodeModel>
*/
public function getRelationExistenceQuery(EloquentBuilder $query, EloquentBuilder $parent,
$columns = ['*']
Expand Down
25 changes: 14 additions & 11 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

/**
* @template TKey of array-key
* @template TModel of Model&Node
* @template Tmodelkey
* @template Tmodel of Model
*
* @extends EloquentCollection<TKey, TModel>
* @phpstan-type NodeModel Node<Tmodelkey,Tmodel>&Tmodel
*
* @extends EloquentCollection<TKey,NodeModel>
*/
final class Collection extends EloquentCollection
{
Expand All @@ -26,17 +29,17 @@ public function linkNodes()
return $this;
}

/** @var Node */
/** @var NodeModel */
$first = $this->first();
$groupedNodes = $this->groupBy($first->getParentIdName());

/** @var Node&Model $node */
/** @var NodeModel $node */
foreach ($this->items as $node) {
if ($node->getParentId() === null) {
$node->setRelation('parent', null);
}

/** @var array<int,Model&Node> */
/** @var array<int,NodeModel> */
$children = $groupedNodes->get($node->getKey(), []);

foreach ($children as $child) {
Expand All @@ -58,7 +61,7 @@ public function linkNodes()
*
* @param mixed $root
*
* @return Collection
* @return Collection<TKey,Tmodelkey,Tmodel>
*/
public function toTree($root = false)
{
Expand All @@ -72,7 +75,7 @@ public function toTree($root = false)

$root = $this->getRootNodeId($root);

/** @var Model&Node $node */
/** @var NodeModel $node */
foreach ($this->items as $node) {
if ($node->getParentId() === $root) {
$items[] = $node;
Expand Down Expand Up @@ -101,7 +104,7 @@ protected function getRootNodeId($root = false)
// least lft value as root node id.
$leastValue = null;

/** @var Model&Node $node */
/** @var NodeModel $node */
foreach ($this->items as $node) {
if ($leastValue === null || $node->getLft() < $leastValue) {
$leastValue = $node->getLft();
Expand All @@ -122,7 +125,7 @@ protected function getRootNodeId($root = false)
*
* @param bool $root
*
* @return Collection
* @return Collection<TKey,Tmodelkey,Tmodel>
*/
public function toFlatTree($root = false): Collection
{
Expand All @@ -143,10 +146,10 @@ public function toFlatTree($root = false): Collection
/**
* Flatten a tree into a non recursive array.
*
* @param Collection<int|string,TModel> $groupedNodes
* @param Collection<int|string,Tmodelkey,Tmodel> $groupedNodes
* @param int|string $parentId
*
* @return Collection
* @return Collection<TKey,Tmodelkey,Tmodel>
*/
protected function flattenTree(Collection $groupedNodes, $parentId): Collection
{
Expand Down
7 changes: 7 additions & 0 deletions src/DescendantsRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
use Illuminate\Database\Eloquent\Model;

/**
* @template Tmodelkey
* @template Tmodel of Model
*
* @phpstan-type NodeModel Node<Tmodelkey,Tmodel>&Tmodel
*
* @disregard P1037
*
* @extends BaseRelation<Tmodelkey,Tmodel>
*/
class DescendantsRelation extends BaseRelation
{
Expand Down
Loading

0 comments on commit e83d5c5

Please sign in to comment.