Skip to content

Commit

Permalink
fix all the functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Smol-An committed Sep 21, 2023
1 parent fd8c992 commit 7deefd7
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 99 deletions.
40 changes: 18 additions & 22 deletions src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,69 @@
use function Differ\Parsers\parse;
use function Differ\Formatters\getFormattedDiff;

function findDiff(array $data1, array $data2): array
function buildDiff(array $data1, array $data2): array
{
$keys = array_unique(array_merge(array_keys($data1), array_keys($data2)));
$sortedKeys = sort($keys, fn($a, $b) => strcmp($a, $b), true);
$sortedKeys = sort($keys, fn($a, $b) => strcmp($a, $b));

$diff = array_map(function ($key) use ($data1, $data2) {
if (!array_key_exists($key, $data1)) {
return [
'key' => $key,
'status' => 'added',
'value' => $data2[$key]
];
} elseif (!array_key_exists($key, $data2)) {
return [
'key' => $key,
'status' => 'removed',
'value' => $data1[$key]
];
} elseif (is_array($data1[$key]) && is_array($data2[$key])) {
return [
'key' => $key,
'status' => 'nested',
'children' => findDiff($data1[$key], $data2[$key])
'children' => buildDiff($data1[$key], $data2[$key])
];
} elseif ($data1[$key] !== $data2[$key]) {
return [
'key' => $key,
'status' => 'updated',
'oldValue' => $data1[$key],
'newValue' => $data2[$key],
];
} else {
return [
'key' => $key,
'status' => 'unchanged',
'value' => $data1[$key]
];
}
}, $sortedKeys);

return array_combine($sortedKeys, $diff);
return $diff;
}

function genDiff(string $pathToFile1, string $pathToFile2, string $formatName = 'stylish'): string
{
$data1 = parse(
getFileContents($pathToFile1),
getFileFormat($pathToFile1)
getFileData($pathToFile1),
pathinfo($pathToFile1, PATHINFO_EXTENSION)
);
$data2 = parse(
getFileContents($pathToFile2),
getFileFormat($pathToFile2)
getFileData($pathToFile2),
pathinfo($pathToFile2, PATHINFO_EXTENSION)
);

$diff = findDiff($data1, $data2);
$diff = buildDiff($data1, $data2);
return getFormattedDiff($diff, $formatName);
}

function getFileContents(string $pathToFile)
function getFileData(string $pathToFile): string
{
$fileContents = $pathToFile[0] === '/'
? file_get_contents($pathToFile)
: file_get_contents(__DIR__ . '/../tests/fixtures/' . $pathToFile);

if ($fileContents === false) {
throw new \Exception("Failed to read file: $pathToFile");
if (!file_exists($pathToFile)) {
return throw new \Exception("File not found: '$pathToFile'");
}

return $fileContents;
}

function getFileFormat(string $pathToFile)
{
return pathinfo($pathToFile, PATHINFO_EXTENSION);
return file_get_contents($pathToFile);
}
6 changes: 3 additions & 3 deletions src/Formatters.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
function getFormattedDiff(array $diff, string $formatName): string
{
switch ($formatName) {
case 'stylish':
return getStylishDiff($diff);
case 'plain':
return getPlainDiff($diff);
case 'json':
return getJsonDiff($diff);
case 'stylish':
return getStylishDiff($diff);
default:
throw new \Exception("Unknown format: $formatName");
throw new \Exception("Unknown format: '$formatName'");
}
}
59 changes: 28 additions & 31 deletions src/Formatters/Plain.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,34 @@

namespace Differ\Formatters\Plain;

function genPlainDiff(array $diff, array $parentKeys = []): array
use function Functional\flatten;

function buildPlainDiff(array $diff, array $parentKeys = []): array
{
return array_merge(
...array_map(function ($key, $node) use ($parentKeys) {
$currentKeys = [...$parentKeys, $key];
$propertyPath = implode('.', $currentKeys);

switch ($node['status']) {
case 'added':
$formattedValue = formatValue($node['value']);
return ["Property '{$propertyPath}' was added with value: {$formattedValue}"];
case 'removed':
return ["Property '{$propertyPath}' was removed"];
case 'updated':
$formattedOldVal = formatValue($node['oldValue']);
$formattedNewVal = formatValue($node['newValue']);
return ["Property '{$propertyPath}' was updated. From {$formattedOldVal} to {$formattedNewVal}"];
case 'nested':
return genPlainDiff($node['children'], $currentKeys);
default:
return [];
}
}, array_keys($diff), $diff)
);
return array_map(function ($node) use ($parentKeys) {
$key = $node['key'];
$currentKeys = [...$parentKeys, $key];
$propertyPath = implode('.', $currentKeys);

switch ($node['status']) {
case 'added':
$formattedValue = formatValue($node['value']);
return ["Property '$propertyPath' was added with value: $formattedValue"];
case 'removed':
return ["Property '$propertyPath' was removed"];
case 'nested':
return buildPlainDiff($node['children'], $currentKeys);
case 'updated':
$formattedOldValue = formatValue($node['oldValue']);
$formattedNewValue = formatValue($node['newValue']);
return ["Property '$propertyPath' was updated. From $formattedOldValue to $formattedNewValue"];
case 'unchanged':
return [];
}
}, $diff);
}

function formatValue(mixed $value)
function formatValue(mixed $value): string
{
if (is_array($value)) {
return '[complex value]';
Expand All @@ -42,15 +43,11 @@ function formatValue(mixed $value)
return $value ? 'true' : 'false';
}

if (is_string($value)) {
return "'$value'";
}

return $value;
return "'$value'";
}

function getPlainDiff(array $diff): string
{
$plainDiff = genPlainDiff($diff);
return implode("\n", $plainDiff);
$plainDiff = buildPlainDiff($diff);
return implode("\n", flatten($plainDiff));
}
75 changes: 38 additions & 37 deletions src/Formatters/Stylish.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,56 @@

namespace Differ\Formatters\Stylish;

function genStylishDiff(array $diff, int $depth = 0): string
function buildStylishDiff(array $diff, int $depth = 0): string
{
$indent = str_repeat(" ", $depth * 4);

$output = array_map(function ($key, $node) use ($depth, $indent) {
$output = array_map(function ($node) use ($depth) {
$indent = getIndent($depth);
$key = $node['key'];
switch ($node['status']) {
case 'added':
$formattedValue = formatValue($node['value'], $depth + 1);
return "{$indent} + {$key}: {$formattedValue}";
$formattedValue = stringify($node['value'], $depth + 1);
return "$indent + $key: $formattedValue";
case 'removed':
$formattedValue = formatValue($node['value'], $depth + 1);
return "{$indent} - {$key}: {$formattedValue}";
case 'updated':
$formattedOldValue = formatValue($node['oldValue'], $depth + 1);
$formattedNewValue = formatValue($node['newValue'], $depth + 1);
return [
"{$indent} - {$key}: {$formattedOldValue}",
"{$indent} + {$key}: {$formattedNewValue}"
];
$formattedValue = stringify($node['value'], $depth + 1);
return "$indent - $key: $formattedValue";
case 'nested':
$nestedDiff = genStylishDiff($node['children'], $depth + 1);
return "{$indent} {$key}: {\n{$nestedDiff}\n{$indent} }";
$nestedDiff = buildStylishDiff($node['children'], $depth + 1);
return "$indent $key: {\n$nestedDiff\n$indent }";
case 'updated':
$formattedOldValue = stringify($node['oldValue'], $depth + 1);
$formattedNewValue = stringify($node['newValue'], $depth + 1);
return "$indent - $key: $formattedOldValue\n$indent + $key: $formattedNewValue";
case 'unchanged':
$formattedValue = formatValue($node['value'], $depth + 1);
return "{$indent} {$key}: {$formattedValue}";
$formattedValue = stringify($node['value'], $depth + 1);
return "$indent $key: $formattedValue";
}
}, array_keys($diff), $diff);
}, $diff);

$flattenedOutput = array_reduce($output, function ($acc, $item) {
if (is_array($item)) {
return array_merge($acc, $item);
} else {
return array_merge($acc, [$item]);
}
}, []);
return implode("\n", $output);
}

return implode("\n", $flattenedOutput);
function getIndent(int $depth = 1, int $spacesCount = 4): string
{
return str_repeat(" ", $spacesCount * $depth);
}

function formatValue(mixed $value, int $depth): string
function stringify(mixed $value, int $depth): string
{
if (is_array($value)) {
$formattedArray = array_map(function ($key, $val) use ($depth) {
$formattedValue = formatValue($val, $depth + 1);
$keyIndent = str_repeat(" ", $depth * 4);
return "{$keyIndent} {$key}: {$formattedValue}";
}, array_keys($value), $value);
return "{\n" . implode("\n", $formattedArray) . "\n" . str_repeat(" ", $depth * 4) . "}";
if (!is_array($value)) {
return formatValue($value);
}

$indent = getIndent($depth);
$formattedArray = array_map(function ($key, $val) use ($depth, $indent) {
$formattedValue = stringify($val, $depth + 1);
return "$indent $key: $formattedValue";
}, array_keys($value), $value);

return "{\n" . implode("\n", $formattedArray) . "\n" . $indent . "}";
}

function formatValue(mixed $value): string
{
if (is_null($value)) {
return 'null';
}
Expand All @@ -65,5 +65,6 @@ function formatValue(mixed $value, int $depth): string

function getStylishDiff(array $diff): string
{
return "{\n" . genStylishDiff($diff) . "\n}";
$stylishDiff = buildStylishDiff($diff);
return "{\n" . $stylishDiff . "\n}";
}
11 changes: 5 additions & 6 deletions src/Parsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

use Symfony\Component\Yaml\Yaml;

function parse(string $dataFile, string $formatFile): array
function parse(string $fileData, string $fileExtension): array
{
switch ($formatFile) {
switch ($fileExtension) {
case 'json':
return json_decode($dataFile, true);
return json_decode($fileData, true);
case 'yml':
return Yaml::parse($dataFile);
case 'yaml':
return Yaml::parse($dataFile);
return Yaml::parse($fileData);
default:
throw new \Exception("Unknown format: $formatFile");
throw new \Exception("Unknown extension: '$fileExtension'");
}
}

0 comments on commit 7deefd7

Please sign in to comment.