Skip to content

Commit

Permalink
Refactoring iter() in Plain.php
Browse files Browse the repository at this point in the history
  • Loading branch information
artengin committed Sep 25, 2024
1 parent 6111422 commit 178bb0d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
25 changes: 12 additions & 13 deletions src/Formatters/Plain.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ function render(array $data): string
return rtrim($result, " \n");
}

function iter(mixed $value, array $acc = []): string
function iter(array $value, array $acc = []): string
{
if (!is_array($value)) {
return toString($value);
}
$func = function ($val) use ($acc) {
if (!is_array($val)) {
return toString($val);
}

if (!array_key_exists(0, $value) && !array_key_exists('type', $value)) {
return toString($value);
}
if (!array_key_exists(0, $val) && !array_key_exists('type', $val)) {
return toString($val);
}

$fun = function ($val) use ($acc) {
$key = $val['key'];
$compare = $val['type'];
$compareText = COMPARE_TEXT_MAP[$compare];
Expand All @@ -43,7 +43,7 @@ function iter(mixed $value, array $acc = []): string
"Property '%s' was %s with value: %s\n",
implode('.', $accNew),
$compareText,
iter($val['value'], $accNew),
toString($val['value']),
),
DELETED => sprintf(
"Property '%s' was %s\n",
Expand All @@ -54,16 +54,15 @@ function iter(mixed $value, array $acc = []): string
"Property '%s' was %s. From %s to %s\n",
implode('.', $accNew),
$compareText,
iter($val['value1'], $accNew),
iter($val['value2'], $accNew),
toString($val['value1']),
toString($val['value2']),
),
NESTED => iter($val['children'], $accNew),
default => null,
};
};

$result = array_map($fun, $value);

$result = array_map($func, $value);
return implode($result);
}

Expand Down
10 changes: 6 additions & 4 deletions src/Formatters/Stylish.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ function stringify(mixed $value, int $depth): string

function toString(mixed $value): string
{
if (is_null($value)) {
return 'null';
}
return trim(var_export($value, true), "'");
return match (true) {
$value === true => 'true',
$value === false => 'false',
is_null($value) => 'null',
default => trim((string) $value, "'")
};
}

0 comments on commit 178bb0d

Please sign in to comment.