Skip to content

Commit c769351

Browse files
Fix CS issue and weird parts of the code
1 parent c867877 commit c769351

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

commands/clean/content.php

+36-37
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,52 @@
33
declare(strict_types = 1);
44

55
use Kirby\CLI\CLI;
6-
use Kirby\Cms\Collection;
76

87
function clean(
9-
Collection $collection,
8+
Generator $collection,
109
array|null $ignore = null,
1110
string|null $lang = null
1211
): void {
13-
foreach($collection as $item) {
14-
// get all fields in the content file
15-
$contentFields = $item->content($lang)->fields();
16-
17-
// unset all fields in the `$ignore` array
18-
foreach ($ignore as $field) {
19-
if (array_key_exists($field, $contentFields) === true) {
20-
unset($contentFields[$field]);
21-
}
22-
}
23-
24-
// get the keys
25-
$contentFields = array_keys($contentFields);
26-
27-
// get all field keys from blueprint
28-
$blueprintFields = array_keys($item->blueprint()->fields());
29-
30-
// get all field keys that are in $contentFields but not in $blueprintFields
31-
$fieldsToBeDeleted = array_diff($contentFields, $blueprintFields);
32-
33-
// update page only if there are any fields to be deleted
34-
if (count($fieldsToBeDeleted) > 0) {
35-
36-
// flip keys and values and set new values to null
37-
$data = array_map(fn ($value) => null, array_flip($fieldsToBeDeleted));
38-
39-
// try to update the page with the data
40-
try {
41-
$item->update($data, $lang);
42-
} catch (Exception $e) {
43-
throw $e->getMessage();
44-
}
45-
}
46-
}
12+
foreach($collection as $item) {
13+
// get all fields in the content file
14+
$contentFields = $item->content($lang)->fields();
15+
16+
// unset all fields in the `$ignore` array
17+
foreach ($ignore as $field) {
18+
if (array_key_exists($field, $contentFields) === true) {
19+
unset($contentFields[$field]);
20+
}
21+
}
22+
23+
// get the keys
24+
$contentFields = array_keys($contentFields);
25+
26+
// get all field keys from blueprint
27+
$blueprintFields = array_keys($item->blueprint()->fields());
28+
29+
// get all field keys that are in $contentFields but not in $blueprintFields
30+
$fieldsToBeDeleted = array_diff($contentFields, $blueprintFields);
31+
32+
// update page only if there are any fields to be deleted
33+
if (count($fieldsToBeDeleted) > 0) {
34+
35+
// flip keys and values and set new values to null
36+
$data = array_map(fn ($value) => null, array_flip($fieldsToBeDeleted));
37+
38+
// try to update the page with the data
39+
try {
40+
$item->update($data, $lang);
41+
} catch (Exception $e) {
42+
throw $e->getMessage();
43+
}
44+
}
45+
}
4746
}
4847

4948
return [
5049
'description' => 'Deletes all fields from page, file or user content files that are not defined in the blueprint, no matter if they contain content or not.',
5150
'command' => static function (CLI $cli): void {
52-
$kirby = $cli->kirby()->root('media');
51+
$kirby = $cli->kirby();
5352

5453
// Authenticate as almighty
5554
$kirby->impersonate('kirby');

0 commit comments

Comments
 (0)