Refactor for code consistency and type safety assertions across repositories and entities

This commit is contained in:
2026-03-09 23:32:48 +01:00
parent 84902072c6
commit af60a51bb3
5 changed files with 23 additions and 15 deletions
+2
View File
@@ -7,6 +7,8 @@ parameters:
- public/ - public/
- src/ - src/
- tests/ - tests/
excludePaths:
- config/reference.php
treatPhpDocTypesAsCertain: false treatPhpDocTypesAsCertain: false
symfony: symfony:
containerXmlPath: var/cache/dev/Tvdt_KernelDevDebugContainer.xml containerXmlPath: var/cache/dev/Tvdt_KernelDevDebugContainer.xml
+2 -1
View File
@@ -96,6 +96,7 @@ class QuizController extends AbstractController
$fetchedQuiz = $this->quizRepository->fetchWithQuestions($quiz->id); $fetchedQuiz = $this->quizRepository->fetchWithQuestions($quiz->id);
\assert($fetchedQuiz->questions->count() > 0); \assert($fetchedQuiz->questions->count() > 0);
$firstQuestion = $fetchedQuiz->questions->first(); $firstQuestion = $fetchedQuiz->questions->first();
\assert($firstQuestion instanceof Question);
return $this->redirectToRoute('tvdt_backoffice_quiz_candidates_question', [ return $this->redirectToRoute('tvdt_backoffice_quiz_candidates_question', [
'seasonCode' => $season->seasonCode, 'seasonCode' => $season->seasonCode,
@@ -264,7 +265,7 @@ class QuizController extends AbstractController
'candidate' => $candidate, 'candidate' => $candidate,
]); ]);
if (!$quizCandidate instanceof \Tvdt\Entity\QuizCandidate) { if (!$quizCandidate instanceof QuizCandidate) {
// Create new QuizCandidate if it doesn't exist (inactive by default when first toggling) // Create new QuizCandidate if it doesn't exist (inactive by default when first toggling)
$quizCandidate = new QuizCandidate($quiz, $candidate); $quizCandidate = new QuizCandidate($quiz, $candidate);
$quizCandidate->active = false; $quizCandidate->active = false;
+3 -3
View File
@@ -155,9 +155,9 @@ class Quiz
} }
} }
if ($missing !== [] || $duplicates !== []) { if ([] !== $missing || [] !== $duplicates) {
$errors = []; $errors = [];
if ($missing !== []) { if ([] !== $missing) {
// If all active candidates are missing, show a special message // If all active candidates are missing, show a special message
if (\count($missing) === \count($activeCandidates)) { if (\count($missing) === \count($activeCandidates)) {
$errors[] = 'No candidates assigned to this question'; $errors[] = 'No candidates assigned to this question';
@@ -166,7 +166,7 @@ class Quiz
} }
} }
if ($duplicates !== []) { if ([] !== $duplicates) {
$errors[] = 'Duplicate candidates: '.implode(', ', $duplicates); $errors[] = 'Duplicate candidates: '.implode(', ', $duplicates);
} }
+3 -2
View File
@@ -6,6 +6,7 @@ namespace Tvdt\Repository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
use Safe\DateTimeImmutable;
use Tvdt\Entity\Candidate; use Tvdt\Entity\Candidate;
use Tvdt\Entity\Quiz; use Tvdt\Entity\Quiz;
use Tvdt\Entity\QuizCandidate; use Tvdt\Entity\QuizCandidate;
@@ -33,7 +34,7 @@ class QuizCandidateRepository extends ServiceEntityRepository
// If QuizCandidate exists but hasn't started yet, set the started timestamp // If QuizCandidate exists but hasn't started yet, set the started timestamp
if (null === $quizCandidate->started) { if (null === $quizCandidate->started) {
$quizCandidate->started = new \DateTimeImmutable(); $quizCandidate->started = new DateTimeImmutable();
$this->getEntityManager()->flush(); $this->getEntityManager()->flush();
} }
@@ -41,7 +42,7 @@ class QuizCandidateRepository extends ServiceEntityRepository
} }
$quizCandidate = new QuizCandidate($quiz, $candidate); $quizCandidate = new QuizCandidate($quiz, $candidate);
$quizCandidate->started = new \Safe\DateTimeImmutable(); $quizCandidate->started = new DateTimeImmutable();
$this->getEntityManager()->persist($quizCandidate); $this->getEntityManager()->persist($quizCandidate);
$this->getEntityManager()->flush(); $this->getEntityManager()->flush();
+6 -2
View File
@@ -96,7 +96,10 @@ class QuizRepository extends ServiceEntityRepository
DQL DQL
)->setParameter('quiz', $quiz)->getResult(); )->setParameter('quiz', $quiz)->getResult();
return array_map(static fn (array $row): Result => new Result( return array_map(static function (array $row): Result {
\assert($row['start_time'] instanceof \DateTimeImmutable);
return new Result(
id: $row['id'], id: $row['id'],
name: $row['name'], name: $row['name'],
correct: (int) $row['correct'], correct: (int) $row['correct'],
@@ -104,7 +107,8 @@ class QuizRepository extends ServiceEntityRepository
penaltySeconds: $row['penaltySeconds'], penaltySeconds: $row['penaltySeconds'],
time: $row['start_time']->diff(new DateTimeImmutable($row['end_time'])), time: $row['start_time']->diff(new DateTimeImmutable($row['end_time'])),
score: $row['score'], score: $row['score'],
), $result); );
}, $result);
} }
public function fetchWithQuestions(Uuid $id): Quiz public function fetchWithQuestions(Uuid $id): Quiz