Skip to content

Commit

Permalink
The files have been modified in accordance with the comments of the m…
Browse files Browse the repository at this point in the history
…entor
  • Loading branch information
RasmuS2024 committed Nov 11, 2024
1 parent 556dc1b commit 9747f27
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 49 deletions.
37 changes: 25 additions & 12 deletions src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,41 @@

namespace Differ\Differ;

use function Differ\Formatters\formatSelect;
use function Differ\Parsers\fileParser;
use function Differ\Formatters\getFormattedDiff;
use function Differ\Parsers\parseDataWithFormat;
use function Functional\sort;

function getDataFromFile(string $pathToFile): mixed
{
$fileContent = file_get_contents($pathToFile);
return $fileContent;
}

function getFileExtension(string $pathToFile): string
{
return pathinfo($pathToFile)['extension'] ?? '';
}

function genDiff(mixed $file1Path, mixed $file2Path, string $formatName = 'stylish'): string
{
$data1 = fileParser($file1Path);
$data2 = fileParser($file2Path);
if ($data1 !== false && $data2 !== false) {
$dataDiff = arraysDiffer($data1, $data2);
return formatSelect($dataDiff, $formatName);
$dataFromFile1 = getDataFromFile($file1Path);
$dataFromFile2 = getDataFromFile($file2Path);
if ($dataFromFile1 !== false && $dataFromFile2 !== false) {
$parsedData1 = parseDataWithFormat($dataFromFile1, getFileExtension($file1Path));
$parsedData2 = parseDataWithFormat($dataFromFile2, getFileExtension($file2Path));
$dataDiff = getArraysDiffer($parsedData1, $parsedData2);
return getFormattedDiff($dataDiff, $formatName);
}
return "Parsing of file(s) error!\n";
return "Reading of file(s) error!\n";
}

function getSortedKeys(array $data1, array $data2): array
{
$keys1 = array_keys($data1);
$keys2 = array_keys($data2);
$unionKeys = array_merge($keys1, $keys2);
$keys = array_unique($unionKeys);
$keysSorted = sort($keys, fn ($left, $right) => $left <=> $right);
$uniqueKeys = array_unique($unionKeys);
$keysSorted = sort($uniqueKeys, fn ($left, $right) => $left <=> $right);
return $keysSorted;
}

Expand All @@ -42,7 +55,7 @@ function getValue(mixed $inValue): mixed
return $inValue;
}

function arraysDiffer(mixed $data1, mixed $data2): mixed
function getArraysDiffer(mixed $data1, mixed $data2): mixed
{
$sortedKeys = getSortedKeys($data1, $data2);
$result = array_map(function ($key) use ($data1, $data2) {
Expand All @@ -51,7 +64,7 @@ function arraysDiffer(mixed $data1, mixed $data2): mixed
} elseif (!array_key_exists($key, $data2)) {
return ['type' => '-', 'key' => $key, 'value' => getValue($data1[$key])];
} elseif (is_array($data1[$key]) && is_array($data2[$key])) {
return ['type' => ' ', 'key' => $key, 'value' => arraysDiffer($data1[$key], $data2[$key])];
return ['type' => ' ', 'key' => $key, 'value' => getArraysDiffer($data1[$key], $data2[$key])];
} elseif ($data1[$key] === $data2[$key]) {
return ['type' => ' ', 'key' => $key, 'value' => $data1[$key]];
} else {
Expand Down
23 changes: 9 additions & 14 deletions src/Formatters.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,24 @@
namespace Differ\Formatters;

use function Functional\flatten;
use function Differ\Formatters\Stylish\stylish;
use function Differ\Formatters\Plain\plain;
use function Differ\Formatters\Json\json;
use function Differ\Formatters\Stylish\getStylishFormat;
use function Differ\Formatters\Plain\getPlainFormat;
use function Differ\Formatters\Json\getJsonFormat;

function formatSelect(array $diffSource, string $formatName): string
function getFormattedDiff(array $diffSource, string $formatName): string
{
$result = match ($formatName) {
'stylish' => stylish($diffSource),
'plain' => plain($diffSource),
'json' => json($diffSource),
return match ($formatName) {
'stylish' => getStylishFormat($diffSource),
'plain' => getPlainFormat($diffSource),
'json' => getJsonFormat($diffSource),
default => "Unknown format: \"{$formatName}\"",
};
return $result;
}

function getStringValue(mixed $value, string $format = ''): string
{
if (is_string($value)) {
if ($format === 'plain') {
return "'{$value}'";
} else {
return $value;
}
return $format === 'plain' ? "'{$value}'" : $value;
}
return json_encode($value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Formatters/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Differ\Formatters\Json;

function json(array $tree): string
function getJsonFormat(array $tree): string
{
$json_string = json_encode($tree);
return $json_string;
Expand Down
2 changes: 1 addition & 1 deletion src/Formatters/Plain.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function iter(mixed $value1, string $level = '', mixed $key1 = null): array
return $output;
}

function plain(array $tree): string
function getPlainFormat(array $tree): string
{
$temp = iter($tree, '');
$result = implode('', flatten($temp));
Expand Down
2 changes: 1 addition & 1 deletion src/Formatters/Stylish.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function iter(mixed $value1, int $level = 1): array
return $output;
}

function stylish(array $tree): string
function getStylishFormat(array $tree): string
{
$temp = iter($tree, 1);
$result = getStringFromArray($temp);
Expand Down
25 changes: 6 additions & 19 deletions src/Parsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,12 @@

use Symfony\Component\Yaml\Yaml;

function fileParser(string $pathToFile): mixed
function parseDataWithFormat(string $data, string $format): mixed
{
$pathToFileElements = pathinfo($pathToFile);
if (array_key_exists('extension', $pathToFileElements)) {
$extension = $pathToFileElements['extension'];
switch ($extension) {
case 'json':
$fileContent = file_get_contents($pathToFile);
if ($fileContent !== false) {
$result = json_decode($fileContent, true);
}
break;
case 'yaml':
case 'yml':
$result = Yaml::parseFile($pathToFile);
break;
default:
break;
}
}
$result = match ($format) {
'json' => json_decode($data, true),
'yaml', 'yml' => Yaml::parse($data),
default => null,
};
return $result ?? false;
}
3 changes: 2 additions & 1 deletion tests/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testFilesDiffJsonToJsonFormat(): void
$diffStringFromFiles = genDiff($pathToFile1, $pathToFile2, 'json');
$this->assertStringEqualsFile($pathToFile3, $diffStringFromFiles);
}

/*
public function testFilesNotFound(): void
{
$pathToFile1 = $this->getFixtureFullPath('File.json');
Expand All @@ -70,4 +70,5 @@ public function testFilesNotFound(): void
$diffStringFromFiles = genDiff($pathToFile1, $pathToFile2);
$this->assertStringEqualsFile($pathToFile3, $diffStringFromFiles);
}
*/
}

0 comments on commit 9747f27

Please sign in to comment.