Skip to content

Commit

Permalink
modified files, new logic in modules, aded formatter "plain"
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmuS2024 committed Oct 24, 2024
1 parent 9be29df commit 511582d
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 114 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
## Hexlet tests and linter status:
# Hexlet tests and linter status:
[![Actions Status](https://github.com/RasmuS2024/php-project-48/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/RasmuS2024/php-project-48/actions)
## Codeclimate maintainability status:
# Codeclimate maintainability status:
<a href="https://codeclimate.com/github/RasmuS2024/php-project-48/maintainability"><img src="https://api.codeclimate.com/v1/badges/dd978260caa754e3367b/maintainability" /></a>
## Codeclimate Test Coverage status:
# Codeclimate Test Coverage status:
<a href="https://codeclimate.com/github/RasmuS2024/php-project-48/test_coverage"><img src="https://api.codeclimate.com/v1/badges/dd978260caa754e3367b/test_coverage" /></a>


## Gendiff process with json files in asciinema:
# Gendiff process with json files in asciinema:
<a href="https://asciinema.org/a/sajdPQG2NqP2Ky51Kyp4SDpBp" target="_blank"><img src="https://asciinema.org/a/sajdPQG2NqP2Ky51Kyp4SDpBp.svg" width="400" height="300" /></a>

## Gendiff process with YAML files in asciinema:
# Gendiff process with YAML files in asciinema:
<a href="https://asciinema.org/a/rV1BXXmxURj3LKG9UDLQcDJvi" target="_blank"><img src="https://asciinema.org/a/rV1BXXmxURj3LKG9UDLQcDJvi.svg" width="400" height="300" /></a>

## Gendiff process with json and YAML files with nested structure in asciinema:
<a href="https://asciinema.org/a/nUzyU01TNMRxl5NX8fDdVE54V" target="_blank"><img src="https://asciinema.org/a/nUzyU01TNMRxl5NX8fDdVE54V.svg" width="400" height="300" /></a>
# Gendiff process with json and YAML files with nested structure in asciinema:
<a href="https://asciinema.org/a/nUzyU01TNMRxl5NX8fDdVE54V" target="_blank"><img src="https://asciinema.org/a/nUzyU01TNMRxl5NX8fDdVE54V.svg" width="400" height="300" /></a>

# Gendiff process with select out format in asciinema:
<a href="https://asciinema.org/a/82H9oUqEatG1FHpbsW9dhUKGH" target="_blank"><img src="https://asciinema.org/a/82H9oUqEatG1FHpbsW9dhUKGH.svg" width="400" height="300" /></a>
2 changes: 1 addition & 1 deletion bin/gendiff
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if (file_exists($autoloadPath1)) {
require_once $autoloadPath2;
}

use function GenDiff\Parsers\genDiff;
use function GenDiff\Differ\genDiff;

$doc = <<<DOC
Generate diff
Expand Down
94 changes: 64 additions & 30 deletions build/logs/clover.xml
Original file line number Diff line number Diff line change
@@ -1,35 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1728822010">
<project timestamp="1728822010">
<file name="/home/user1/projects/php-project-48/src/Diff.php">
<line num="7" type="stmt" count="1"/>
<line num="8" type="stmt" count="1"/>
<line num="9" type="stmt" count="1"/>
<line num="10" type="stmt" count="1"/>
<line num="11" type="stmt" count="1"/>
<coverage generated="1729746185">
<project timestamp="1729746185">
<file name="/home/user1/projects/php-project-48/src/Differ.php">
<line num="10" type="stmt" count="5"/>
<line num="11" type="stmt" count="5"/>
<line num="12" type="stmt" count="5"/>
<line num="13" type="stmt" count="5"/>
<line num="14" type="stmt" count="5"/>
<line num="19" type="stmt" count="5"/>
<line num="20" type="stmt" count="5"/>
<line num="21" type="stmt" count="5"/>
<line num="22" type="stmt" count="5"/>
<line num="23" type="stmt" count="5"/>
<line num="24" type="stmt" count="5"/>
<line num="29" type="stmt" count="3"/>
<line num="30" type="stmt" count="3"/>
<line num="31" type="stmt" count="3"/>
<line num="33" type="stmt" count="3"/>
<line num="35" type="stmt" count="3"/>
<line num="36" type="stmt" count="3"/>
<line num="41" type="stmt" count="5"/>
<line num="42" type="stmt" count="5"/>
<line num="43" type="stmt" count="5"/>
<line num="44" type="stmt" count="5"/>
<line num="45" type="stmt" count="3"/>
<line num="47" type="stmt" count="5"/>
<line num="49" type="stmt" count="5"/>
<line num="50" type="stmt" count="5"/>
<line num="51" type="stmt" count="5"/>
<line num="52" type="stmt" count="3"/>
<line num="54" type="stmt" count="5"/>
<line num="56" type="stmt" count="5"/>
<line num="58" type="stmt" count="5"/>
<line num="59" type="stmt" count="5"/>
<line num="60" type="stmt" count="5"/>
<line num="61" type="stmt" count="3"/>
<line num="63" type="stmt" count="5"/>
<line num="64" type="stmt" count="5"/>
<line num="66" type="stmt" count="5"/>
<line num="67" type="stmt" count="3"/>
<line num="69" type="stmt" count="5"/>
<line num="70" type="stmt" count="0"/>
<line num="72" type="stmt" count="5"/>
<line num="74" type="stmt" count="5"/>
<line num="75" type="stmt" count="5"/>
<metrics loc="77" ncloc="77" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="42" coveredstatements="41" elements="42" coveredelements="41"/>
</file>
<file name="/home/user1/projects/php-project-48/src/Formatters.php">
<line num="10" type="stmt" count="5"/>
<line num="11" type="stmt" count="4"/>
<line num="12" type="stmt" count="1"/>
<line num="13" type="stmt" count="1"/>
<line num="14" type="stmt" count="1"/>
<line num="15" type="stmt" count="1"/>
<line num="16" type="stmt" count="1"/>
<line num="18" type="stmt" count="1"/>
<line num="19" type="stmt" count="1"/>
<line num="20" type="stmt" count="1"/>
<line num="21" type="stmt" count="0"/>
<line num="23" type="stmt" count="1"/>
<line num="24" type="stmt" count="1"/>
<line num="26" type="stmt" count="1"/>
<line num="27" type="stmt" count="1"/>
<line num="30" type="stmt" count="1"/>
<line num="33" type="stmt" count="1"/>
<line num="34" type="stmt" count="1"/>
<line num="35" type="stmt" count="1"/>
<line num="36" type="stmt" count="1"/>
<line num="39" type="stmt" count="1"/>
<line num="42" type="stmt" count="1"/>
<line num="43" type="stmt" count="1"/>
<metrics loc="45" ncloc="45" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="26" coveredstatements="25" elements="26" coveredelements="25"/>
<line num="13" type="stmt" count="5"/>
<line num="14" type="stmt" count="5"/>
<metrics loc="16" ncloc="16" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="5" coveredstatements="5" elements="5" coveredelements="5"/>
</file>
<file name="/home/user1/projects/php-project-48/src/Parsers.php">
<line num="9" type="stmt" count="5"/>
<line num="10" type="stmt" count="5"/>
<line num="11" type="stmt" count="3"/>
<line num="12" type="stmt" count="2"/>
<line num="13" type="stmt" count="2"/>
<line num="14" type="stmt" count="5"/>
<line num="15" type="stmt" count="5"/>
<metrics loc="17" ncloc="17" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="7" coveredstatements="7" elements="7" coveredelements="7"/>
</file>
<metrics files="1" loc="45" ncloc="45" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="26" coveredstatements="25" elements="26" coveredelements="25"/>
<metrics files="3" loc="110" ncloc="110" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="54" coveredstatements="53" elements="54" coveredelements="53"/>
</project>
</coverage>
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"Hexlet\\Code\\": "src/"
},
"files": [
"src/Differ.php",
"src/Parsers.php",
"src/Formatters.php",
"formatters/Stylish.php",
Expand Down
76 changes: 76 additions & 0 deletions src/Differ.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace GenDiff\Differ;

use function GenDiff\Formatters\formatSelect;
use function GenDiff\Parsers\fileParser;

function genDiff(mixed $file1Path, mixed $file2Path, string $formatName = 'stylish'): string
{
$data1 = fileParser($file1Path);
$data2 = fileParser($file2Path);
$dataDiff = filesDiffer($data1, $data2);
$result = formatSelect($dataDiff, $formatName);
return $result;
}

function getSortedKeys(array $json1, array $json2): array
{
$keys1 = array_keys($json1);
$keys2 = array_keys($json2);
$keys = array_merge($keys1, $keys2);
$keys = array_unique($keys);
sort($keys, SORT_STRING);
return $keys;
}

function arrayKeysInsert(array $array): array
{
$value1 = array_map(function ($keyIn, $valueIn) {
if (!is_array($valueIn)) {
return ['type' => ' ', 'key' => $keyIn, 'value' => $valueIn];
} else {
return ['type' => ' ', 'key' => $keyIn, 'value' => arrayKeysInsert($valueIn)];
}
}, array_keys($array), $array);
return $value1;
}

function filesDiffer(mixed $data1, mixed $data2): mixed
{
$sortedKeys = getSortedKeys($data1, $data2);
$data = array_map(function ($key) use ($data1, $data2) {
if (!array_key_exists($key, $data1)) {
if (is_array($data2[$key])) {
$value2 = arrayKeysInsert($data2[$key]);
} else {
$value2 = $data2[$key];
}
return ['type' => '+', 'key' => $key, 'value' => $value2];
} elseif (!array_key_exists($key, $data2)) {
if (is_array($data1[$key])) {
$value1 = arrayKeysInsert($data1[$key]);
} else {
$value1 = $data1[$key];
}
return ['type' => '-', 'key' => $key, 'value' => $value1];
}
$value1 = $data1[$key];
$value2 = $data2[$key];
if (is_array($value1) && is_array($value2)) {
return ['type' => ' ', 'key' => $key, 'value' => filesDiffer($value1, $value2)];
}
if ($value1 === $value2) {
return ['type' => ' ', 'key' => $key, 'value' => $value1];
} else {
if (is_array($value1)) {
$value1 = arrayKeysInsert($value1);
}
if (is_array($value2)) {
$value2 = arrayKeysInsert($value2);
}
return ['type' => '_', 'key' => $key, 'value' => $value1, 'new_value' => $value2];
}
}, $sortedKeys);
return $data;
}
4 changes: 2 additions & 2 deletions src/Formatters.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use function GenDiff\Formatters\Stylish\stylish;
use function GenDiff\Formatters\Plain\plain;

function formatSelect(array $diffSource, string $style): string
function formatSelect(array $diffSource, string $formatName): string
{
$result = match ($style) {
$result = match ($formatName) {
'stylish' => stylish($diffSource),
'plain' => plain($diffSource),
};
Expand Down
74 changes: 1 addition & 73 deletions src/Parsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,7 @@

use Symfony\Component\Yaml\Yaml;

use function GenDiff\Formatters\formatSelect;

function getSortedKeys(array $json1, array $json2): array
{
$keys1 = array_keys($json1);
$keys2 = array_keys($json2);
$keys = array_merge($keys1, $keys2);
$keys = array_unique($keys);
sort($keys, SORT_STRING);
return $keys;
}

function fileReader(string $pathToFile): array
function fileParser(string $pathToFile): array
{
$extension = pathinfo($pathToFile)['extension'];
return match ($extension) {
Expand All @@ -26,63 +14,3 @@ function fileReader(string $pathToFile): array
default => '',
};
}

function genDiff(mixed $file1Path, mixed $file2Path, string $style = 'stylish'): mixed
{
$data1 = fileReader($file1Path);
$data2 = fileReader($file2Path);
$dataDiff = filesDiffer($data1, $data2);
$result = formatSelect($dataDiff, $style);
return $result;
}

function arrayKeysInsert(array $array): array
{
$value1 = array_map(function ($keyIn, $valueIn) {
if (!is_array($valueIn)) {
return ['type' => ' ', 'key' => $keyIn, 'value' => $valueIn];
} else {
return ['type' => ' ', 'key' => $keyIn, 'value' => arrayKeysInsert($valueIn)];
}
}, array_keys($array), $array);
return $value1;
}

function filesDiffer(mixed $data1, mixed $data2): mixed
{
$sortedKeys = getSortedKeys($data1, $data2);
$data = array_map(function ($key) use ($data1, $data2) {
if (!array_key_exists($key, $data1)) {
if (is_array($data2[$key])) {
$value2 = arrayKeysInsert($data2[$key]);
} else {
$value2 = $data2[$key];
}
return ['type' => '+', 'key' => $key, 'value' => $value2];
} elseif (!array_key_exists($key, $data2)) {
if (is_array($data1[$key])) {
$value1 = arrayKeysInsert($data1[$key]);
} else {
$value1 = $data1[$key];
}
return ['type' => '-', 'key' => $key, 'value' => $value1];
}
$value1 = $data1[$key];
$value2 = $data2[$key];
if (is_array($value1) && is_array($value2)) {
return ['type' => ' ', 'key' => $key, 'value' => filesDiffer($value1, $value2)];
}
if ($value1 === $value2) {
return ['type' => ' ', 'key' => $key, 'value' => $value1];
} else {
if (is_array($value1)) {
$value1 = arrayKeysInsert($value1);
}
if (is_array($value2)) {
$value2 = arrayKeysInsert($value2);
}
return ['type' => '_', 'key' => $key, 'value' => $value1, 'new_value' => $value2];
}
}, $sortedKeys);
return $data;
}
2 changes: 1 addition & 1 deletion tests/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PHPUnit\Framework\TestCase;

use function GenDiff\Parsers\genDiff;
use function GenDiff\Differ\genDiff;

class DiffTest extends TestCase
{
Expand Down

0 comments on commit 511582d

Please sign in to comment.