Skip to content

Commit 28efff9

Browse files
committed
lvl 4
1 parent 793d01f commit 28efff9

13 files changed

+230
-181
lines changed

phpstan-baseline.neon

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
parameters:
2+
ignoreErrors:
3+
4+
-
5+
message: '#^Call to an undefined method Kalnoy\\Nestedset\\Contracts\\NodeQueryBuilder\<Tmodel of Illuminate\\Database\\Eloquent\\Model\>\:\:whereIn\(\)\.$#'
6+
identifier: method.notFound
7+
count: 1
8+
path: src/QueryBuilder.php
9+
10+
-
11+
message: '#^Dynamic call to static method Kalnoy\\Nestedset\\QueryBuilder\<Tmodel of Illuminate\\Database\\Eloquent\\Model\>\:\:whereRaw\(\)\.$#'
12+
identifier: staticMethod.dynamicCall
13+
count: 1
14+
path: src/QueryBuilder.php
15+

phpstan.neon

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
includes:
22
- vendor/larastan/larastan/extension.neon
33
- vendor/lychee-org/phpstan-lychee/phpstan.neon
4+
- phpstan-baseline.neon
45

56
parameters:
6-
level: 2
7+
level: 4
78
paths:
89
- src
10+
- tests/models/
911
excludePaths:
1012
stubFiles:
1113
ignoreErrors:
12-
# - identifier: missingType.generics
13-
- '#Dynamic call to static method Kalnoy\\Nestedset\\QueryBuilder<Tmodel of Illuminate\\Database\\Eloquent\\Model>::whereRaw\(\).#'
14+
- '#.*covariant.*#'
15+
- '#.*contravariant.*#'
16+
-
17+
message: '#^Call to an undefined method Kalnoy\\Nestedset\\Contracts\\Node::assert.*\(\)\.$#'

src/AncestorsRelation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected function matches(Node $model, Node $related): bool
4343

4444
/**
4545
* @param NodeQueryBuilder<Tmodel> $query
46-
* @param NodeModel $model
46+
* @param NodeModel $model
4747
*
4848
* @return void
4949
*/

src/BaseRelation.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Database\Eloquent\Relations\Relation;
99
use Illuminate\Database\Query\Builder;
10+
use Kalnoy\Nestedset\Contracts\NestedSetCollection;
1011
use Kalnoy\Nestedset\Contracts\Node;
1112
use Kalnoy\Nestedset\Contracts\NodeQueryBuilder;
12-
use Kalnoy\Nestedset\Contracts\NestedSetCollection;
1313

1414
/**
1515
* @template Tmodel of Model
1616
*
17-
* @phpstan-type NodeModel \Kalnoy\Nestedset\Contracts\Node<Tmodel>&Model
18-
* @extends Relation<NodeModel,NodeModel,EloquentCollection<int,Node<Tmodel>>>
17+
* @phpstan-type NodeModel Node<Tmodel>&Tmodel
18+
*
19+
* @extends Relation<NodeModel,NodeModel,EloquentCollection<int,NodeModel>>
1920
*
2021
* @property NodeModel $related
2122
* @property NodeModel $parent
2223
*/
23-
abstract class BaseRelation extends Relation // @phpstan-ignore generics.notSubtype (Phpstan does not recognise the require-extend on the interface)
24+
abstract class BaseRelation extends Relation
2425
{
2526
/**
2627
* @var NodeQueryBuilder<Tmodel>
@@ -42,8 +43,8 @@ abstract class BaseRelation extends Relation // @phpstan-ignore generics.notSubt
4243
/**
4344
* AncestorsRelation constructor.
4445
*
45-
* @param QueryBuilder<Tmodel> $builder
46-
* @param NodeModel $model
46+
* @param QueryBuilder<NodeModel> $builder
47+
* @param NodeModel $model
4748
*/
4849
public function __construct(QueryBuilder $builder, Model $model)
4950
{
@@ -64,7 +65,7 @@ abstract protected function matches(Node $model, Node $related): bool;
6465

6566
/**
6667
* @param NodeQueryBuilder<Tmodel> $query
67-
* @param NodeModel $model
68+
* @param NodeModel $model
6869
*
6970
* @return void
7071
*/

src/Collection.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
use Kalnoy\Nestedset\Exceptions\NestedSetException;
99

1010
/**
11-
*
1211
* @template Tmodel of Model
1312
*
14-
* @phpstan-type NodeModel \Kalnoy\Nestedset\Contracts\Node<Tmodel>
13+
* @phpstan-type NodeModel \Kalnoy\Nestedset\Contracts\Node<Tmodel>&Model
1514
*
1615
* @extends EloquentCollection<array-key,NodeModel>
1716
*/
@@ -41,12 +40,13 @@ public function linkNodes()
4140
$node->setRelation('parent', null);
4241
}
4342

44-
/** @var array<int,NodeModel> */
4543
$children = $groupedNodes->get($node->getKey(), []);
4644

47-
foreach ($children as $child) {
48-
/** @disregard */
49-
$child->setRelation('parent', $node);
45+
if (count($children) > 0) {
46+
foreach ($children as $child) {
47+
/** @disregard */
48+
$child->setRelation('parent', $node);
49+
}
5050
}
5151

5252
/** @disregard */
@@ -65,9 +65,9 @@ public function linkNodes()
6565
*
6666
* @param mixed $root
6767
*
68-
* @return Collection<Tmodel>
68+
* @return NestedSetCollection<Tmodel>
6969
*/
70-
public function toTree($root = false): Collection
70+
public function toTree($root = false): NestedSetCollection
7171
{
7272
if ($this->isEmpty()) {
7373
return new static();
@@ -129,9 +129,9 @@ protected function getRootNodeId($root = false)
129129
*
130130
* @param bool $root
131131
*
132-
* @return Collection<Tmodel>
132+
* @return NestedSetCollection<Tmodel>
133133
*/
134-
public function toFlatTree($root = false): Collection
134+
public function toFlatTree($root = false): NestedSetCollection
135135
{
136136
/** @Var Collection<Tmodel> */
137137
$result = new Collection();
@@ -142,7 +142,6 @@ public function toFlatTree($root = false): Collection
142142

143143
/** @var NodeModel */
144144
$first = $this->first();
145-
/** @var Collection<NodeModel> */
146145
$groupedNodes = $this->groupBy($first->getParentIdName());
147146

148147
return $result->flattenTree($groupedNodes, $this->getRootNodeId($root));
@@ -154,16 +153,18 @@ public function toFlatTree($root = false): Collection
154153
* @param Collection<Tmodel> $groupedNodes
155154
* @param array-key $parentId
156155
*
157-
* @return Collection<Tmodel>
156+
* @return NestedSetCollection<Tmodel>
158157
*/
159-
protected function flattenTree(Collection $groupedNodes, $parentId): Collection
158+
protected function flattenTree(Collection $groupedNodes, $parentId): NestedSetCollection
160159
{
161-
/** @var array<int,NodeModel> */
162160
$nodes = $groupedNodes->get($parentId, []);
163-
foreach ($nodes as $node) {
164-
$this->push($node);
165161

166-
$this->flattenTree($groupedNodes, $node->getKey());
162+
if (count($nodes) > 0) {
163+
foreach ($nodes as $node) {
164+
$this->push($node);
165+
166+
$this->flattenTree($groupedNodes, $node->getKey());
167+
}
167168
}
168169

169170
return $this;

src/Contracts/NestedSetCollection.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php
22

3-
43
namespace Kalnoy\Nestedset\Contracts;
54

65
/**
7-
* @template Tmodel of \Illuminate\Database\Eloquent\Model
6+
* @template-covariant Tmodel of \Illuminate\Database\Eloquent\Model
87
*
98
* @phpstan-type NodeModel Node<Tmodel>
109
*
1110
* @require-extends \Illuminate\Database\Eloquent\Collection
11+
*
12+
* @method NestedSetCollection groupBy(string $column)
13+
* @method array<int,NodeModel> all()
1214
*/
1315
interface NestedSetCollection
1416
{
@@ -21,7 +23,6 @@ interface NestedSetCollection
2123
*/
2224
public function linkNodes();
2325

24-
2526
/**
2627
* Build a tree from a list of nodes. Each item will have set children relation.
2728
*
@@ -31,7 +32,7 @@ public function linkNodes();
3132
*
3233
* @param mixed $root
3334
*
34-
* @return Collection<NodeModel>
35+
* @return NestedSetCollection<Tmodel>
3536
*/
3637
public function toTree($root = false): NestedSetCollection;
3738

@@ -41,7 +42,7 @@ public function toTree($root = false): NestedSetCollection;
4142
*
4243
* @param bool $root
4344
*
44-
* @return Collection<NodeModel>
45+
* @return NestedSetCollection<Tmodel>
4546
*/
4647
public function toFlatTree($root = false): NestedSetCollection;
4748
}

src/Contracts/Node.php

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Kalnoy\Nestedset\Contracts;
44

5-
use Illuminate\Database\Query\Builder as BaseQueryBuilder;
65
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
76
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
87
use Illuminate\Database\Eloquent\Relations\BelongsTo;
98
use Illuminate\Database\Eloquent\Relations\HasMany;
109
use Illuminate\Database\Eloquent\Relations\Relation;
10+
use Illuminate\Database\Query\Builder as BaseQueryBuilder;
1111

1212
/**
1313
* Accompanies {@link \Kalnoy\Nestedset\NodeTrait}.
@@ -23,9 +23,19 @@
2323
*
2424
* @template Tmodel of \Illuminate\Database\Eloquent\Model
2525
*
26-
* @phpstan-type NodeModel Node<Tmodel>
27-
*
26+
* @phpstan-type NodeModel Node<Tmodel>&Tmodel
27+
*
2828
* @require-extends \Illuminate\Database\Eloquent\Model
29+
*
30+
* @method mixed getKey()
31+
* @method mixed getKeyName()
32+
* @method Node setRelation($relation, $value)
33+
* @method mixed save()
34+
* @method string getTable()
35+
* @method mixed getAttribute($key)
36+
* @method string getDeletedAtColumn()
37+
* @method Node getRelationValue($key)
38+
* @method bool usesSoftDelete()
2939
*/
3040
interface Node
3141
{
@@ -51,7 +61,7 @@ public function children(): HasMany;
5161
/**
5262
* Get query for descendants of the node.
5363
*
54-
* @return DescendantsRelation<Tmodel>
64+
* @return Relation<NodeModel,NodeModel,EloquentCollection<int,Node<Tmodel>&Tmodel>>
5565
*/
5666
public function descendants(): Relation;
5767

@@ -74,9 +84,9 @@ public function siblingsAndSelf(): NodeQueryBuilder;
7484
*
7585
* @param string[] $columns
7686
*
77-
* @return EloquentCollection<int,NodeModel>
87+
* @return NestedSetCollection<Tmodel>
7888
*/
79-
public function getSiblingsAndSelf(array $columns = ['*']): EloquentCollection;
89+
public function getSiblingsAndSelf(array $columns = ['*']): NestedSetCollection;
8090

8191
/**
8292
* Get query for siblings after the node.
@@ -109,7 +119,7 @@ public function prevNodes(): NodeQueryBuilder;
109119
/**
110120
* Get query ancestors of the node.
111121
*
112-
* @return AncestorsRelation<Tmodel>
122+
* @return Relation<NodeModel,NodeModel,EloquentCollection<int,Node<Tmodel>&Tmodel>>
113123
*/
114124
public function ancestors(): Relation;
115125

@@ -291,35 +301,35 @@ public function getPrevNode(array $columns = ['*']): Node;
291301
/**
292302
* @param string[] $columns
293303
*
294-
* @return Collection<Tmodel>
304+
* @return NestedSetCollection<Tmodel>
295305
*/
296306
public function getAncestors(array $columns = ['*']);
297307

298308
/**
299309
* @param string[] $columns
300310
*
301-
* @return Collection<Tmodel>
311+
* @return NestedSetCollection<Tmodel>
302312
*/
303313
public function getDescendants(array $columns = ['*']);
304314

305315
/**
306316
* @param string[] $columns
307317
*
308-
* @return Collection<Tmodel>
318+
* @return NestedSetCollection<Tmodel>
309319
*/
310320
public function getSiblings(array $columns = ['*']);
311321

312322
/**
313323
* @param string[] $columns
314324
*
315-
* @return Collection<Tmodel>
325+
* @return NestedSetCollection<Tmodel>
316326
*/
317327
public function getNextSiblings(array $columns = ['*']);
318328

319329
/**
320330
* @param string[] $columns
321331
*
322-
* @return Collection<Tmodel>
332+
* @return NestedSetCollection<Tmodel>
323333
*/
324334
public function getPrevSiblings(array $columns = ['*']);
325335

@@ -430,20 +440,4 @@ public function isSelfOrDescendantOf(Node $other);
430440
* @return NodeQueryBuilder<Tmodel>
431441
*/
432442
public function newQuery();
433-
434-
/**
435-
* Get the value of the model's primary key.
436-
* ! This is directly from Model method...
437-
*
438-
* @return mixed
439-
*/
440-
public function getKey();
441-
442-
/**
443-
* Get the value of the model's primary key.
444-
* ! This is directly from Model method...
445-
*
446-
* @return mixed
447-
*/
448-
public function getKeyName();
449443
}

0 commit comments

Comments
 (0)