Skip to content

Commit e28d109

Browse files
Change logic of Parser: logic of the Translator moved to the Parser, Translator has been deleted. Output to the screen is delegated to the gendiff (binary file). Refactoring name of function.
1 parent 9bbc0bb commit e28d109

File tree

5 files changed

+36
-41
lines changed

5 files changed

+36
-41
lines changed

bin/gendiff

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if (file_exists($autoloadPathLocal)) {
1111
require_once $autoloadPathGlobal;
1212
}
1313

14-
use function Differ\Parser\parse;
14+
use function Differ\Differ\genDiff;
1515

1616
$doc = <<<DOC
1717
Generate diff
@@ -29,4 +29,4 @@ DOC;
2929

3030
$args = Docopt::handle($doc, array('version' => 'Gendiff 1.0'));
3131

32-
parse($args['<firstFile>'], $args['<secondFile>'], $args['--format']);
32+
echo genDiff($args['<firstFile>'], $args['<secondFile>'], $args['--format']);

composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"files": [
2727
"src/Parser.php",
2828
"src/Differ/Differ.php",
29-
"src/Differ/Translator.php",
3029
"src/Formatters.php",
3130
"src/Formatters/Stylish.php",
3231
"src/Formatters/Plain.php",

src/Differ/Differ.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
namespace Differ\Differ;
44

5-
use function Differ\Formatters\selectFormatter;
65
use function Functional\sort;
7-
use function Differ\Translator\getJson;
6+
use function Differ\Parser\parse;
7+
use function Differ\Parser\parseToJson;
88

99
function genDiff(string $pathToFile1, string $pathToFile2, string $format = 'stylish'): string
1010
{
11-
$firstFile = getJson($pathToFile1);
12-
$secondFile = getJson($pathToFile2);
11+
$firstFile = parseToJson($pathToFile1);
12+
$secondFile = parseToJson($pathToFile2);
1313

1414
$elements = sortingFirstFile($firstFile, $secondFile);
1515
$elements2 = sortingSecondFile($secondFile, $firstFile);
1616

1717
$resultDiff = sortArrayByKeysRecursive(array_merge_recursive($elements, $elements2));
18-
return selectFormatter($resultDiff, $format);
18+
return parse($resultDiff, $format);
1919
}
2020

2121
function sortingFirstFile(mixed $tree1, mixed $tree2): mixed

src/Differ/Translator.php

-29
This file was deleted.

src/Parser.php

+29-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,35 @@
22

33
namespace Differ\Parser;
44

5-
use function Differ\Differ\genDiff;
5+
use Symfony\Component\Yaml\Yaml;
66

7-
function parse(string $pathToFile1, string $pathToFile2, string $format): void
7+
use function Differ\Formatters\selectFormatter;
8+
9+
function parse(array $resultDiff, string $format): string
10+
{
11+
return selectFormatter($resultDiff, $format);
12+
}
13+
14+
function parseToJson(string $path): array
815
{
9-
$resultString = genDiff($pathToFile1, $pathToFile2, $format);
10-
echo $resultString;
16+
$content = getFileContent($path);
17+
18+
if (str_ends_with($path, '.yaml') || str_ends_with($path, '.yml')) {
19+
return Yaml::parse($content);
20+
}
21+
22+
return json_decode($content, true);
23+
}
24+
25+
function getFileContent(string $path): string
26+
{
27+
$dirPath = __DIR__;
28+
29+
if (str_starts_with($path, '/')) {
30+
$fullPath = $path;
31+
} else {
32+
$fullPath = "{$dirPath}/../../{$path}";
33+
}
34+
35+
return file_get_contents($fullPath);
1136
}

0 commit comments

Comments
 (0)