refactor(support): centralize ID-array normalization in toIntIds()

Consolidates the scattered `array_values(array_unique(array_map('intval',
$x)))` + manual positive-filter pattern behind a single lenient helper
`toIntIds(mixed $value): array` in core/Support/helpers/array.php.

- `RepositoryArrayHelper::sanitizePositiveIds()` now delegates (keeps
  strict array-input contract + existing tests intact).
- Drops two private duplicates: `UserProfileViewService::normalizeIds()`
  and `AddressBookService::normalizeIds()`.
- Replaces 12 inline occurrences across admin action pages with the
  helper, cutting boilerplate by 3-5 lines per site.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-24 19:11:53 +02:00
parent 52f10ae46c
commit 6c99c040b2
14 changed files with 99 additions and 68 deletions

View File

@@ -63,12 +63,13 @@ final class RepositoryArrayHelper
/**
* Sanitize an array of IDs to unique positive integers.
*
* Thin repository-layer adapter over the global `toIntIds()` helper,
* kept for the stricter `array` input contract expected by repositories.
*/
public static function sanitizePositiveIds(array $ids): array
{
$ids = array_values(array_unique(array_map('intval', $ids)));
return array_values(array_filter($ids, static fn (int $id): bool => $id > 0));
return toIntIds($ids);
}
/**

View File

@@ -50,7 +50,7 @@ class UserProfileViewService
return ['status' => 'forbidden'];
}
$viewerTenantIds = $this->normalizeIds($this->scopeGateway->getUserTenantIds($currentUserId));
$viewerTenantIds = toIntIds($this->scopeGateway->getUserTenantIds($currentUserId));
$assignments = $this->userAssignmentService->buildAssignmentsForUser($targetUserId);
$departmentMap = [];
@@ -154,14 +154,4 @@ class UserProfileViewService
return $profile;
}
/**
* @param array<int, int|string> $values
* @return list<int>
*/
private function normalizeIds(array $values): array
{
$ids = array_values(array_unique(array_map('intval', $values)));
return array_values(array_filter($ids, static fn (int $id): bool => $id > 0));
}
}

View File

@@ -2,6 +2,7 @@
// Load all global helper groups used by templates and page actions.
require __DIR__ . '/helpers/app.php';
require __DIR__ . '/helpers/array.php';
require __DIR__ . '/helpers/branding.php';
require __DIR__ . '/helpers/export.php';
require __DIR__ . '/helpers/grid.php';

View File

@@ -0,0 +1,28 @@
<?php
/**
* Normalize loose input into a list of unique positive integer IDs.
*
* Accepts any value: an array of scalars, a single scalar, or null/''.
* Scalars are treated as a single-element list. Non-numeric, empty,
* zero, or negative values are dropped. Input order of the first
* occurrence of each ID is preserved.
*
* Typical use: cleaning POST body arrays like `$request->body('role_ids', [])`
* before handing them to a service or repository.
*
* @return list<int>
*/
function toIntIds(mixed $value): array
{
if ($value === null || $value === '') {
return [];
}
if (!is_array($value)) {
$value = [$value];
}
$ids = array_map('intval', $value);
$ids = array_filter($ids, static fn (int $id): bool => $id > 0);
return array_values(array_unique($ids));
}