Skip to content

Commit

Permalink
corrected that key difference content only value without key, and old…
Browse files Browse the repository at this point in the history
… and new type content keys old and new in value key
  • Loading branch information
vladimir-xz committed Mar 2, 2024
1 parent 9c91a11 commit 15372d6
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 152 deletions.
15 changes: 9 additions & 6 deletions src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,40 @@

function addNewLine(string $key, mixed $value): array
{
return ['type' => 'added', 'key' => $key, 'difference' => [$key => $value]];
return ['type' => 'added', 'key' => $key, 'value' => $value];
}

function addDeletedLine(string $key, mixed $value): array
{
return ['type' => 'deleted', 'key' => $key, 'difference' => [$key => $value]];
return ['type' => 'deleted', 'key' => $key, 'value' => $value];
}

function addSameLine(string $key, mixed $value): array
{
return ['type' => 'same', 'key' => $key, 'difference' => [$key => $value]];
return ['type' => 'same', 'key' => $key, 'value' => $value];
}

function addChangedLine(string $key, mixed $value): array
{
return ['type' => 'changed', 'key' => $key, 'difference' => $value ];
return ['type' => 'changed', 'key' => $key, 'value' => $value ];
}

function addOldAndNew(string $commonKey, mixed $old, mixed $new): array
{
return ['type' => 'old and new',
'key' => $commonKey,
'difference' => [addDeletedLine($commonKey, $old), addNewLine($commonKey, $new)]];
'value' => [
'oldValue' => $old,
'newValue' => $new
]];
}

function getNode(mixed $value): array
{
return [
'type' => $value['type'],
'key' => $value['key'],
'difference' => $value['difference']
'value' => $value['value']
];
}

Expand Down
18 changes: 7 additions & 11 deletions src/Formatters/Plain.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,25 @@ function printValuePlain(mixed $value): string
function format(array $comparedArray, array $tempForKeys = []): string
{
$differencies = array_map(function ($node) use ($tempForKeys) {
['type' => $type, 'key' => $key, 'difference' => $difference] = getNode($node);
['type' => $type, 'key' => $key, 'value' => $value] = getNode($node);
$newKeys = array_merge($tempForKeys, [$key]);
$keyToPrint = implode('.', $newKeys);
switch ($type) {
case 'old and new':
$oldAndNewValues = array_map(function ($node) {
['type' => $type, 'difference' => $difference] = getNode($node);
$valueToPrint = printValuePlain(current($difference));
return [$type => $valueToPrint];
}, $difference);
$bothValues = array_merge(...$oldAndNewValues);
return "Property '{$keyToPrint}' was updated. From {$bothValues['deleted']} to {$bothValues['added']}";
$oldValue = printValuePlain($value['oldValue']);
$newValue = printValuePlain($value['newValue']);
return "Property '{$keyToPrint}' was updated. From {$oldValue} to {$newValue}";
case 'changed':
return format($difference, $newKeys);
return format($value, $newKeys);
case 'same':
break;
case 'added':
$valueString = printValuePlain(current($difference));
$valueString = printValuePlain($value);
return "Property '{$keyToPrint}' was added with value: {$valueString}";
case 'deleted':
return "Property '{$keyToPrint}' was removed";
default:
throw new \Exception("Unknown status of value: \"{$type}\"!");
throw new \Exception("Unknown type of value: \"{$type}\"!");
}
}, $comparedArray);
$withoutEmpties = array_filter($differencies, fn ($array) => $array);
Expand Down
59 changes: 35 additions & 24 deletions src/Formatters/Stylish.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,26 @@
'old and new' => '',
];

function stringify(mixed $item, int $depth, int $offset = 2, string $separator = ' '): string
function createEmptySpace(int $depth, int $offset = 0, string $separator = ' ')
{
$emptySpaceWithoutSymbol = str_repeat($separator, $depth);
if (!$offset) {
return $emptySpaceWithoutSymbol;
}
return substr($emptySpaceWithoutSymbol, $offset, null);
}

function stringify(mixed $item, int $depth): string
{
if (!is_array($item)) {
$itemString = is_string($item) ? $item : var_export($item, true);
return $itemString === 'NULL' ? 'null' : $itemString;
}
$emptySpace = str_repeat($separator, $depth);
$lines = array_map(function ($key, $value) use ($depth, $separator, $offset) {
$emptySpace = createEmptySpace($depth);
$lines = array_map(function ($key, $value) use ($depth) {
$nextDepth = $depth + 1;
$emptySpace = substr(str_repeat($separator, $nextDepth), $offset, null);
$valueString = stringify($value, $nextDepth, $offset);
$emptySpace = createEmptySpace($nextDepth);
$valueString = stringify($value, $nextDepth);
return "{$emptySpace}{$key}: {$valueString}";
}, array_keys($item), $item);
$linesWithBrackets = ['{', ...$lines, "{$emptySpace}}"];
Expand All @@ -31,23 +40,25 @@ function stringify(mixed $item, int $depth, int $offset = 2, string $separator =

function format(array $comparedData, int $depth = 0): string
{
$iter = function ($comparedData) use (&$iter, $depth) {
$result = array_map(function ($data) use ($iter, $depth) {
['type' => $type, 'key' => $key, 'difference' => $difference] = getNode($data);
$symbol = SYMBOLS[$type];
$keyWithSymbol = "{$symbol} {$key}";
$nextDepth = $depth + 1;
$offsetWithoutSymbol = 0;
if ($type === 'old and new') {
return $iter($difference);
} elseif ($type === 'changed') {
$valueString = format($difference, $nextDepth);
} else {
$valueString = stringify(current($difference), $nextDepth, $offsetWithoutSymbol);
}
return [$keyWithSymbol => $valueString];
}, $comparedData);
return array_merge(...$result);
};
return stringify($iter($comparedData), $depth);
$emptySpace = createEmptySpace($depth);
$lines = array_map(function ($data) use ($depth) {
['type' => $type, 'key' => $key, 'value' => $value] = getNode($data);
$symbol = SYMBOLS[$type];
$nextDepth = $depth + 1;
$offsetForSymbol = 2;
$emptySpace = createEmptySpace($nextDepth, $offsetForSymbol);
$keyForPrint = "{$emptySpace}{$symbol} {$key}";
if ($type === 'old and new') {
$valueOld = stringify($value['oldValue'], $nextDepth);
$valueNew = stringify($value['newValue'], $nextDepth);
return "{$emptySpace}- {$key}: {$valueOld}\n{$emptySpace}+ {$key}: {$valueNew}";
} elseif ($type === 'changed') {
$valueString = format($value, $nextDepth);
} else {
$valueString = stringify($value, $nextDepth);
}
return "{$keyForPrint}: {$valueString}";
}, $comparedData);
$linesWithBrackets = ['{', ...$lines, "{$emptySpace}}"];
return implode("\n", $linesWithBrackets);
}
4 changes: 2 additions & 2 deletions tests/FormattersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testUnknownReportFormatException(): void

public function testUnknownStatusOfValue(): void
{
$this->expectExceptionMessage("Unknown status of value: \"*\"!");
format([['type' => '*', 'key' => 'key', 'difference' => ['key' => 'value']]]);
$this->expectExceptionMessage("Unknown type of value: \"*\"!");
format([['type' => '*', 'key' => 'key', 'value' => 'value']]);
}
}
Loading

0 comments on commit 15372d6

Please sign in to comment.