Skip to content

Commit

Permalink
fix: psr 12 code style
Browse files Browse the repository at this point in the history
  • Loading branch information
kolotov committed Sep 3, 2023
1 parent ab4f61c commit dae2977
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 112 deletions.
57 changes: 20 additions & 37 deletions src/Games/CalcGame.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace BrainGames\Games\EvenGame;

use Exception;

use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\LOCATION_HANDLERS;
use const BrainGames\Utils\ModuleUtils\LOCATION_SETTINGS;
Expand All @@ -12,41 +10,26 @@
return function ($module) {
$module[LOCATION_SETTINGS][SETTING_RULES] = 'What is the result of the expression?';

$module[LOCATION_HANDLERS][HANDLER_QUESTION] = fn() => questionHandler();
$module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () {
$operands = ['*', '+', '-'];
$firstNum = random_int(1, 100);
$secondNum = random_int(1, 100);
$operand = $operands[random_int(0, count($operands) - 1)];
switch ($operand) {
case '*':
$expectedAnswer = $firstNum * $secondNum;
break;
case '+':
$expectedAnswer = $firstNum + $secondNum;
break;
case '-':
$expectedAnswer = $firstNum - $secondNum;
break;
}

$question = "$firstNum $operand $secondNum";
return [$question, $expectedAnswer];
};

return $module;
};

/**
* Returns a question and its expected answer.
*
* - 0: The question (string)
* - 1: The expected answer (string)
*
* @return array{
* 0: string,
* 1: string
* }
* @throws Exception
*/
function questionHandler(): array
{
$operands = ['*', '+', '-'];
$firstNum = random_int(1, 100);
$secondNum = random_int(1, 100);
$operand = $operands[random_int(0, count($operands) - 1)];
switch ($operand) {
case '*':
$expectedAnswer = $firstNum * $secondNum;
break;
case '+':
$expectedAnswer = $firstNum + $secondNum;
break;
case '-':
$expectedAnswer = $firstNum - $secondNum;
break;
}

$question = "$firstNum $operand $secondNum";
return [$question, $expectedAnswer];
}
27 changes: 5 additions & 22 deletions src/Games/EvenGame.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace BrainGames\Games\EvenGame;

use Exception;

use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\LOCATION_HANDLERS;
use const BrainGames\Utils\ModuleUtils\LOCATION_SETTINGS;
Expand All @@ -12,26 +10,11 @@
return function ($module) {
$module[LOCATION_SETTINGS][SETTING_RULES] = 'Answer "yes" if the number is even, otherwise answer "no".';

$module[LOCATION_HANDLERS][HANDLER_QUESTION] = fn() => questionHandler();
$module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () {
$question = random_int(1, 100);
$isEven = $question % 2 === 0;
return [$question, $isEven ? 'yes' : 'no'];
};

return $module;
};

/**
* Returns a question and its expected answer.
*
* - 0: The question (string)
* - 1: The expected answer (string)
*
* @return array{
* 0: string,
* 1: string
* }
* @throws Exception
*/
function questionHandler(): array
{
$question = random_int(1, 100);
$isEven = $question % 2 === 0;
return [$question, $isEven ? 'yes' : 'no'];
}
33 changes: 8 additions & 25 deletions src/Games/GcdGame.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace BrainGames\Games\EvenGame;

use Exception;

use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\LOCATION_HANDLERS;
use const BrainGames\Utils\ModuleUtils\LOCATION_SETTINGS;
Expand All @@ -12,29 +10,14 @@
return function ($module) {
$module[LOCATION_SETTINGS][SETTING_RULES] = 'Find the greatest common divisor of given numbers.';

$module[LOCATION_HANDLERS][HANDLER_QUESTION] = fn() => questionHandler();
$module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () {
$firstNum = random_int(1, 100);
$secondNum = random_int(1, 100);

$expectedAnswer = gmp_gcd($firstNum, $firstNum);
$question = "$firstNum $secondNum";
return [$question, $expectedAnswer];
};

return $module;
};

/**
* Returns a question and its expected answer.
*
* - 0: The question (string)
* - 1: The expected answer (string)
*
* @return array{
* 0: string,
* 1: string
* }
* @throws Exception
*/
function questionHandler(): array
{
$firstNum = random_int(1, 100);
$secondNum = random_int(1, 100);

$expectedAnswer = gmp_gcd($firstNum, $firstNum);
$question = "$firstNum $secondNum";
return [$question, $expectedAnswer];
}
39 changes: 11 additions & 28 deletions src/Games/ProgressionGame.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace BrainGames\Games\EvenGame;

use Exception;

use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\LOCATION_HANDLERS;
use const BrainGames\Utils\ModuleUtils\LOCATION_SETTINGS;
Expand All @@ -12,32 +10,17 @@
return function ($module) {
$module[LOCATION_SETTINGS][SETTING_RULES] = 'What number is missing in the progression?';

$module[LOCATION_HANDLERS][HANDLER_QUESTION] = fn() => questionHandler();
$module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () {
$lengthProgression = 10;
$step = random_int(2, 10);
$shift = random_int(1, 10);
$progression = range($shift, $shift + (($lengthProgression - 1) * $step), $step);
$guessIndex = random_int(0, 8);
$expectedAnswer = $progression[$guessIndex];
$progression[$guessIndex] = '..';
$question = implode(' ', $progression);
return [$question, $expectedAnswer];
};

return $module;
};

/**
* Returns a question and its expected answer.
*
* - 0: The question (string)
* - 1: The expected answer (string)
*
* @return array{
* 0: string,
* 1: string
* }
* @throws Exception
*/
function questionHandler(): array
{
$lengthProgression = 10;
$step = random_int(2, 10);
$shift = random_int(1, 10);
$progression = range($shift, $shift + (($lengthProgression - 1) * $step), $step);
$guessIndex = random_int(0, 8);
$expectedAnswer = $progression[$guessIndex];
$progression[$guessIndex] = '..';
$question = implode(' ', $progression);
return [$question, $expectedAnswer];
}

0 comments on commit dae2977

Please sign in to comment.