From c0fabf9433a8e2d9d9d5dbd9ec95b16b3520bb2b Mon Sep 17 00:00:00 2001 From: ImmortaL Date: Tue, 2 Apr 2024 19:10:02 +0300 Subject: [PATCH] apply some feedback (docs, unused prop and getter for dangling model, add more types for args) --- app/src/Controller/IndexController.php | 6 +++--- app/src/Entity/UserAnswer.php | 3 --- app/src/Form/Type/UserAnswerType.php | 3 +-- app/src/Model/AnswerModel.php | 12 +++--------- app/src/Model/QuestionModel.php | 8 ++++---- app/src/Service/QuestionService.php | 6 +++--- app/templates/index/results.html.twig | 20 +++++++++++++++----- 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/app/src/Controller/IndexController.php b/app/src/Controller/IndexController.php index 2ff6fff..f6dd0ca 100644 --- a/app/src/Controller/IndexController.php +++ b/app/src/Controller/IndexController.php @@ -49,7 +49,6 @@ class IndexController extends AbstractController $session->set('step', $step); $form = $this->getForm($test, $step); } else { - $session->set('test_' . $test->getId(), true); $session->remove('step'); $oldUuid = $this->getUuid(); $this->getUuid(true); @@ -77,7 +76,8 @@ class IndexController extends AbstractController return $this->render('index/results.html.twig',[ 'test' => $test, - 'existingAnswers' => $existingAnswers, + 'correctAnswers' => array_filter($existingAnswers, fn(UserAnswer $a) => $a->isCorrect()), + 'incorrectAnswers' => array_filter($existingAnswers, fn(UserAnswer $a) => !$a->isCorrect()), 'service' => $this->questionService, 'uuid' => $uuid, ]); @@ -97,7 +97,7 @@ class IndexController extends AbstractController ]); } - private function getUuid($forceNew = false) + private function getUuid(bool $forceNew = false) { $session = $this->requestStack->getCurrentRequest()->getSession(); if (!$session->has('uuid') || $forceNew) { diff --git a/app/src/Entity/UserAnswer.php b/app/src/Entity/UserAnswer.php index 88783d7..e2551fd 100644 --- a/app/src/Entity/UserAnswer.php +++ b/app/src/Entity/UserAnswer.php @@ -72,9 +72,6 @@ class UserAnswer return $this; } - /** - * @return array - */ public function getAnswer(): array { return $this->answer; diff --git a/app/src/Form/Type/UserAnswerType.php b/app/src/Form/Type/UserAnswerType.php index 21c3fce..c027066 100644 --- a/app/src/Form/Type/UserAnswerType.php +++ b/app/src/Form/Type/UserAnswerType.php @@ -22,10 +22,9 @@ class UserAnswerType extends AbstractType public function __construct(private readonly QuestionService $questionService) { - } - public function buildForm(FormBuilderInterface $builder, array $options) + public function buildForm(FormBuilderInterface $builder, array $options): void { /** @var QuestionModel $questionModel */ $questionModel = $options['questionModel']; diff --git a/app/src/Model/AnswerModel.php b/app/src/Model/AnswerModel.php index 87f46be..9e961fd 100644 --- a/app/src/Model/AnswerModel.php +++ b/app/src/Model/AnswerModel.php @@ -2,12 +2,11 @@ namespace App\Model; -final class AnswerModel +final readonly class AnswerModel { public function __construct( - private readonly string $name, - private readonly string $value, - private readonly array $config + private string $name, + private array $config ) { @@ -18,11 +17,6 @@ final class AnswerModel return $this->name; } - public function getValue(): string - { - return $this->value; - } - public function getConfig(): array { return $this->config; diff --git a/app/src/Model/QuestionModel.php b/app/src/Model/QuestionModel.php index e617378..d830ff6 100644 --- a/app/src/Model/QuestionModel.php +++ b/app/src/Model/QuestionModel.php @@ -2,11 +2,11 @@ namespace App\Model; -final class QuestionModel +final readonly class QuestionModel { public function __construct( - private readonly string $prompt, - private readonly array $answers + private string $prompt, + private array $answers ) { @@ -18,7 +18,7 @@ final class QuestionModel } /** - * @return + * @return array */ public function getAnswers(): array { diff --git a/app/src/Service/QuestionService.php b/app/src/Service/QuestionService.php index 3b3760f..ad6748b 100644 --- a/app/src/Service/QuestionService.php +++ b/app/src/Service/QuestionService.php @@ -14,7 +14,7 @@ class QuestionService { $questionAnswers = $question->getAnswers(); $answers = array_map( - fn($answer, $i) => new AnswerModel($this->assembleString($answer), $i, $answer), + fn($answer, $i) => new AnswerModel($this->assembleString($answer), $answer), $questionAnswers, array_keys($questionAnswers) ); @@ -92,7 +92,7 @@ class QuestionService $result *= (float) $nextEntry['value']; break; case OperatorsEnum::DIV: - if((double) $nextEntry['value'] === 0.0) { + if((float) $nextEntry['value'] === 0.0) { throw new \RuntimeException('Division by 0'); } $result /= (float) $nextEntry['value']; @@ -108,7 +108,7 @@ class QuestionService return $result; } - public function assembleString(array $entries, $isPrompt = false): string + public function assembleString(array $entries, bool $isPrompt = false): string { $ret = []; foreach($entries as $entry) { diff --git a/app/templates/index/results.html.twig b/app/templates/index/results.html.twig index a2e766c..9484746 100644 --- a/app/templates/index/results.html.twig +++ b/app/templates/index/results.html.twig @@ -7,16 +7,26 @@

You finished trial: {{ test.getName() }}

Your results link
-
    - {% for existingAnswer in existingAnswers %} -
  • - {{ service.getQuestionModel(existingAnswer.question).getPrompt() }} - {% for answer in existingAnswer.getAnswer() %} +
      + {% for correctAnswer in correctAnswers %} +
    • + {{ service.getQuestionModel(correctAnswer.question).getPrompt() }} + {% for answer in correctAnswer.getAnswer() %} {{ service.assembleString(answer) }}{% if not loop.last %},{% endif %} {% endfor %}
    • {% endfor %}
    +
      + {% for incorrectAnswer in incorrectAnswers %} +
    • + {{ service.getQuestionModel(incorrectAnswer.question).getPrompt() }} + {% for answer in incorrectAnswer.getAnswer() %} + {{ service.assembleString(answer) }}{% if not loop.last %},{% endif %} + {% endfor %} +
    • + {% endfor %} +