From 56448952948c57e51597c281a4c3e3ba2fa676a8 Mon Sep 17 00:00:00 2001 From: ildyria Date: Sat, 8 Jun 2024 19:48:12 +0200 Subject: [PATCH] phpstan typing progress --- phpstan.neon | 3 +- src/AncestorsRelation.php | 12 ++- src/BaseRelation.php | 14 +-- src/Collection.php | 14 +-- src/DescendantsRelation.php | 9 +- src/NestedSet.php | 10 +-- src/Node.php | 137 +++++++++++++--------------- src/NodeTrait.php | 175 +++++++++++++++--------------------- src/QueryBuilder.php | 16 ++-- 9 files changed, 173 insertions(+), 217 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 6ced14f..7656bd8 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -8,4 +8,5 @@ parameters: excludePaths: stubFiles: ignoreErrors: - - identifier: missingType.generics \ No newline at end of file + - identifier: missingType.generics + - '#Interface must be located in "Contract" or "Contracts" namespace#' \ No newline at end of file diff --git a/src/AncestorsRelation.php b/src/AncestorsRelation.php index 9778fb0..68dbeaf 100644 --- a/src/AncestorsRelation.php +++ b/src/AncestorsRelation.php @@ -4,6 +4,9 @@ use Illuminate\Database\Eloquent\Model; +/** + * @template T + */ class AncestorsRelation extends BaseRelation { /** @@ -44,14 +47,9 @@ protected function addEagerConstraint($query, $model) } /** - * @param $hash - * @param $table - * @param $lft - * @param $rgt - * - * @return string + * {@inheritdoc} */ - protected function relationExistenceCondition($hash, $table, $lft, $rgt) + protected function relationExistenceCondition(string $hash, string $table, string $lft, string $rgt): string { $key = $this->getBaseQuery()->getGrammar()->wrap($this->parent->getKeyName()); diff --git a/src/BaseRelation.php b/src/BaseRelation.php index b9ea115..141a44f 100644 --- a/src/BaseRelation.php +++ b/src/BaseRelation.php @@ -16,7 +16,7 @@ abstract class BaseRelation extends Relation protected $query; /** - * @var NodeTrait|Model + * @var Node&Model */ protected $parent; @@ -59,19 +59,19 @@ abstract protected function matches(Model $model, $related); abstract protected function addEagerConstraint($query, $model); /** - * @param $hash - * @param $table - * @param $lft - * @param $rgt + * @param string $hash + * @param string $table + * @param string $lft + * @param string $rgt * * @return string */ - abstract protected function relationExistenceCondition($hash, $table, $lft, $rgt); + abstract protected function relationExistenceCondition(string $hash, string $table, string $lft, string $rgt): string; /** * @param EloquentBuilder $query * @param EloquentBuilder $parent - * @param array $columns + * @param string[] $columns * * @return mixed */ diff --git a/src/Collection.php b/src/Collection.php index 5b2b20e..cfc563f 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -22,15 +22,15 @@ public function linkNodes() $groupedNodes = $this->groupBy($this->first()->getParentIdName()); - /** @var NodeTrait|Model $node */ + /** @var Node&Model $node */ foreach ($this->items as $node) { - if (!$node->getParentId()) { + if ($node->getParentId() === null) { $node->setRelation('parent', null); } $children = $groupedNodes->get($node->getKey(), []); - /** @var Model|NodeTrait $child */ + /** @var Model&Node $child */ foreach ($children as $child) { $child->setRelation('parent', $node); } @@ -64,9 +64,9 @@ public function toTree($root = false) $root = $this->getRootNodeId($root); - /** @var Model|NodeTrait $node */ + /** @var Model&Node $node */ foreach ($this->items as $node) { - if ($node->getParentId() == $root) { + if ($node->getParentId() === $root) { $items[] = $node; } } @@ -77,7 +77,7 @@ public function toTree($root = false) /** * @param mixed $root * - * @return int + * @return int|string|null */ protected function getRootNodeId($root = false) { @@ -93,7 +93,7 @@ protected function getRootNodeId($root = false) // least lft value as root node id. $leastValue = null; - /** @var Model|NodeTrait $node */ + /** @var Model&Node $node */ foreach ($this->items as $node) { if ($leastValue === null || $node->getLft() < $leastValue) { $leastValue = $node->getLft(); diff --git a/src/DescendantsRelation.php b/src/DescendantsRelation.php index 1a3da77..185e73d 100644 --- a/src/DescendantsRelation.php +++ b/src/DescendantsRelation.php @@ -42,14 +42,9 @@ protected function matches(Model $model, $related) } /** - * @param $hash - * @param $table - * @param $lft - * @param $rgt - * - * @return string + * {@inheritdoc} */ - protected function relationExistenceCondition($hash, $table, $lft, $rgt) + protected function relationExistenceCondition(string $hash, string $table, string $lft, string $rgt): string { return "{$hash}.{$lft} between {$table}.{$lft} + 1 and {$table}.{$rgt}"; } diff --git a/src/NestedSet.php b/src/NestedSet.php index 1d330b3..933b444 100644 --- a/src/NestedSet.php +++ b/src/NestedSet.php @@ -36,7 +36,7 @@ class NestedSet * * @param \Illuminate\Database\Schema\Blueprint $table */ - public static function columns(Blueprint $table) + public static function columns(Blueprint $table): void { $table->unsignedInteger(self::LFT)->default(0); $table->unsignedInteger(self::RGT)->default(0); @@ -50,7 +50,7 @@ public static function columns(Blueprint $table) * * @param \Illuminate\Database\Schema\Blueprint $table */ - public static function dropColumns(Blueprint $table) + public static function dropColumns(Blueprint $table): void { $columns = static::getDefaultColumns(); @@ -61,9 +61,9 @@ public static function dropColumns(Blueprint $table) /** * Get a list of default columns. * - * @return array + * @return string[] */ - public static function getDefaultColumns() + public static function getDefaultColumns(): array { return [static::LFT, static::RGT, static::PARENT_ID]; } @@ -75,7 +75,7 @@ public static function getDefaultColumns() * * @return bool */ - public static function isNode($node) + public static function isNode(mixed $node): bool { return $node instanceof Node; } diff --git a/src/Node.php b/src/Node.php index d86be99..5cbde7d 100644 --- a/src/Node.php +++ b/src/Node.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Support\Collection; /** * Accompanies {@link \Kalnoy\Nestedset\NodeTrait}. @@ -25,102 +26,102 @@ interface Node * * @return BelongsTo */ - public function parent(); + public function parent(): BelongsTo; /** * Relation to children. * * @return HasMany */ - public function children(); + public function children(): HasMany; /** * Get query for descendants of the node. * * @return DescendantsRelation */ - public function descendants(); + public function descendants(): DescendantsRelation; /** * Get query for siblings of the node. * * @return QueryBuilder */ - public function siblings(); + public function siblings(): QueryBuilder; /** * Get the node siblings and the node itself. * * @return QueryBuilder */ - public function siblingsAndSelf(); + public function siblingsAndSelf(): QueryBuilder; /** * Get query for the node siblings and the node itself. * - * @param array $columns + * @param string[] $columns * * @return EloquentCollection */ - public function getSiblingsAndSelf(array $columns = ['*']); + public function getSiblingsAndSelf(array $columns = ['*']): EloquentCollection; /** * Get query for siblings after the node. * * @return QueryBuilder */ - public function nextSiblings(); + public function nextSiblings(): QueryBuilder; /** * Get query for siblings before the node. * * @return QueryBuilder */ - public function prevSiblings(); + public function prevSiblings(): QueryBuilder; /** * Get query for nodes after current node. * * @return QueryBuilder */ - public function nextNodes(); + public function nextNodes(): QueryBuilder; /** * Get query for nodes before current node in reversed order. * * @return QueryBuilder */ - public function prevNodes(); + public function prevNodes(): QueryBuilder; /** * Get query ancestors of the node. * * @return AncestorsRelation */ - public function ancestors(); + public function ancestors(): AncestorsRelation; /** * Make this node a root node. * * @return $this */ - public function makeRoot(); + public function makeRoot(): Node; /** * Save node as root. * * @return bool */ - public function saveAsRoot(); + public function saveAsRoot(): bool; /** - * @param $lft - * @param $rgt - * @param $parentId + * @param int $lft + * @param int $rgt + * @param int|string|null $parentId * * @return $this */ - public function rawNode($lft, $rgt, $parentId); + public function rawNode(int $lft, int $rgt, int|string|null $parentId): Node; /** * Move node up given amount of positions. @@ -129,7 +130,7 @@ public function rawNode($lft, $rgt, $parentId); * * @return bool */ - public function up($amount = 1); + public function up(int $amount = 1): bool; /** * Move node down given amount of positions. @@ -138,12 +139,12 @@ public function up($amount = 1); * * @return bool */ - public function down($amount = 1); + public function down(int $amount = 1): bool; /** * @since 2.0 */ - public function newEloquentBuilder($query); + public function newEloquentBuilder(QueryBuilder $query): QueryBuilder; /** * Get a new base query that includes deleted nodes. @@ -152,7 +153,7 @@ public function newEloquentBuilder($query); * * @return QueryBuilder */ - public function newNestedSetQuery($table = null); + public function newNestedSetQuery(?QueryBuilder $table = null): QueryBuilder; /** * @param ?string $table @@ -170,159 +171,147 @@ public function newScopedQuery($table = null); public function applyNestedSetScope($query, $table = null); /** - * @param array $attributes + * @param string[] $attributes * - * @return self + * @return QueryBuilder */ - public static function scoped(array $attributes); + public static function scoped(array $attributes): QueryBuilder; - public function newCollection(array $models = []); + /** + * @template Tmodel of \Illuminate\Database\Eloquent\Model + * + * @param array $models + * + * @return Collection + */ + public function newCollection(array $models = []): Collection; /** * Get node height (rgt - lft + 1). - * - * @return int */ - public function getNodeHeight(); + public function getNodeHeight(): int; /** * Get number of descendant nodes. - * - * @return int */ - public function getDescendantCount(); + public function getDescendantCount(): int; /** * Set the value of model's parent id key. * * Behind the scenes node is appended to found parent node. * - * @param int $value + * @param int|string|null $value * * @throws \Exception If parent node doesn't exists */ - public function setParentIdAttribute($value); + public function setParentIdAttribute(int|string|null $value): void; /** * Get whether node is root. - * - * @return bool */ - public function isRoot(); + public function isRoot(): bool; - /** - * @return bool - */ - public function isLeaf(); + public function isLeaf(): bool; /** * Get the lft key name. - * - * @return string */ - public function getLftName(); + public function getLftName(): string; /** * Get the rgt key name. - * - * @return string */ - public function getRgtName(); + public function getRgtName(): string; /** * Get the parent id key name. - * - * @return string */ - public function getParentIdName(); + public function getParentIdName(): string; /** * Get the value of the model's lft key. - * - * @return int */ - public function getLft(); + public function getLft(): int|null; /** * Get the value of the model's rgt key. - * - * @return int */ - public function getRgt(); + public function getRgt(): int|null; /** * Get the value of the model's parent id key. * - * @return int + * @return int|string|null */ - public function getParentId(); + public function getParentId(): int|string|null; /** * Returns node that is next to current node without constraining to siblings. * * This can be either a next sibling or a next sibling of the parent node. * - * @param array $columns + * @param string[] $columns * * @return self */ - public function getNextNode(array $columns = ['*']); + public function getNextNode(array $columns = ['*']): Node; /** * Returns node that is before current node without constraining to siblings. * * This can be either a prev sibling or parent node. * - * @param array $columns + * @param string[] $columns * * @return self */ - public function getPrevNode(array $columns = ['*']); + public function getPrevNode(array $columns = ['*']): Node; /** - * @param array $columns + * @param string[] $columns * * @return Collection */ public function getAncestors(array $columns = ['*']); /** - * @param array $columns + * @param string[] $columns * * @return Collection|self[] */ public function getDescendants(array $columns = ['*']); /** - * @param array $columns + * @param string[] $columns * * @return Collection|self[] */ public function getSiblings(array $columns = ['*']); /** - * @param array $columns + * @param string[] $columns * * @return Collection */ public function getNextSiblings(array $columns = ['*']); /** - * @param array $columns + * @param string[] $columns * * @return Collection */ public function getPrevSiblings(array $columns = ['*']); /** - * @param array $columns + * @param string[] $columns * * @return Node */ public function getNextSibling(array $columns = ['*']); /** - * @param array $columns + * @param string[] $columns * * @return Node */ @@ -338,26 +327,26 @@ public function getBounds(); * * @return $this */ - public function setLft($value); + public function setLft(int $value): Node; /** * @param $value * * @return $this */ - public function setRgt($value); + public function setRgt(int $value): Node; /** * @param $value * * @return $this */ - public function setParentId($value); + public function setParentId(int|string|null $value): Node; /** - * @param array|null $except + * @param string[]|null $except * * @return $this */ - public function replicate(?array $except = null); + public function replicate(?array $except = null): Node; } diff --git a/src/NodeTrait.php b/src/NodeTrait.php index 2fc6ba3..f23c088 100644 --- a/src/NodeTrait.php +++ b/src/NodeTrait.php @@ -2,6 +2,7 @@ namespace Kalnoy\Nestedset; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -12,34 +13,25 @@ trait NodeTrait { /** * Pending operation. - * - * @var array */ - protected $pending; + protected array|null $pending = null; /** * Whether the node has moved since last save. - * - * @var bool */ - protected $moved = false; + protected bool $moved = false; - /** - * @var \Carbon\Carbon - */ - public static $deletedAt; + public static Carbon $deletedAt; /** * Keep track of the number of performed operations. - * - * @var int */ - public static $actionsPerformed = 0; + public static int $actionsPerformed = 0; /** * Sign on model events. */ - public static function bootNodeTrait() + public static function bootNodeTrait(): void { static::saving(function ($model) { return $model->callPendingAction(); @@ -71,7 +63,7 @@ public static function bootNodeTrait() * * @return $this */ - protected function setNodeAction($action) + protected function setNodeAction($action): self { $this->pending = func_get_args(); @@ -102,9 +94,9 @@ protected function callPendingAction() } /** - * @return bool + * {@inheritdoc} */ - public static function usesSoftDelete() + public static function usesSoftDelete(): bool { static $softDelete; @@ -118,9 +110,9 @@ public static function usesSoftDelete() } /** - * @return bool + * {@inheritdoc} */ - protected function actionRaw() + protected function actionRaw(): bool { return true; } @@ -145,10 +137,8 @@ protected function actionRoot() /** * Get the lower bound. - * - * @return int */ - protected function getLowerBound() + protected function getLowerBound(): int { return (int) $this->newNestedSetQuery()->max($this->getRgtName()); } @@ -226,7 +216,7 @@ public function refreshNode() * * @return BelongsTo */ - public function parent() + public function parent(): BelongsTo { return $this->belongsTo(get_class($this), $this->getParentIdName()) ->setModel($this); @@ -237,7 +227,7 @@ public function parent() * * @return HasMany */ - public function children() + public function children(): HasMany { return $this->hasMany(get_class($this), $this->getParentIdName()) ->setModel($this); @@ -248,7 +238,7 @@ public function children() * * @return DescendantsRelation */ - public function descendants() + public function descendants(): DescendantsRelation { return new DescendantsRelation($this->newQuery(), $this); } @@ -258,7 +248,7 @@ public function descendants() * * @return QueryBuilder */ - public function siblings() + public function siblings(): QueryBuilder { return $this->newScopedQuery() ->where($this->getKeyName(), '<>', $this->getKey()) @@ -270,7 +260,7 @@ public function siblings() * * @return \Kalnoy\Nestedset\QueryBuilder */ - public function siblingsAndSelf() + public function siblingsAndSelf(): QueryBuilder { return $this->newScopedQuery() ->where($this->getParentIdName(), '=', $this->getParentId()); @@ -283,7 +273,7 @@ public function siblingsAndSelf() * * @return \Illuminate\Database\Eloquent\Collection */ - public function getSiblingsAndSelf(array $columns = ['*']) + public function getSiblingsAndSelf(array $columns = ['*']): EloquentCollection { return $this->siblingsAndSelf()->get($columns); } @@ -293,7 +283,7 @@ public function getSiblingsAndSelf(array $columns = ['*']) * * @return QueryBuilder */ - public function nextSiblings() + public function nextSiblings(): QueryBuilder { return $this->nextNodes() ->where($this->getParentIdName(), '=', $this->getParentId()); @@ -304,7 +294,7 @@ public function nextSiblings() * * @return QueryBuilder */ - public function prevSiblings() + public function prevSiblings(): QueryBuilder { return $this->prevNodes() ->where($this->getParentIdName(), '=', $this->getParentId()); @@ -315,7 +305,7 @@ public function prevSiblings() * * @return QueryBuilder */ - public function nextNodes() + public function nextNodes(): QueryBuilder { return $this->newScopedQuery() ->where($this->getLftName(), '>', $this->getLft()); @@ -326,7 +316,7 @@ public function nextNodes() * * @return QueryBuilder */ - public function prevNodes() + public function prevNodes(): QueryBuilder { return $this->newScopedQuery() ->where($this->getLftName(), '<', $this->getLft()); @@ -337,7 +327,7 @@ public function prevNodes() * * @return AncestorsRelation */ - public function ancestors() + public function ancestors(): AncestorsRelation { return new AncestorsRelation($this->newQuery(), $this); } @@ -347,7 +337,7 @@ public function ancestors() * * @return $this */ - public function makeRoot() + public function makeRoot(): Node { $this->setParent(null)->dirtyBounds(); @@ -359,7 +349,7 @@ public function makeRoot() * * @return bool */ - public function saveAsRoot() + public function saveAsRoot(): bool { if ($this->exists && $this->isRoot()) { return $this->save(); @@ -375,7 +365,7 @@ public function saveAsRoot() * * @return bool */ - public function appendNode(self $node) + public function appendNode(self $node): bool { return $node->appendToNode($this)->save(); } @@ -387,7 +377,7 @@ public function appendNode(self $node) * * @return bool */ - public function prependNode(self $node) + public function prependNode(self $node): bool { return $node->prependToNode($this)->save(); } @@ -399,7 +389,7 @@ public function prependNode(self $node) * * @return $this */ - public function appendToNode(self $parent) + public function appendToNode(self $parent): self { return $this->appendOrPrependTo($parent); } @@ -411,7 +401,7 @@ public function appendToNode(self $parent) * * @return $this */ - public function prependToNode(self $parent) + public function prependToNode(self $parent): self { return $this->appendOrPrependTo($parent, true); } @@ -510,13 +500,13 @@ public function insertBeforeNode(self $node) } /** - * @param $lft - * @param $rgt - * @param $parentId + * @param int $lft + * @param int $rgt + * @param int|string|null $parentId * * @return $this */ - public function rawNode($lft, $rgt, $parentId) + public function rawNode(int $lft, int $rgt, int|string|null $parentId): self { $this->setLft($lft)->setRgt($rgt)->setParentId($parentId); @@ -530,7 +520,7 @@ public function rawNode($lft, $rgt, $parentId) * * @return bool */ - public function up($amount = 1) + public function up(int $amount = 1): bool { $sibling = $this->prevSiblings() ->defaultOrder('desc') @@ -551,7 +541,7 @@ public function up($amount = 1) * * @return bool */ - public function down($amount = 1) + public function down(int $amount = 1): bool { $sibling = $this->nextSiblings() ->defaultOrder() @@ -692,7 +682,7 @@ protected function restoreDescendants($deletedAt) * * @since 2.0 */ - public function newEloquentBuilder($query) + public function newEloquentBuilder($query): QueryBuilder { return new QueryBuilder($query); } @@ -704,7 +694,7 @@ public function newEloquentBuilder($query) * * @return QueryBuilder */ - public function newNestedSetQuery($table = null) + public function newNestedSetQuery($table = null): QueryBuilder { $builder = $this->usesSoftDelete() ? $this->withTrashed() @@ -718,7 +708,7 @@ public function newNestedSetQuery($table = null) * * @return QueryBuilder */ - public function newScopedQuery($table = null) + public function newScopedQuery($table = null): QueryBuilder { return $this->applyNestedSetScope($this->newQuery(), $table); } @@ -760,7 +750,7 @@ protected function getScopeAttributes() * * @return QueryBuilder */ - public static function scoped(array $attributes) + public static function scoped(array $attributes): QueryBuilder { $instance = new static(); @@ -772,7 +762,7 @@ public static function scoped(array $attributes) /** * {@inheritdoc} */ - public function newCollection(array $models = []) + public function newCollection(array $models = []): Collection { return new Collection($models); } @@ -812,10 +802,8 @@ public static function create(array $attributes = [], ?self $parent = null) /** * Get node height (rgt - lft + 1). - * - * @return int */ - public function getNodeHeight() + public function getNodeHeight(): int { if (!$this->exists) { return 2; @@ -826,10 +814,8 @@ public function getNodeHeight() /** * Get number of descendant nodes. - * - * @return int */ - public function getDescendantCount() + public function getDescendantCount(): int { return ceil($this->getNodeHeight() / 2) - 1; } @@ -839,11 +825,11 @@ public function getDescendantCount() * * Behind the scenes node is appended to found parent node. * - * @param int $value + * @param int|string|null $value * * @throws \Exception If parent node doesn't exists */ - public function setParentIdAttribute($value) + public function setParentIdAttribute(int|string|null $value): void { if ($this->getParentId() == $value) { return; @@ -858,68 +844,53 @@ public function setParentIdAttribute($value) /** * Get whether node is root. - * - * @return bool */ - public function isRoot() + public function isRoot(): bool { return is_null($this->getParentId()); } - /** - * @return bool - */ - public function isLeaf() + public function isLeaf(): bool { return $this->getLft() + 1 == $this->getRgt(); } /** * Get the lft key name. - * - * @return string */ - public function getLftName() + public function getLftName(): string { return NestedSet::LFT; } /** * Get the rgt key name. - * - * @return string */ - public function getRgtName() + public function getRgtName(): string { return NestedSet::RGT; } /** * Get the parent id key name. - * - * @return string */ - public function getParentIdName() + public function getParentIdName(): string { return NestedSet::PARENT_ID; } /** * Get the value of the model's lft key. - * - * @return int */ - public function getLft() + public function getLft(): int|null { return $this->getAttributeValue($this->getLftName()); } /** * Get the value of the model's rgt key. - * - * @return int */ - public function getRgt() + public function getRgt(): int|null { return $this->getAttributeValue($this->getRgtName()); } @@ -927,9 +898,9 @@ public function getRgt() /** * Get the value of the model's parent id key. * - * @return int + * @return int|string|null */ - public function getParentId() + public function getParentId(): int|string|null { return $this->getAttributeValue($this->getParentIdName()); } @@ -939,11 +910,11 @@ public function getParentId() * * This can be either a next sibling or a next sibling of the parent node. * - * @param array $columns + * @param string[] $columns * * @return self */ - public function getNextNode(array $columns = ['*']) + public function getNextNode(array $columns = ['*']): self { return $this->nextNodes()->defaultOrder()->first($columns); } @@ -953,17 +924,17 @@ public function getNextNode(array $columns = ['*']) * * This can be either a prev sibling or parent node. * - * @param array $columns + * @param string[] $columns * * @return self */ - public function getPrevNode(array $columns = ['*']) + public function getPrevNode(array $columns = ['*']): self { return $this->prevNodes()->defaultOrder('desc')->first($columns); } /** - * @param array $columns + * @param string[] $columns * * @return Collection */ @@ -973,7 +944,7 @@ public function getAncestors(array $columns = ['*']) } /** - * @param array $columns + * @param string[] $columns * * @return Collection|self[] */ @@ -983,7 +954,7 @@ public function getDescendants(array $columns = ['*']) } /** - * @param array $columns + * @param string[] $columns * * @return Collection|self[] */ @@ -993,7 +964,7 @@ public function getSiblings(array $columns = ['*']) } /** - * @param array $columns + * @param string[] $columns * * @return Collection|self[] */ @@ -1003,7 +974,7 @@ public function getNextSiblings(array $columns = ['*']) } /** - * @param array $columns + * @param string[] $columns * * @return Collection|self[] */ @@ -1013,7 +984,7 @@ public function getPrevSiblings(array $columns = ['*']) } /** - * @param array $columns + * @param string[] $columns * * @return self */ @@ -1023,7 +994,7 @@ public function getNextSibling(array $columns = ['*']) } /** - * @param array $columns + * @param string[] $columns * * @return self */ @@ -1150,9 +1121,9 @@ public function getBounds() /** * @param $value * - * @return $this + * @return self $this */ - public function setLft($value) + public function setLft(int $value): self { $this->attributes[$this->getLftName()] = $value; @@ -1162,9 +1133,9 @@ public function setLft($value) /** * @param $value * - * @return $this + * @return self $this */ - public function setRgt($value) + public function setRgt(int $value): self { $this->attributes[$this->getRgtName()] = $value; @@ -1172,11 +1143,11 @@ public function setRgt($value) } /** - * @param $value + * @param int|string|null $value * - * @return $this + * @return self $this */ - public function setParentId($value) + public function setParentId(int|string|null $value): self { $this->attributes[$this->getParentIdName()] = $value; @@ -1239,11 +1210,11 @@ protected function assertSameScope(self $node) } /** - * @param array|null $except + * @param string[]|null $except * * @return \Illuminate\Database\Eloquent\Model */ - public function replicate(?array $except = null) + public function replicate(?array $except = null): self { $defaults = [ $this->getParentIdName(), diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 7aaf508..934bb01 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -13,7 +13,7 @@ class QueryBuilder extends Builder { /** - * @var NodeTrait|Model + * @var Node&Model */ protected $model; @@ -510,7 +510,7 @@ public function hasChildren() */ public function defaultOrder($dir = 'asc') { - $this->query->orders = null; + $this->query->orders = []; $this->query->orderBy($this->model->getLftName(), $dir); @@ -665,9 +665,9 @@ protected function columnPatch($col, array $params) * * @since 2.0 * - * @return array + * @return array{oddness:int,duplicates:int,wrong_parent:int,missing_parent:int} */ - public function countErrors() + public function countErrors(): array { $checks = []; @@ -691,6 +691,7 @@ public function countErrors() $query->selectSub($inner, $key); } + /** @var array{oddness:int,duplicates:int,wrong_parent:int,missing_parent:int} */ return (array) $query->first(); } @@ -1080,12 +1081,13 @@ public function applyNestedSetScope($table = null) /** * Get the root node. * - * @param array $columns + * @param string[] $columns * - * @return self + * @return (Node&Model)|null */ - public function root(array $columns = ['*']) + public function root(array $columns = ['*']): (Model&Node)|null { + /** @var (Node&Model)|null */ return $this->whereIsRoot()->first($columns); } }