From d6f9aa6b4523e9a8f2f032f57515de4d13b5a052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 28 Jan 2026 16:19:25 +0100 Subject: [PATCH] Refactor subject conversion in `AuditEvent2Trail` and enhance `SubjectConverterInterface` - Replaced `array_map` with `array_reduce` in `AuditEvent2Trail` for flattening converted subjects. - Enhanced `SubjectConverterInterface` with a `supportsConvert` method and PHPStan annotations to improve type safety and flexibility. --- .../ChillMainBundle/Audit/AuditEvent2Trail.php | 8 +++++++- .../Audit/SubjectConverterInterface.php | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Bundle/ChillMainBundle/Audit/AuditEvent2Trail.php b/src/Bundle/ChillMainBundle/Audit/AuditEvent2Trail.php index 2b7380631..4a1927636 100644 --- a/src/Bundle/ChillMainBundle/Audit/AuditEvent2Trail.php +++ b/src/Bundle/ChillMainBundle/Audit/AuditEvent2Trail.php @@ -34,7 +34,13 @@ final readonly class AuditEvent2Trail implements AuditEvent2TrailInterface $event->description->trans($this->translator) : $event->description; - $targets = array_map(fn (mixed $subject) => $this->subjectConverterManager->convertEntityToSubjects($subject), $event->subjects); + $targets = array_reduce( + $event->subjects, + function (array $carry, mixed $item): array { + return array_merge($carry, $this->subjectConverterManager->convertEntityToSubjects($item)); + }, + [] + ); $user = $this->security->getUser(); diff --git a/src/Bundle/ChillMainBundle/Audit/SubjectConverterInterface.php b/src/Bundle/ChillMainBundle/Audit/SubjectConverterInterface.php index 35e766fcb..b0ed59d7d 100644 --- a/src/Bundle/ChillMainBundle/Audit/SubjectConverterInterface.php +++ b/src/Bundle/ChillMainBundle/Audit/SubjectConverterInterface.php @@ -11,13 +11,27 @@ declare(strict_types=1); namespace Chill\MainBundle\Audit; +/** + * @template T + */ interface SubjectConverterInterface { /** + * @param T $subject + * * @return Subject|list */ public function convert(mixed $subject): Subject|array; + /** + * Determines whether the given subject is supported for conversion. + * + * @param mixed $subject the subject to check for conversion support + * + * @return bool true if the subject can be converted, false otherwise + * + * @phpstan-assert-if-true T $subject + */ public function supportsConvert(mixed $subject): bool; public static function getDefaultPriority(): int;