Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
ccailly committed Apr 26, 2024
1 parent 79d4121 commit 1d23d5d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
3 changes: 1 addition & 2 deletions ajax/form/answer.php
Expand Up @@ -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');
Expand All @@ -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'));
}
Expand Down
15 changes: 8 additions & 7 deletions src/Form/EndUserInputNameProvider.php
Expand Up @@ -36,7 +36,7 @@
namespace Glpi\Form;

/**
* Helpdesk form
* Utility class to provide the end user input name
*/
final class EndUserInputNameProvider
{
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Question.php
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/Glpi/Form/EndUserInputNameProvider.php
Expand Up @@ -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',
Expand Down

0 comments on commit 1d23d5d

Please sign in to comment.