Skip to content

Commit 2c175cb

Browse files
committed
Higher Rector presets to Improve type safety, clarity, and robustness
Modernize and harden nesting logic and configuration: - Replace individual Rector level calls with withPreparedSets(...) in rector.php. - Add declare(strict_types=1) to source and test stubs. - Improve NestableCollection: add stricter type hints, rename parameters for clarity (item->model, collection->baseCollection), update method signatures (listsFlattened, listsFlattenedQualified, setParentsRecursive), and use explicit model checks. - Tighten nesting logic: treat empty/'0' parentColumn as no-parent, use early continues when building parent/child relations, and add explicit closure return types. - Add return type for newCollection in NestableTrait. - Small test change: use consistent string concatenation for generated titles. These changes improve type safety, clarity, and robustness of tree-building behavior and align the project with Rector presets.
1 parent cb2f414 commit 2c175cb

5 files changed

Lines changed: 41 additions & 28 deletions

File tree

rector.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
])
1212
// uncomment to reach your current PHP version
1313
->withPhpSets()
14-
->withTypeCoverageLevel(10)
15-
->withDeadCodeLevel(10)
16-
->withCodeQualityLevel(10);
14+
->withPreparedSets(
15+
deadCode: true,
16+
codeQuality: true,
17+
codingStyle: true,
18+
typeDeclarations: true,
19+
privatization: true,
20+
naming: true,
21+
instanceOf: true,
22+
earlyReturn: true,
23+
rectorPreset: true,
24+
);

src/NestableCollection.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* (c) Samuel De Backer <sdebacker@gmail.com>
57
*
68
* For the full copyright and license information, please view the LICENSE
79
* file that was distributed with this source code.
810
*/
9-
1011
namespace TypiCMS;
1112

1213
use Illuminate\Database\Eloquent\Collection;
@@ -76,7 +77,7 @@ public function noCleaning(): self
7677
/** @return self<TKey, TModel> */
7778
public function nest(): self
7879
{
79-
if (! $this->parentColumn) {
80+
if ($this->parentColumn === '' || $this->parentColumn === '0') {
8081
return $this;
8182
}
8283

@@ -95,10 +96,10 @@ public function nest(): self
9596
return $this;
9697
}
9798

98-
public function anAncestorIsMissing(Model $item): bool
99+
public function anAncestorIsMissing(Model $model): bool
99100
{
100101
/** @var int|string|null $parentId */
101-
$parentId = $item->{$this->parentColumn};
102+
$parentId = $model->{$this->parentColumn};
102103

103104
if (! $parentId) {
104105
return false;
@@ -115,23 +116,23 @@ public function anAncestorIsMissing(Model $item): bool
115116
}
116117

117118
/**
118-
* @param BaseCollection<array-key, Model>|null $collection
119+
* @param BaseCollection<array-key, Model>|null $baseCollection
119120
* @param array<int|string, string> $flattened
120121
* @return array<int|string, string>
121122
*/
122123
public function listsFlattened(
123124
string $column = 'title',
124-
?BaseCollection $collection = null,
125+
?BaseCollection $baseCollection = null,
125126
int $level = 0,
126127
array &$flattened = [],
127128
?string $indentChars = null,
128129
mixed $parentString = null,
129130
): array {
130-
$collection ??= $this;
131+
$baseCollection ??= $this;
131132
$indentChars ??= $this->indentChars;
132133

133134
/** @var Model $item */
134-
foreach ($collection as $item) {
135+
foreach ($baseCollection as $item) {
135136
$itemString = $this->buildFlattenedLabel($item, $column, $indentChars, $level, $parentString);
136137
/** @var int|string $key */
137138
$key = $item->getKey();
@@ -156,18 +157,18 @@ public function listsFlattened(
156157
}
157158

158159
/**
159-
* @param BaseCollection<array-key, Model>|null $collection
160+
* @param BaseCollection<array-key, Model>|null $baseCollection
160161
* @param array<int|string, string> $flattened
161162
* @return array<int|string, string>
162163
*/
163164
public function listsFlattenedQualified(
164165
string $column = 'title',
165-
?BaseCollection $collection = null,
166+
?BaseCollection $baseCollection = null,
166167
int $level = 0,
167168
array &$flattened = [],
168169
?string $indentChars = null,
169170
): array {
170-
return $this->listsFlattened($column, $collection, $level, $flattened, $indentChars, true);
171+
return $this->listsFlattened($column, $baseCollection, $level, $flattened, $indentChars, true);
171172
}
172173

173174
public function total(): int
@@ -200,7 +201,7 @@ protected function initializeChildrenCollections(): void
200201
protected function rejectOrphans(): static
201202
{
202203
/** @var static */
203-
return $this->reject(fn($item) => $item->{$this->parentColumn} && $this->anAncestorIsMissing($item));
204+
return $this->reject(fn($item): bool => $item->{$this->parentColumn} && $this->anAncestorIsMissing($item));
204205
}
205206

206207
/**
@@ -213,10 +214,12 @@ protected function assignChildrenToParents(self|BaseCollection $collection): arr
213214

214215
/** @var Model $item */
215216
foreach ($collection as $item) {
216-
if (! $item->{$this->parentColumn} || ! isset($collection[$item->{$this->parentColumn}])) {
217+
if (! $item->{$this->parentColumn}) {
218+
continue;
219+
}
220+
if (! isset($collection[$item->{$this->parentColumn}])) {
217221
continue;
218222
}
219-
220223
/** @var BaseCollection<array-key, Model> $parentChildren */
221224
$parentChildren = $collection[$item->{$this->parentColumn}]->{$this->childrenName};
222225
$parentChildren->push($item);
@@ -227,14 +230,14 @@ protected function assignChildrenToParents(self|BaseCollection $collection): arr
227230
}
228231

229232
protected function buildFlattenedLabel(
230-
Model $item,
233+
Model $model,
231234
string $column,
232235
string $indentChars,
233236
int $level,
234237
mixed $parentString,
235238
): string {
236239
/** @var string $value */
237-
$value = $item->{$column};
240+
$value = $model->{$column};
238241

239242
if (! $parentString) {
240243
return str_repeat($indentChars, $level).$value;
@@ -248,13 +251,13 @@ protected function buildFlattenedLabel(
248251
return $parentString.$indentChars.$value;
249252
}
250253

251-
/** @param BaseCollection<array-key, Model> $items */
252-
protected function setParentsRecursive(BaseCollection $items, ?Model $parent = null): void
254+
/** @param BaseCollection<array-key, Model> $baseCollection */
255+
protected function setParentsRecursive(BaseCollection $baseCollection, ?Model $model = null): void
253256
{
254257
/** @var Model $item */
255-
foreach ($items as $item) {
256-
if ($parent) {
257-
$item->setRelation($this->parentRelation, $parent);
258+
foreach ($baseCollection as $item) {
259+
if ($model instanceof \Illuminate\Database\Eloquent\Model) {
260+
$item->setRelation($this->parentRelation, $model);
258261
}
259262

260263
/** @var BaseCollection<array-key, Model> $children */

src/NestableTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace TypiCMS;
46

57
trait NestableTrait
68
{
79
/**
810
* Return a custom nested collection.
9-
*
10-
* @return NestableCollection
1111
*/
12-
public function newCollection(array $models = [])
12+
public function newCollection(array $models = []): \TypiCMS\NestableCollection
1313
{
1414
return new NestableCollection($models);
1515
}

tests/Stubs/Item.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Tests\Stubs;
46

57
use Illuminate\Database\Eloquent\Model;

tests/Unit/NestableCollectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function makeItem(int $id, ?int $parentId = null, string $title = ''): Item
99
$item = new Item;
1010
$item->id = $id;
1111
$item->parent_id = $parentId;
12-
$item->title = $title ?: "Item {$id}";
12+
$item->title = $title ?: 'Item ' . $id;
1313

1414
return $item;
1515
}

0 commit comments

Comments
 (0)