Skip to content

Commit

Permalink
fix: errors after phpstan lint
Browse files Browse the repository at this point in the history
  • Loading branch information
kolotov committed Sep 9, 2023
1 parent 866b4a5 commit 92bd19d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 deletions.
15 changes: 8 additions & 7 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,29 @@ function runBrainGame(string $moduleName): void
runGame($module);
}

function loadTexts($module): array
function loadTexts(array $module): array
{
$textsFile = __DIR__ . '/texts.json';
if (!file_exists($textsFile)) {
throw new RuntimeException("Config $textsFile not found");
}

try {
$texts = json_decode(file_get_contents($textsFile), true, 512, JSON_THROW_ON_ERROR);
$content = file_get_contents($textsFile);
$texts = $content ? json_decode($content, true, 512, JSON_THROW_ON_ERROR) : '';
} catch (JsonException $e) {
}

if (empty($texts)) {
$isEmptyConfig = !isset($texts) || !$texts;
if ($isEmptyConfig) {
throw new RuntimeException("Config $textsFile wasn't loaded");
}

$module[LOCATION_TEXTS] = $texts;
return $module;
}

function buildGame($moduleName): array
function buildGame(string $moduleName): array
{
$module = [];
$moduleFile = __DIR__ . '/Games/' . $moduleName . '.php';
Expand All @@ -60,7 +62,7 @@ function buildGame($moduleName): array
function greetUser(array $module): array
{
line(getText($module, 'dialogs.welcome'));
$userName = prompt(getText($module, 'prompts.ask_name') . ' ', false, '');
$userName = prompt(getText($module, 'prompts.ask_name') . ' ', '', '');
$module = setUserName($module, $userName);

line(getText($module, 'dialogs.greeting', ['[user_name]' => $userName]));
Expand All @@ -74,9 +76,8 @@ function runGame(array $module, int $correctAnswerCounter = 0): void

[$question, $correctAnswer] = getQuestionAnswerPairHandler($module);
line(getText($module, 'prompts.question', ['[question]' => $question]));

$userAnswer = prompt(getText($module, 'prompts.answer'));
$isValidAnswer = (string)$correctAnswer === $userAnswer;
$isValidAnswer = $correctAnswer === $userAnswer;

/** Game over */
if (!$isValidAnswer) {
Expand Down
2 changes: 1 addition & 1 deletion src/Games/CalcGame.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\SETTING_RULES;

function loader($module): array
function loader(array $module): array
{
$module[SETTING_RULES] = 'What is the result of the expression?';
$module[HANDLER_QUESTION] = static fn() => handler();
Expand Down
2 changes: 1 addition & 1 deletion src/Games/EvenGame.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\SETTING_RULES;

function loader($module): array
function loader(array $module): array
{
$module[SETTING_RULES] = 'Answer "yes" if the number is even, otherwise answer "no".';
$module[HANDLER_QUESTION] = static fn() => handler();
Expand Down
4 changes: 2 additions & 2 deletions src/Games/GcdGame.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\SETTING_RULES;

function loader($module): array
function loader(array $module): array
{
$module[SETTING_RULES] = 'Find the greatest common divisor of given numbers.';
$module[HANDLER_QUESTION] = static fn() => handler();
Expand All @@ -21,7 +21,7 @@ function handler(): array
return [$question, $expectedAnswer];
}

function gcd($a, $b): int
function gcd(int $a, int $b): int
{
return $b === 0 ? $a : gcd($b, $a % $b);
}
4 changes: 2 additions & 2 deletions src/Games/PrimeGame.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\SETTING_RULES;

function loader($module): array
function loader(array $module): array
{
$module[SETTING_RULES] = 'Answer "yes" if given number is prime. Otherwise answer "no".';
$module[HANDLER_QUESTION] = static fn() => handler();
Expand All @@ -19,7 +19,7 @@ function handler(): array
return [$question, $isPrime ? 'yes' : 'no'];
}

function isPrime($number): bool
function isPrime(int $number): bool
{
if ($number <= 1) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Games/ProgressionGame.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\SETTING_RULES;

function loader($module): array
function loader(array $module): array
{
$module[SETTING_RULES] = 'What number is missing in the progression?';
$module[HANDLER_QUESTION] = static fn() => handler();
Expand Down
15 changes: 8 additions & 7 deletions src/Utils/ModuleUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,37 @@
* 1: string
* }
*/
function getQuestionAnswerPairHandler($module): array
function getQuestionAnswerPairHandler(array $module): array
{
return $module[HANDLER_QUESTION]();
}

function getRulesDescription($module): string
function getRulesDescription(array $module): string
{
return $module[SETTING_RULES];
}

function getText($module, $key, $replace_pairs = []): string
function getText(array $module, string $key, array $replacePairs = []): string
{
$texts = getTexts($module);
$keys = explode('.', $key);
/** @var string $text */
$text = array_reduce($keys, fn($tail, $key) => is_array($tail) ? ($tail[$key] ?? '') : $tail, $texts);
return strtr($text, $replace_pairs);
return strtr($text, $replacePairs);
}

function setUserName($module, $userName): array
function setUserName(array $module, string $userName): array
{
$module[LOCATION_DATA][DATA_USER_NAME] = $userName;
return $module;
}

function getUserName($module): string
function getUserName(array $module): string
{
return $module[LOCATION_DATA][DATA_USER_NAME];
}

function getTexts($module): array
function getTexts(array $module): array
{
return $module[LOCATION_TEXTS];
}

0 comments on commit 92bd19d

Please sign in to comment.