From 2a046245dcf302eeedeadaafd7b35d5e8abc93b4 Mon Sep 17 00:00:00 2001 From: Baptiste Leduc Date: Mon, 21 Sep 2026 19:27:34 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20migre=20l'entit=C3=A9=20Room=20de?= =?UTF-8?q?=20Ting=20vers=20Doctrine=20(#2383)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpstan-baseline.php | 44 ++++----------- .../Controller/Admin/Event/RoomAction.php | 22 ++++---- .../Admin/Event/Session/EditAction.php | 6 +- .../Admin/Event/Session/IndexAction.php | 8 +-- .../Entity/Repository/RoomRepository.php | 29 ++++++++++ sources/AppBundle/Event/Entity/Room.php | 27 +++++++++ sources/AppBundle/Event/Form/RoomType.php | 2 +- .../Entity/Repository/RoomRepositoryTest.php | 55 +++++++++++++++++++ 8 files changed, 140 insertions(+), 53 deletions(-) create mode 100644 sources/AppBundle/Event/Entity/Repository/RoomRepository.php create mode 100644 sources/AppBundle/Event/Entity/Room.php create mode 100644 tests/integration/AppBundle/Event/Entity/Repository/RoomRepositoryTest.php diff --git a/phpstan-baseline.php b/phpstan-baseline.php index f85ead6d8..22f549ef7 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -3025,30 +3025,6 @@ 'count' => 1, 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RemoveEventAction.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Binary operation "\\." between \'edit_room_\' and mixed results in an error\\.$#', - 'identifier' => 'binaryOp.invalid', - 'count' => 1, - 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Cannot call method getId\\(\\) on mixed\\.$#', - 'identifier' => 'method.nonObject', - 'count' => 1, - 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Cannot call method getName\\(\\) on mixed\\.$#', - 'identifier' => 'method.nonObject', - 'count' => 3, - 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Method AppBundle\\\\Controller\\\\Admin\\\\Event\\\\RoomAction\\:\\:getFormsForRooms\\(\\) has parameter \\$rooms with generic interface CCMBenchmark\\\\Ting\\\\Repository\\\\CollectionInterface but does not specify its types\\: T$#', - 'identifier' => 'missingType.generics', - 'count' => 1, - 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php', -]; $ignoreErrors[] = [ 'message' => '#^Method AppBundle\\\\Controller\\\\Admin\\\\Event\\\\RoomAction\\:\\:getFormsForRooms\\(\\) return type with generic interface Symfony\\\\Component\\\\Form\\\\FormInterface does not specify its types\\: TData$#', 'identifier' => 'missingType.generics', @@ -3056,19 +3032,19 @@ 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php', ]; $ignoreErrors[] = [ - 'message' => '#^Parameter \\#1 \\$entity of method CCMBenchmark\\\\Ting\\\\Repository\\\\Repository\\\\:\\:delete\\(\\) expects AppBundle\\\\Event\\\\Model\\\\Room, mixed given\\.$#', - 'identifier' => 'argument.type', - 'count' => 1, + 'message' => '#^Cannot access property \\$name on mixed\\.$#', + 'identifier' => 'property.nonObject', + 'count' => 2, 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php', ]; $ignoreErrors[] = [ - 'message' => '#^Parameter \\#1 \\$entity of method CCMBenchmark\\\\Ting\\\\Repository\\\\Repository\\\\:\\:save\\(\\) expects AppBundle\\\\Event\\\\Model\\\\Room, mixed given\\.$#', + 'message' => '#^Parameter \\#1 \\$entity of method AppBundle\\\\Doctrine\\\\EntityRepository\\\\:\\:delete\\(\\) expects AppBundle\\\\Event\\\\Entity\\\\Room, mixed given\\.$#', 'identifier' => 'argument.type', - 'count' => 2, + 'count' => 1, 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php', ]; $ignoreErrors[] = [ - 'message' => '#^Parameter \\#1 \\$eventId of method AppBundle\\\\Event\\\\Model\\\\Room\\:\\:setEventId\\(\\) expects int, int\\|null given\\.$#', + 'message' => '#^Parameter \\#1 \\$entity of method AppBundle\\\\Doctrine\\\\EntityRepository\\\\:\\:save\\(\\) expects AppBundle\\\\Event\\\\Entity\\\\Room, mixed given\\.$#', 'identifier' => 'argument.type', 'count' => 1, 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php', @@ -3076,7 +3052,7 @@ $ignoreErrors[] = [ 'message' => '#^Parameter \\#2 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#', 'identifier' => 'argument.type', - 'count' => 3, + 'count' => 2, 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php', ]; $ignoreErrors[] = [ @@ -3140,7 +3116,7 @@ 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/Session/EditAction.php', ]; $ignoreErrors[] = [ - 'message' => '#^Method AppBundle\\\\Controller\\\\Admin\\\\Event\\\\Session\\\\EditAction\\:\\:roomChoices\\(\\) should return array\\ but returns array\\\\.$#', + 'message' => '#^Method AppBundle\\\\Controller\\\\Admin\\\\Event\\\\Session\\\\EditAction\\:\\:roomChoices\\(\\) should return array\\ but returns array\\\\.$#', 'identifier' => 'return.type', 'count' => 1, 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/Session/EditAction.php', @@ -3170,7 +3146,7 @@ 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/Session/EditAction.php', ]; $ignoreErrors[] = [ - 'message' => '#^Possibly invalid array key type mixed\\.$#', + 'message' => '#^Possibly invalid array key type string\\|null\\.$#', 'identifier' => 'offsetAccess.invalidOffset', 'count' => 1, 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/Session/EditAction.php', @@ -3188,7 +3164,7 @@ 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/Session/IndexAction.php', ]; $ignoreErrors[] = [ - 'message' => '#^Parameter \\#2 \\$title of class AppBundle\\\\Event\\\\Model\\\\Session\\\\CalendarResource constructor expects string, mixed given\\.$#', + 'message' => '#^Parameter \\#2 \\$title of class AppBundle\\\\Event\\\\Model\\\\Session\\\\CalendarResource constructor expects string, string\\|null given\\.$#', 'identifier' => 'argument.type', 'count' => 1, 'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/Session/IndexAction.php', diff --git a/sources/AppBundle/Controller/Admin/Event/RoomAction.php b/sources/AppBundle/Controller/Admin/Event/RoomAction.php index 0c3f63422..7beb92969 100644 --- a/sources/AppBundle/Controller/Admin/Event/RoomAction.php +++ b/sources/AppBundle/Controller/Admin/Event/RoomAction.php @@ -6,10 +6,9 @@ use AppBundle\Event\AdminEventSelection; use Symfony\Component\Form\FormView; +use AppBundle\Event\Entity\Repository\RoomRepository; +use AppBundle\Event\Entity\Room; use AppBundle\Event\Form\RoomType; -use AppBundle\Event\Model\Repository\RoomRepository; -use AppBundle\Event\Model\Room; -use CCMBenchmark\Ting\Repository\CollectionInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormInterface; @@ -29,16 +28,16 @@ public function __invoke(Request $request, AdminEventSelection $eventSelection): $rooms = $this->roomRepository->getByEvent($event); $editForms = $this->getFormsForRooms($rooms); - foreach ($editForms as $form) { + foreach ($editForms as $i => $form) { $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $room = $form->getData(); if ($request->request->has('delete')) { $this->roomRepository->delete($room); - $this->addFlash('notice', sprintf('La salle "%s" a été supprimée.', $room->getName())); + $this->addFlash('notice', sprintf('La salle "%s" a été supprimée.', $room->name)); } else { $this->roomRepository->save($room); - $this->addFlash('notice', sprintf('La salle "%s" a été sauvegardée.', $room->getName())); + $this->addFlash('notice', sprintf('La salle "%s" a été sauvegardée.', $room->name)); } return $this->redirectToRoute('admin_event_room', [ @@ -48,15 +47,14 @@ public function __invoke(Request $request, AdminEventSelection $eventSelection): } $newRoom = new Room(); - $newRoom->setEventId($event->getId()); + $newRoom->eventId = $event->getId(); $addForm = $this->createForm(RoomType::class, $newRoom); $addForm->handleRequest($request); if ($addForm->isSubmitted() && $addForm->isValid()) { - $newRoom = $addForm->getData(); $this->roomRepository->save($newRoom); - $this->addFlash('notice', sprintf('La salle "%s" a été ajoutée.', $newRoom->getName())); + $this->addFlash('notice', sprintf('La salle "%s" a été ajoutée.', $newRoom->name)); return $this->redirectToRoute('admin_event_room', [ 'id' => $event->getId(), @@ -74,13 +72,15 @@ public function __invoke(Request $request, AdminEventSelection $eventSelection): } /** + * @param array $rooms + * * @return FormInterface[] */ - private function getFormsForRooms(CollectionInterface $rooms): array + private function getFormsForRooms(array $rooms): array { $forms = []; foreach ($rooms as $room) { - $forms[] = $this->formFactory->createNamedBuilder('edit_room_' . $room->getId(), RoomType::class, + $forms[] = $this->formFactory->createNamedBuilder('edit_room_' . $room->id, RoomType::class, $room)->getForm(); } diff --git a/sources/AppBundle/Controller/Admin/Event/Session/EditAction.php b/sources/AppBundle/Controller/Admin/Event/Session/EditAction.php index f49806c72..df3dd7b0b 100644 --- a/sources/AppBundle/Controller/Admin/Event/Session/EditAction.php +++ b/sources/AppBundle/Controller/Admin/Event/Session/EditAction.php @@ -5,13 +5,13 @@ namespace AppBundle\Controller\Admin\Event\Session; use AppBundle\AuditLog\Audit; +use AppBundle\Event\Entity\Repository\RoomRepository; +use AppBundle\Event\Entity\Room; use AppBundle\Event\Model\Event; use AppBundle\Event\Model\Planning; use AppBundle\Event\Model\Repository\EventRepository; use AppBundle\Event\Model\Repository\PlanningRepository; -use AppBundle\Event\Model\Repository\RoomRepository; use AppBundle\Event\Model\Repository\TalkRepository; -use AppBundle\Event\Model\Room; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\DateTimeType; @@ -134,7 +134,7 @@ private function roomChoices(Event $event): array $rooms = $this->roomRepository->getByEvent($event); /** @var Room $room */ foreach ($rooms as $room) { - $roomChoices[$room->getName()] = $room->getId(); + $roomChoices[$room->name] = $room->id; } return $roomChoices; diff --git a/sources/AppBundle/Controller/Admin/Event/Session/IndexAction.php b/sources/AppBundle/Controller/Admin/Event/Session/IndexAction.php index 4960342b6..9018abc13 100644 --- a/sources/AppBundle/Controller/Admin/Event/Session/IndexAction.php +++ b/sources/AppBundle/Controller/Admin/Event/Session/IndexAction.php @@ -5,11 +5,11 @@ namespace AppBundle\Controller\Admin\Event\Session; use AppBundle\Event\AdminEventSelection; +use AppBundle\Event\Entity\Repository\RoomRepository; +use AppBundle\Event\Entity\Room; use AppBundle\Event\Model\Event; use AppBundle\Event\Model\Planning; -use AppBundle\Event\Model\Repository\RoomRepository; use AppBundle\Event\Model\Repository\TalkRepository; -use AppBundle\Event\Model\Room; use AppBundle\Event\Model\Session\CalendarEvent; use AppBundle\Event\Model\Session\CalendarResource; use AppBundle\Event\Model\TalkAggregate; @@ -54,8 +54,8 @@ private function calendarResources(Event $event): array /** @var Room $room */ foreach ($rooms as $i => $room) { $resources[] = new CalendarResource( - $room->getId(), - $room->getName(), + $room->id, + $room->name, $colors[$i % $m], ); } diff --git a/sources/AppBundle/Event/Entity/Repository/RoomRepository.php b/sources/AppBundle/Event/Entity/Repository/RoomRepository.php new file mode 100644 index 000000000..50ecf72ef --- /dev/null +++ b/sources/AppBundle/Event/Entity/Repository/RoomRepository.php @@ -0,0 +1,29 @@ + + */ +final class RoomRepository extends EntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Room::class); + } + + /** + * @return list + */ + public function getByEvent(Event $event): array + { + return array_values($this->findBy(['eventId' => $event->getId()])); + } +} diff --git a/sources/AppBundle/Event/Entity/Room.php b/sources/AppBundle/Event/Entity/Room.php new file mode 100644 index 000000000..479304368 --- /dev/null +++ b/sources/AppBundle/Event/Entity/Room.php @@ -0,0 +1,27 @@ +get(RoomRepository::class); + $event = new Event(); + $event->setId(4242); + + // Deux salles : une pour l'événement testé, l'autre pour un événement voisin + $room = new Room(); + $room->name = 'La salle de test'; + $room->eventId = 4242; + $roomRepository->save($room); + + $autre = new Room(); + $autre->name = 'La salle ailleurs'; + $autre->eventId = 99; + $roomRepository->save($autre); + + // L'auto-incrément remplit l'identifiant à la sauvegarde + self::assertNotNull($room->id); + self::assertGreaterThan(0, $room->id); + + $rooms = $roomRepository->getByEvent($event); + self::assertCount(1, $rooms); + self::assertSame('La salle de test', $rooms[0]->name); + self::assertSame($room->id, $rooms[0]->id); + + // Mise à jour puis vidage de l'EntityManager pour vérifier la persistance en base + $room->name = 'La salle renommée'; + $roomRepository->save($room); + $roomRepository->getEntityManager()->clear(); + + $reloaded = $roomRepository->find($room->id); + self::assertInstanceOf(Room::class, $reloaded); + self::assertSame('La salle renommée', $reloaded->name); + self::assertSame(4242, $reloaded->eventId); + + $roomRepository->delete($reloaded); + + self::assertNull($roomRepository->find($room->id)); + self::assertCount(0, $roomRepository->getByEvent($event)); + } +}