From 1d23d5d2f2109a7eb170f805d3c6a43072ae6b69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Cailly?= Date: Fri, 26 Apr 2024 11:57:08 +0200 Subject: [PATCH] Apply suggestions from code review --- ajax/form/answer.php | 3 +-- src/Form/EndUserInputNameProvider.php | 15 ++++++++------- src/Form/Question.php | 2 +- .../Glpi/Form/EndUserInputNameProvider.php | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/ajax/form/answer.php b/ajax/form/answer.php index 5f74f80bad2..5fc9963d5a1 100644 --- a/ajax/form/answer.php +++ b/ajax/form/answer.php @@ -36,7 +36,6 @@ use Glpi\Form\AnswersHandler\AnswersHandler; use Glpi\Form\EndUserInputNameProvider; use Glpi\Form\Form; -use Glpi\Form\Question; use Glpi\Http\Response; include('../../inc/includes.php'); @@ -60,7 +59,7 @@ } // Validate the 'answers' parameter by filtering and reindexing the $_POST array. -$answers = EndUserInputNameProvider::getAnswers(); +$answers = (new EndUserInputNameProvider())->getAnswers($_POST); if (empty($answers)) { Response::sendError(400, __('Invalid answers')); } diff --git a/src/Form/EndUserInputNameProvider.php b/src/Form/EndUserInputNameProvider.php index 1b8d9dcd99c..abf23ab2112 100644 --- a/src/Form/EndUserInputNameProvider.php +++ b/src/Form/EndUserInputNameProvider.php @@ -36,7 +36,7 @@ namespace Glpi\Form; /** - * Helpdesk form + * Utility class to provide the end user input name */ final class EndUserInputNameProvider { @@ -49,20 +49,21 @@ final class EndUserInputNameProvider * @param Question $question * @return string */ - public static function getEndUserInputName(Question $question): string + public function getEndUserInputName(Question $question): string { - return sprintf(static::END_USER_INPUT_NAME, $question->getID()); + return sprintf(self::END_USER_INPUT_NAME, $question->getID()); } /** * Get the answers submitted by the end user * The answers are indexed by question ID * + * @param array $inputs The inputs submitted by the end user * @return array */ - public static function getAnswers(): array + public function getAnswers(array $inputs): array { - $filteredAnswers = self::filterAnswers($_POST); + $filteredAnswers = self::filterAnswers($inputs); $reindexedAnswers = self::reindexAnswers($filteredAnswers); return $reindexedAnswers; @@ -75,7 +76,7 @@ public static function getAnswers(): array * @param array $answers * @return array */ - private static function filterAnswers(array $answers): array + private function filterAnswers(array $answers): array { return array_filter( $answers, @@ -93,7 +94,7 @@ function ($key) { * @param array $answers * @return array */ - private static function reindexAnswers(array $answers): array + private function reindexAnswers(array $answers): array { return array_reduce( array_keys($answers), diff --git a/src/Form/Question.php b/src/Form/Question.php index ed812a14218..de161803c35 100644 --- a/src/Form/Question.php +++ b/src/Form/Question.php @@ -123,7 +123,7 @@ protected function getForm(): Form public function getEndUserInputName(): string { - return EndUserInputNameProvider::getEndUserInputName($this); + return (new EndUserInputNameProvider())->getEndUserInputName($this); } public function prepareInputForAdd($input) diff --git a/tests/functional/Glpi/Form/EndUserInputNameProvider.php b/tests/functional/Glpi/Form/EndUserInputNameProvider.php index f8b17ec2d59..7e13a6fbc46 100644 --- a/tests/functional/Glpi/Form/EndUserInputNameProvider.php +++ b/tests/functional/Glpi/Form/EndUserInputNameProvider.php @@ -71,14 +71,14 @@ public function testGetAnswers() ); // Generate the answers - $_POST = [ + $inputs = [ $form->getQuestions()[array_keys($form->getQuestions())[0]]->getEndUserInputName() => 'John Doe', $form->getQuestions()[array_keys($form->getQuestions())[1]]->getEndUserInputName() => 'john.doe@mail.mail', 'invalid_input' => 'invalid_value', ]; // Check that the answers are correctly indexed by question ID - $this->array(\Glpi\Form\EndUserInputNameProvider::getAnswers()) + $this->array((new \Glpi\Form\EndUserInputNameProvider())->getAnswers($inputs)) ->hasSize(2) ->isEqualTo([ $form->getQuestions()[array_keys($form->getQuestions())[0]]->getID() => 'John Doe',