1515
1616class 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