Skip to content

Commit a21eb7a

Browse files
committed
Type hinting
1 parent 03cd87e commit a21eb7a

File tree

3 files changed

+53
-58
lines changed

3 files changed

+53
-58
lines changed

composer.json

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
{
22
"name": "typicms/nestablecollection",
33
"description": "A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.",
4-
"keywords": ["Laravel", "Eloquent", "Collection", "TypiCMS", "tree", "nested set", "adjacency list"],
4+
"keywords": [
5+
"Laravel",
6+
"Eloquent",
7+
"Collection",
8+
"TypiCMS",
9+
"tree",
10+
"nested set",
11+
"adjacency list"
12+
],
513
"license": "MIT",
614
"authors": [
715
{
@@ -10,8 +18,14 @@
1018
}
1119
],
1220
"require": {
13-
"illuminate/database": "~4.2|~5.0|~6.0|~7.0|~8.0|~9.0",
14-
"illuminate/support": "~4.2|~5.0|~6.0|~7.0|~8.0|~9.0"
21+
"illuminate/database": "~9.0",
22+
"illuminate/support": "~9.0"
23+
},
24+
"require-dev": {
25+
"illuminate/database": "~9.0",
26+
"illuminate/support": "~9.0",
27+
"nunomaduro/larastan": "^2.0",
28+
"orchestra/testbench": "^7.4"
1529
},
1630
"autoload": {
1731
"psr-4": {

phpstan.neon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
includes:
2+
- ./vendor/nunomaduro/larastan/extension.neon
3+
4+
parameters:
5+
6+
paths:
7+
- src
8+
9+
# The level 9 is the highest level
10+
level: 5
11+
12+
checkGenericClassInNonGenericObjectType: false

src/NestableCollection.php

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,26 @@
1515

1616
class NestableCollection extends Collection
1717
{
18-
protected $total;
18+
protected int $total;
1919

20-
protected $parentColumn;
20+
protected string $parentColumn;
2121

22-
protected $removeItemsWithMissingAncestor = true;
22+
protected bool $removeItemsWithMissingAncestor = true;
2323

24-
protected $indentChars = '    ';
24+
protected string $indentChars = '    ';
2525

26-
protected $childrenName = 'items';
26+
protected string $childrenName = 'items';
2727

28-
protected $parentRelation = 'parent';
28+
protected string $parentRelation = 'parent';
2929

30-
public function __construct($items = [])
30+
public function __construct(array $items = [])
3131
{
3232
parent::__construct($items);
3333
$this->parentColumn = 'parent_id';
3434
$this->total = count($items);
3535
}
3636

37-
public function childrenName($name)
37+
public function childrenName(string $name): self
3838
{
3939
$this->childrenName = $name;
4040

@@ -43,10 +43,8 @@ public function childrenName($name)
4343

4444
/**
4545
* Nest items.
46-
*
47-
* @return mixed NestableCollection
4846
*/
49-
public function nest()
47+
public function nest(): self
5048
{
5149
$parentColumn = $this->parentColumn;
5250
if (!$parentColumn) {
@@ -80,6 +78,7 @@ public function nest()
8078
foreach ($collection->items as $item) {
8179
if ($item->{$parentColumn} && isset($collection[$item->{$parentColumn}])) {
8280
$collection[$item->{$parentColumn}]->{$this->childrenName}->push($item);
81+
// @phpstan-ignore-next-line
8382
$keysToDelete[] = $item->id;
8483
}
8584
}
@@ -93,29 +92,21 @@ public function nest()
9392
/**
9493
* Recursive function that flatten a nested Collection
9594
* with characters (default is four spaces).
96-
*
97-
* @param string $column
98-
* @param int $level
99-
* @param array &$flattened
100-
* @param null|string $indentChars
101-
* @param null|bool|string $parent_string
102-
*
103-
* @return array
10495
*/
105-
public function listsFlattened($column = 'title', BaseCollection $collection = null, $level = 0, array &$flattened = [], $indentChars = null, $parent_string = null)
96+
public function listsFlattened(string $column = 'title', BaseCollection $collection = null, int $level = 0, array &$flattened = [], ?string $indentChars = null, mixed $parentString = null): array
10697
{
10798
$collection = $collection ?: $this;
10899
$indentChars = $indentChars ?: $this->indentChars;
109100
foreach ($collection as $item) {
110-
if ($parent_string) {
111-
$item_string = ($parent_string === true) ? $item->{$column} : $parent_string.$indentChars.$item->{$column};
101+
if ($parentString) {
102+
$item_string = ($parentString === true) ? $item->{$column} : $parentString.$indentChars.$item->{$column};
112103
} else {
113104
$item_string = str_repeat($indentChars, $level).$item->{$column};
114105
}
115106

116107
$flattened[$item->id] = $item_string;
117108
if ($item->{$this->childrenName}) {
118-
$this->listsFlattened($column, $item->{$this->childrenName}, $level + 1, $flattened, $indentChars, ($parent_string) ? $item_string : null);
109+
$this->listsFlattened($column, $item->{$this->childrenName}, $level + 1, $flattened, $indentChars, ($parentString) ? $item_string : null);
119110
}
120111
}
121112

@@ -124,27 +115,16 @@ public function listsFlattened($column = 'title', BaseCollection $collection = n
124115

125116
/**
126117
* Returns a fully qualified version of listsFlattened.
127-
*
128-
* @param string $column
129-
* @param int $level
130-
* @param array &$flattened
131-
* @param string $indentChars
132-
*
133-
* @return array
134118
*/
135-
public function listsFlattenedQualified($column = 'title', BaseCollection $collection = null, $level = 0, array &$flattened = [], $indentChars = null)
119+
public function listsFlattenedQualified(string $column = 'title', BaseCollection $collection = null, int $level = 0, array &$flattened = [], ?string $indentChars = null): array
136120
{
137121
return $this->listsFlattened($column, $collection, $level, $flattened, $indentChars, true);
138122
}
139123

140124
/**
141125
* Change the default indent characters when flattening lists.
142-
*
143-
* @param string $indentChars
144-
*
145-
* @return $this
146126
*/
147-
public function setIndent($indentChars)
127+
public function setIndent(string $indentChars): self
148128
{
149129
$this->indentChars = $indentChars;
150130

@@ -153,10 +133,8 @@ public function setIndent($indentChars)
153133

154134
/**
155135
* Force keeping items that have a missing ancestor.
156-
*
157-
* @return NestableCollection
158136
*/
159-
public function noCleaning()
137+
public function noCleaning(): self
160138
{
161139
$this->removeItemsWithMissingAncestor = false;
162140

@@ -165,12 +143,8 @@ public function noCleaning()
165143

166144
/**
167145
* Check if an ancestor is missing.
168-
*
169-
* @param mixed $item
170-
*
171-
* @return bool
172146
*/
173-
public function anAncestorIsMissing($item)
147+
public function anAncestorIsMissing(mixed $item): bool
174148
{
175149
$parentColumn = $this->parentColumn;
176150
if (!$item->{$parentColumn}) {
@@ -186,38 +160,33 @@ public function anAncestorIsMissing($item)
186160

187161
/**
188162
* Get total items in nested collection.
189-
*
190-
* @return int
191163
*/
192-
public function total()
164+
public function total(): int
193165
{
194166
return $this->total;
195167
}
196168

197169
/**
198170
* Get total items for laravel 4 compatibility.
199-
*
200-
* @return int
201171
*/
202-
public function getTotal()
172+
public function getTotal(): int
203173
{
204174
return $this->total();
205175
}
206176

207177
/**
208-
* Sets the $item->parent relation for each item in the NestableCollection to be the parent it has in the collection
178+
* Sets the $item->parent relation for each item in the
179+
* NestableCollection to be the parent it has in the collection
209180
* so it can be used without querying the database.
210-
*
211-
* @return $this
212181
*/
213-
public function setParents()
182+
public function setParents(): self
214183
{
215184
$this->setParentsRecursive($this);
216185

217186
return $this;
218187
}
219188

220-
protected function setParentsRecursive(&$items, &$parent = null)
189+
protected function setParentsRecursive(self &$items, &$parent = null): void
221190
{
222191
foreach ($items as $item) {
223192
if ($parent) {

0 commit comments

Comments
 (0)