Skip to content

Commit 4574a7c

Browse files
committed
3 commit
1 parent 1e9b4dc commit 4574a7c

File tree

1 file changed

+47
-44
lines changed

1 file changed

+47
-44
lines changed

src/Formatters/Stylish.php

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,45 @@
66

77
function formatStylish(array $diff): string
88
{
9-
return buildOutput($diff);
9+
return iter($diff, 1);
1010
}
1111

12-
function buildOutput(array $diff, int $depth = 0): string
12+
function iter(array $diff, int $depth): string
1313
{
14-
$lines = array_map(function ($node) use ($depth) {
15-
return formatNode($node, $depth);
14+
$indent = buildIndent($depth);
15+
$bracketIndent = buildIndent($depth - 1);
16+
17+
$lines = array_map(function ($node) use ($depth, $indent) {
18+
$type = $node['type'];
19+
$key = $node['key'];
20+
21+
switch ($type) {
22+
case 'added':
23+
$value = stringify($node['value'], $depth);
24+
return "{$indent}+ {$key}: {$value}";
25+
case 'removed':
26+
$value = stringify($node['value'], $depth);
27+
return "{$indent}- {$key}: {$value}";
28+
case 'unchanged':
29+
$value = stringify($node['value'], $depth);
30+
return "{$indent} {$key}: {$value}";
31+
case 'changed':
32+
$oldValue = stringify($node['oldValue'], $depth);
33+
$newValue = stringify($node['newValue'], $depth);
34+
return "{$indent}- {$key}: {$oldValue}\n{$indent}+ {$key}: {$newValue}";
35+
case 'nested':
36+
$children = iter($node['children'], $depth + 1);
37+
return "{$indent} {$key}: {$children}";
38+
default:
39+
throw new \Exception("Unknown type: {$type}");
40+
}
1641
}, $diff);
1742

18-
return "{\n" . implode("\n", $lines) . "\n" . indent($depth) . "}";
19-
}
20-
21-
function formatNode(array $node, int $depth): string
22-
{
23-
$key = $node['key'];
24-
25-
switch ($node['type']) {
26-
case 'nested':
27-
$children = buildOutput($node['children'], $depth + 1);
28-
return indent($depth) . " {$key}: {$children}";
29-
30-
case 'added':
31-
$value = stringify($node['value'], $depth + 1);
32-
return indent($depth) . "+ {$key}: {$value}";
33-
34-
case 'removed':
35-
$value = stringify($node['value'], $depth + 1);
36-
return indent($depth) . "- {$key}: {$value}";
37-
38-
case 'changed':
39-
$oldValue = stringify($node['oldValue'], $depth + 1);
40-
$newValue = stringify($node['newValue'], $depth + 1);
41-
return indent($depth) . "- {$key}: {$oldValue}\n" .
42-
indent($depth) . "+ {$key}: {$newValue}";
43-
44-
case 'unchanged':
45-
$value = stringify($node['value'], $depth + 1);
46-
return indent($depth) . " {$key}: {$value}";
47-
48-
default:
49-
throw new \Exception("Unknown node type: {$node['type']}");
50-
}
43+
return implode("\n", [
44+
"{",
45+
...$lines,
46+
"{$bracketIndent}}"
47+
]);
5148
}
5249

5350
function stringify(mixed $value, int $depth): string
@@ -64,16 +61,22 @@ function stringify(mixed $value, int $depth): string
6461
return (string) $value;
6562
}
6663

67-
$lines = [];
68-
foreach ($value as $key => $val) {
69-
$formattedValue = stringify($val, $depth + 1);
70-
$lines[] = indent($depth) . " {$key}: {$formattedValue}";
71-
}
64+
$indent = buildIndent($depth);
65+
$bracketIndent = buildIndent($depth - 1);
7266

73-
return "{\n" . implode("\n", $lines) . "\n" . indent($depth - 1) . " }";
67+
$lines = array_map(function ($key, $val) use ($depth, $indent) {
68+
$formattedValue = stringify($val, $depth + 1);
69+
return "{$indent} {$key}: {$formattedValue}";
70+
}, array_keys($value), $value);
71+
72+
return implode("\n", [
73+
"{",
74+
...$lines,
75+
"{$bracketIndent} }"
76+
]);
7477
}
7578

76-
function indent(int $depth): string
79+
function buildIndent(int $depth): string
7780
{
7881
return str_repeat(' ', $depth);
7982
}

0 commit comments

Comments
 (0)