Skip to content

Commit

Permalink
moved FilesProcessing to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-xz committed Feb 19, 2024
1 parent e2582f5 commit defb220
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 78 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "calculate differencies",
"autoload": {
"files": [
"src/FilesProcessing.php",
"src/Parsers.php",
"src/Differ.php",
"src/Formatters.php",
Expand Down
18 changes: 8 additions & 10 deletions src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Differ\Differ;

use Differ\FilesProcessing;

use function Differ\Parsers\parseFile;
use function Differ\Parsers\makePathAbsolute;
use function Differ\Formatters\chooseFormateAndPrint;
use function Functional\sort;

Expand Down Expand Up @@ -44,11 +45,6 @@ function getNode(mixed $array)
];
}

function getKeyAndValue(array $difference)
{
return ['key' => key($difference),'value' => current($difference)];
}

function compare(array $dataOne, array $dataTwo)
{
$mergedKeys = array_merge(array_keys($dataOne), array_keys($dataTwo));
Expand All @@ -71,10 +67,12 @@ function compare(array $dataOne, array $dataTwo)

function genDiff(string $pathToFile1, string $pathToFile2, string $format = 'stylish')
{
$firstAbsolutePath = makePathAbsolute($pathToFile1);
$secondAbsolutePath = makePathAbsolute($pathToFile2);
$firstFile = parseFile($firstAbsolutePath);
$secondFile = parseFile($secondAbsolutePath);
$firstAbsolutePath = FilesProcessing\makePathAbsolute($pathToFile1);
$secondAbsolutePath = FilesProcessing\makePathAbsolute($pathToFile2);
$firstContent = FilesProcessing\getFilesContent($firstAbsolutePath);
$secondContent = FilesProcessing\getFilesContent($secondAbsolutePath);
$firstFile = parseFile(pathinfo($firstAbsolutePath, PATHINFO_EXTENSION), $firstContent);
$secondFile = parseFile(pathinfo($secondAbsolutePath, PATHINFO_EXTENSION), $secondContent);
$differencies = compare($firstFile, $secondFile);
return chooseFormateAndPrint($format, $differencies);
}
69 changes: 34 additions & 35 deletions src/Formatters/Stylish.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Differ\Formatters\Stylish;

use function Differ\Differ\getNode;
use function Differ\Differ\getKeyAndValue;

function stringify(mixed $item)
{
Expand All @@ -15,40 +14,40 @@ function stringify(mixed $item)
return $result;
}

function makeStylish2(mixed $comparedData, int $depth = 0, string $separator = ' ', int $offset = 2)
{
if (!is_array($comparedData)) {
return stringify($comparedData);
}
$emptySpace = str_repeat($separator, $depth);
$result = array_map(function ($key, $data) use ($separator, $depth, $offset) {
$nextDepth = $depth + 1;
$emptySpace = substr(str_repeat($separator, $nextDepth), $offset, null);
['status' => $status, 'symbol' => $symbol, 'difference' => $difference] = getNode($data);
if ($status === 'unsorted') {
$keyOfValue = $key;
$value = $data;
} else {
$keyOfValue = key($difference);
$value = current($difference);
}
if ($status === 'old and new') {
$oldAndNewValues = array_map(function ($node) use ($nextDepth, $emptySpace) {
['symbol' => $symbol, 'difference' => $difference] = getNode($node);
$key = key($difference);
$value = current($difference);
$stringValue = makeStylish($value, $nextDepth);
return "{$emptySpace}{$symbol}{$key}: {$stringValue}";
}, $value);
return implode("\n", $oldAndNewValues);
} else {
$convertedValue = makeStylish($value, $nextDepth);
}
return "{$emptySpace}{$symbol}{$keyOfValue}: {$convertedValue}";
}, array_keys($comparedData), $comparedData);
$final = implode("\n", $result);
return "{\n{$final}\n{$emptySpace}}";
}
// function makeStylish2(mixed $comparedData, int $depth = 0, string $separator = ' ', int $offset = 2)
// {
// if (!is_array($comparedData)) {
// return stringify($comparedData);
// }
// $emptySpace = str_repeat($separator, $depth);
// $result = array_map(function ($key, $data) use ($separator, $depth, $offset) {
// $nextDepth = $depth + 1;
// $emptySpace = substr(str_repeat($separator, $nextDepth), $offset, null);
// ['status' => $status, 'symbol' => $symbol, 'difference' => $difference] = getNode($data);
// if ($status === 'unsorted') {
// $keyOfValue = $key;
// $value = $data;
// } else {
// $keyOfValue = key($difference);
// $value = current($difference);
// }
// if ($status === 'old and new') {
// $oldAndNewValues = array_map(function ($node) use ($nextDepth, $emptySpace) {
// ['symbol' => $symbol, 'difference' => $difference] = getNode($node);
// $key = key($difference);
// $value = current($difference);
// $stringValue = makeStylish($value, $nextDepth);
// return "{$emptySpace}{$symbol}{$key}: {$stringValue}";
// }, $value);
// return implode("\n", $oldAndNewValues);
// } else {
// $convertedValue = makeStylish($value, $nextDepth);
// }
// return "{$emptySpace}{$symbol}{$keyOfValue}: {$convertedValue}";
// }, array_keys($comparedData), $comparedData);
// $final = implode("\n", $result);
// return "{\n{$final}\n{$emptySpace}}";
// }

function makeStylish(mixed $comparedData, int $depth = 0, string $separator = ' ', int $offset = 2)
{
Expand Down
32 changes: 4 additions & 28 deletions src/Parsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,16 @@

use Symfony\Component\Yaml\Yaml;

function makePathAbsolute(string $pathToFile)
function parseFile(string $extention, string $content)
{
$realPath = realpath($pathToFile);
if ($realPath === false) {
$absolutePath = __DIR__ . $pathToFile;
} else {
$absolutePath = $realPath;
}
return $absolutePath;
}

function getFilesContent(string $absolutePath)
{
if (!file_exists($absolutePath)) {
throw new \Exception("File do not found: \"{$absolutePath}\"!");
} elseif (filesize($absolutePath) == 0) {
$pathBaseName = pathinfo($absolutePath, PATHINFO_BASENAME);
throw new \Exception("File \"{$pathBaseName}\" is empty.");
}
return file_get_contents($absolutePath, true);
}

function parseFile(string $pathToFile)
{
$fileExtention = pathinfo($pathToFile, PATHINFO_EXTENSION);
$content = getFilesContent($pathToFile);
if ($fileExtention === 'json') {
if ($extention === 'json') {
if ($content === false) {
throw new \Exception('Error when turning value into string');
}
return json_decode($content, true);
} elseif ($fileExtention === 'yaml' || $fileExtention === 'yml') {
} elseif ($extention === 'yaml' || $extention === 'yml') {
return Yaml::parse($content);
} else {
throw new \Exception("Unknow file extention: \"{$fileExtention}\"!");
throw new \Exception("Unknow file extention: \"{$extention}\"!");
}
}
9 changes: 4 additions & 5 deletions tests/ParsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@
namespace Differ\Tests;

use PHPUnit\Framework\TestCase;
use Differ\FilesProcessing;

use function Differ\Parsers\parseFile;
use function Differ\Parsers\getFilesContent;

class ParsersTest extends TestCase
{
public function testDoNotExistException(): void
{
$this->expectExceptionMessage("File do not found: \"/Notexist.txt\"!");
getFilesContent('/Notexist.txt');
FilesProcessing\getFilesContent('/Notexist.txt');
}
public function testBadExtention(): void
{
$txtFile = __DIR__ . "/fixtures/Example.txt";
$this->expectExceptionMessage("Unknow file extention: \"txt\"!");
parseFile($txtFile);
parseFile('txt', '');
}
public function testEmptyDile(): void
{
$empty = __DIR__ . "/fixtures/Empty.json";
$this->expectExceptionMessage("File \"Empty.json\" is empty.");
getFilesContent($empty);
FilesProcessing\getFilesContent($empty);
}
}

0 comments on commit defb220

Please sign in to comment.