From fc15b85d11628e9506b9d66293cd32b04ef94183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 28 Nov 2022 12:24:10 +0100 Subject: [PATCH] DX: [accompanying period] refactor the getCenters method Use more portable spl_object_hash for listing different centers --- .../ChillPersonBundle/Entity/AccompanyingPeriod.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php index 0b6efe328..ae7b927a6 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php @@ -53,7 +53,7 @@ use Symfony\Component\Validator\GroupSequenceProviderInterface; use UnexpectedValueException; -use function in_array; +use function array_key_exists; use const SORT_REGULAR; /** @@ -644,16 +644,18 @@ class AccompanyingPeriod implements public function getCenters(): ?iterable { + $centers = []; + foreach ($this->getPersons() as $person) { if ( - !in_array($person->getCenter(), $centers ?? [], true) - && null !== $person->getCenter() + null !== $person->getCenter() + && !array_key_exists(spl_object_hash($person->getCenter()), $centers) ) { - $centers[] = $person->getCenter(); + $centers[spl_object_hash($person->getCenter())] = $person->getCenter(); } } - return $centers ?? null; + return array_values($centers); } /**