Skip to content

Commit

Permalink
Refactoring /tests, Differ.php, Formatter.php, Parser.php
Browse files Browse the repository at this point in the history
  • Loading branch information
artengin committed Sep 25, 2024
1 parent 3f285b2 commit bd738d4
Show file tree
Hide file tree
Showing 17 changed files with 347 additions and 276 deletions.
4 changes: 3 additions & 1 deletion bin/gendiff
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
$autoloadPath1 = __DIR__ . '/../../../autoload.php';
$autoloadPath2 = __DIR__ . '/../vendor/autoload.php';

use function Differ\Differ\genDiff;

if (file_exists($autoloadPath1)) {
require_once $autoloadPath1;
} else {
Expand All @@ -30,5 +32,5 @@ $firsFilePath = $args['<firstFile>'];
$secondFilePath = $args['<secondFile>'];
$format = $args['--format'];

$diff = Differ\Differ\genDiff($firsFilePath, $secondFilePath, $format);
$diff = genDiff($firsFilePath, $secondFilePath, $format);
print_r($diff);
251 changes: 127 additions & 124 deletions build/logs/clover.xml
Original file line number Diff line number Diff line change
@@ -1,70 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1726953441">
<project timestamp="1726953441">
<file name="/var/www/html/php-project-48/src/Diff.php">
<line num="17" type="stmt" count="9"/>
<line num="18" type="stmt" count="9"/>
<line num="19" type="stmt" count="9"/>
<line num="20" type="stmt" count="9"/>
<line num="21" type="stmt" count="9"/>
<line num="22" type="stmt" count="9"/>
<line num="26" type="stmt" count="9"/>
<line num="27" type="stmt" count="0"/>
<line num="30" type="stmt" count="9"/>
<line num="31" type="stmt" count="9"/>
<line num="35" type="stmt" count="9"/>
<line num="36" type="stmt" count="9"/>
<line num="37" type="stmt" count="9"/>
<line num="38" type="stmt" count="9"/>
<line num="40" type="stmt" count="9"/>
<line num="41" type="stmt" count="9"/>
<line num="42" type="stmt" count="9"/>
<line num="45" type="stmt" count="9"/>
<line num="46" type="stmt" count="9"/>
<line num="48" type="stmt" count="9"/>
<line num="49" type="stmt" count="9"/>
<line num="50" type="stmt" count="9"/>
<line num="51" type="stmt" count="9"/>
<line num="52" type="stmt" count="9"/>
<line num="55" type="stmt" count="9"/>
<line num="56" type="stmt" count="9"/>
<line num="57" type="stmt" count="9"/>
<line num="58" type="stmt" count="9"/>
<line num="59" type="stmt" count="9"/>
<line num="60" type="stmt" count="9"/>
<line num="63" type="stmt" count="9"/>
<line num="64" type="stmt" count="9"/>
<line num="65" type="stmt" count="9"/>
<line num="66" type="stmt" count="9"/>
<line num="67" type="stmt" count="9"/>
<line num="68" type="stmt" count="9"/>
<line num="71" type="stmt" count="9"/>
<line num="72" type="stmt" count="9"/>
<line num="73" type="stmt" count="9"/>
<line num="74" type="stmt" count="9"/>
<line num="75" type="stmt" count="9"/>
<line num="76" type="stmt" count="9"/>
<line num="79" type="stmt" count="9"/>
<line num="80" type="stmt" count="9"/>
<line num="81" type="stmt" count="9"/>
<line num="82" type="stmt" count="9"/>
<line num="83" type="stmt" count="9"/>
<line num="84" type="stmt" count="9"/>
<line num="85" type="stmt" count="9"/>
<metrics loc="87" ncloc="87" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="49" coveredstatements="48" elements="49" coveredelements="48"/>
<coverage generated="1727164145">
<project timestamp="1727164145">
<file name="/var/www/html/php-project-48/src/Differ.php">
<line num="17" type="stmt" count="18"/>
<line num="18" type="stmt" count="18"/>
<line num="20" type="stmt" count="18"/>
<line num="21" type="stmt" count="18"/>
<line num="23" type="stmt" count="18"/>
<line num="24" type="stmt" count="18"/>
<line num="28" type="stmt" count="18"/>
<line num="29" type="stmt" count="0"/>
<line num="32" type="stmt" count="18"/>
<line num="33" type="stmt" count="18"/>
<line num="34" type="stmt" count="18"/>
<line num="35" type="stmt" count="18"/>
<line num="39" type="stmt" count="18"/>
<line num="40" type="stmt" count="18"/>
<line num="41" type="stmt" count="18"/>
<line num="42" type="stmt" count="18"/>
<line num="44" type="stmt" count="18"/>
<line num="45" type="stmt" count="18"/>
<line num="46" type="stmt" count="18"/>
<line num="49" type="stmt" count="18"/>
<line num="50" type="stmt" count="18"/>
<line num="52" type="stmt" count="18"/>
<line num="53" type="stmt" count="18"/>
<line num="54" type="stmt" count="18"/>
<line num="55" type="stmt" count="18"/>
<line num="56" type="stmt" count="18"/>
<line num="59" type="stmt" count="18"/>
<line num="60" type="stmt" count="18"/>
<line num="61" type="stmt" count="18"/>
<line num="62" type="stmt" count="18"/>
<line num="63" type="stmt" count="18"/>
<line num="64" type="stmt" count="18"/>
<line num="67" type="stmt" count="18"/>
<line num="68" type="stmt" count="18"/>
<line num="69" type="stmt" count="18"/>
<line num="70" type="stmt" count="18"/>
<line num="71" type="stmt" count="18"/>
<line num="72" type="stmt" count="18"/>
<line num="75" type="stmt" count="18"/>
<line num="76" type="stmt" count="18"/>
<line num="77" type="stmt" count="18"/>
<line num="78" type="stmt" count="18"/>
<line num="79" type="stmt" count="18"/>
<line num="80" type="stmt" count="18"/>
<line num="83" type="stmt" count="18"/>
<line num="84" type="stmt" count="18"/>
<line num="85" type="stmt" count="18"/>
<line num="86" type="stmt" count="18"/>
<line num="87" type="stmt" count="18"/>
<line num="88" type="stmt" count="18"/>
<line num="89" type="stmt" count="18"/>
<metrics loc="91" ncloc="91" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="51" coveredstatements="50" elements="51" coveredelements="50"/>
</file>
<file name="/var/www/html/php-project-48/src/Formatter.php">
<line num="11" type="stmt" count="9"/>
<line num="12" type="stmt" count="3"/>
<line num="11" type="stmt" count="18"/>
<line num="12" type="stmt" count="12"/>
<line num="13" type="stmt" count="3"/>
<line num="14" type="stmt" count="3"/>
<line num="15" type="stmt" count="9"/>
<line num="16" type="stmt" count="9"/>
<line num="15" type="stmt" count="18"/>
<line num="16" type="stmt" count="18"/>
<metrics loc="18" ncloc="18" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="6" coveredstatements="6" elements="6" coveredelements="6"/>
</file>
<file name="/var/www/html/php-project-48/src/Formatters/Json.php">
<line num="7" type="stmt" count="3"/>
<metrics loc="9" ncloc="9" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="1" coveredstatements="1" elements="1" coveredelements="1"/>
<line num="8" type="stmt" count="3"/>
<line num="9" type="stmt" count="3"/>
<metrics loc="11" ncloc="11" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="3" coveredstatements="3" elements="3" coveredelements="3"/>
</file>
<file name="/var/www/html/php-project-48/src/Formatters/Plain.php">
<line num="21" type="stmt" count="3"/>
Expand Down Expand Up @@ -111,75 +115,74 @@
<metrics loc="83" ncloc="83" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="41" coveredstatements="41" elements="41" coveredelements="41"/>
</file>
<file name="/var/www/html/php-project-48/src/Formatters/Stylish.php">
<line num="23" type="stmt" count="3"/>
<line num="24" type="stmt" count="3"/>
<line num="25" type="stmt" count="3"/>
<line num="27" type="stmt" count="3"/>
<line num="28" type="stmt" count="3"/>
<line num="29" type="stmt" count="3"/>
<line num="31" type="stmt" count="3"/>
<line num="32" type="stmt" count="3"/>
<line num="33" type="stmt" count="3"/>
<line num="34" type="stmt" count="3"/>
<line num="35" type="stmt" count="3"/>
<line num="36" type="stmt" count="3"/>
<line num="37" type="stmt" count="3"/>
<line num="38" type="stmt" count="3"/>
<line num="39" type="stmt" count="3"/>
<line num="40" type="stmt" count="3"/>
<line num="41" type="stmt" count="3"/>
<line num="42" type="stmt" count="3"/>
<line num="43" type="stmt" count="3"/>
<line num="44" type="stmt" count="3"/>
<line num="45" type="stmt" count="3"/>
<line num="46" type="stmt" count="3"/>
<line num="47" type="stmt" count="3"/>
<line num="48" type="stmt" count="3"/>
<line num="51" type="stmt" count="3"/>
<line num="52" type="stmt" count="3"/>
<line num="54" type="stmt" count="3"/>
<line num="56" type="stmt" count="3"/>
<line num="57" type="stmt" count="3"/>
<line num="58" type="stmt" count="3"/>
<line num="59" type="stmt" count="3"/>
<line num="60" type="stmt" count="3"/>
<line num="61" type="stmt" count="3"/>
<line num="62" type="stmt" count="3"/>
<line num="63" type="stmt" count="3"/>
<line num="64" type="stmt" count="3"/>
<line num="65" type="stmt" count="3"/>
<line num="67" type="stmt" count="3"/>
<line num="68" type="stmt" count="3"/>
<line num="70" type="stmt" count="3"/>
<line num="76" type="stmt" count="3"/>
<line num="77" type="stmt" count="3"/>
<line num="79" type="stmt" count="3"/>
<line num="80" type="stmt" count="3"/>
<line num="81" type="stmt" count="3"/>
<line num="82" type="stmt" count="3"/>
<line num="83" type="stmt" count="3"/>
<line num="84" type="stmt" count="3"/>
<line num="85" type="stmt" count="3"/>
<line num="86" type="stmt" count="3"/>
<line num="87" type="stmt" count="3"/>
<line num="88" type="stmt" count="3"/>
<line num="89" type="stmt" count="3"/>
<line num="91" type="stmt" count="3"/>
<line num="92" type="stmt" count="3"/>
<line num="97" type="stmt" count="3"/>
<line num="98" type="stmt" count="3"/>
<line num="100" type="stmt" count="3"/>
<line num="23" type="stmt" count="12"/>
<line num="24" type="stmt" count="12"/>
<line num="25" type="stmt" count="12"/>
<line num="27" type="stmt" count="12"/>
<line num="28" type="stmt" count="12"/>
<line num="29" type="stmt" count="12"/>
<line num="31" type="stmt" count="12"/>
<line num="32" type="stmt" count="12"/>
<line num="33" type="stmt" count="12"/>
<line num="34" type="stmt" count="12"/>
<line num="35" type="stmt" count="12"/>
<line num="36" type="stmt" count="12"/>
<line num="37" type="stmt" count="12"/>
<line num="38" type="stmt" count="12"/>
<line num="39" type="stmt" count="12"/>
<line num="40" type="stmt" count="12"/>
<line num="41" type="stmt" count="12"/>
<line num="42" type="stmt" count="12"/>
<line num="43" type="stmt" count="12"/>
<line num="44" type="stmt" count="12"/>
<line num="45" type="stmt" count="12"/>
<line num="46" type="stmt" count="12"/>
<line num="47" type="stmt" count="12"/>
<line num="48" type="stmt" count="12"/>
<line num="51" type="stmt" count="12"/>
<line num="52" type="stmt" count="12"/>
<line num="54" type="stmt" count="12"/>
<line num="56" type="stmt" count="12"/>
<line num="57" type="stmt" count="12"/>
<line num="58" type="stmt" count="12"/>
<line num="59" type="stmt" count="12"/>
<line num="60" type="stmt" count="12"/>
<line num="61" type="stmt" count="12"/>
<line num="62" type="stmt" count="12"/>
<line num="63" type="stmt" count="12"/>
<line num="64" type="stmt" count="12"/>
<line num="65" type="stmt" count="12"/>
<line num="67" type="stmt" count="12"/>
<line num="68" type="stmt" count="12"/>
<line num="70" type="stmt" count="12"/>
<line num="76" type="stmt" count="12"/>
<line num="77" type="stmt" count="12"/>
<line num="79" type="stmt" count="12"/>
<line num="80" type="stmt" count="12"/>
<line num="81" type="stmt" count="12"/>
<line num="82" type="stmt" count="12"/>
<line num="83" type="stmt" count="12"/>
<line num="84" type="stmt" count="12"/>
<line num="85" type="stmt" count="12"/>
<line num="86" type="stmt" count="12"/>
<line num="87" type="stmt" count="12"/>
<line num="88" type="stmt" count="12"/>
<line num="89" type="stmt" count="12"/>
<line num="91" type="stmt" count="12"/>
<line num="92" type="stmt" count="12"/>
<line num="97" type="stmt" count="12"/>
<line num="98" type="stmt" count="12"/>
<line num="100" type="stmt" count="12"/>
<metrics loc="102" ncloc="102" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="58" coveredstatements="58" elements="58" coveredelements="58"/>
</file>
<file name="/var/www/html/php-project-48/src/Parser.php">
<line num="9" type="stmt" count="9"/>
<line num="10" type="stmt" count="9"/>
<line num="11" type="stmt" count="6"/>
<line num="12" type="stmt" count="6"/>
<line num="13" type="stmt" count="9"/>
<line num="14" type="stmt" count="9"/>
<metrics loc="16" ncloc="16" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="6" coveredstatements="6" elements="6" coveredelements="6"/>
<line num="9" type="stmt" count="18"/>
<line num="10" type="stmt" count="12"/>
<line num="11" type="stmt" count="12"/>
<line num="12" type="stmt" count="18"/>
<line num="13" type="stmt" count="18"/>
<metrics loc="15" ncloc="15" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="5" coveredstatements="5" elements="5" coveredelements="5"/>
</file>
<metrics files="6" loc="315" ncloc="315" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="161" coveredstatements="160" elements="161" coveredelements="160"/>
<metrics files="6" loc="320" ncloc="320" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="164" coveredstatements="163" elements="164" coveredelements="163"/>
</project>
</coverage>
38 changes: 21 additions & 17 deletions src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Differ\Differ;

use function Functional\sort;
use function Differ\Formatter\formatting;
use function Differ\Formatter\format;
use function Differ\Parser\parser;

const UNCHANGED = 'unchanged';
Expand All @@ -14,21 +14,25 @@

function genDiff(string $file1, string $file2, string $format = 'stylish'): string
{
$contentFile1 = getContents($file1);
$contentFile2 = getContents($file2);
$valueFile1 = parser($file1, $contentFile1);
$valueFile2 = parser($file2, $contentFile2);
['extension' => $extension1, 'content' => $contentFile1] = getContents($file1);
['extension' => $extension2, 'content' => $contentFile2] = getContents($file2);

$valueFile1 = parser($extension1, $contentFile1);
$valueFile2 = parser($extension2, $contentFile2);

$valueDiff = buildDiff($valueFile1, $valueFile2);
return formatting($valueDiff, $format);
return format($valueDiff, $format);
}
function getContents(string $path): string
function getContents(string $path): array
{
if (!file_exists($path)) {
throw new \Exception("Invalid file path: {$path}");
}

$content = file_get_contents($path);
return $content;
return [
'extension' => pathinfo($path, PATHINFO_EXTENSION),
'content' => file_get_contents($path),
];
}
function buildDiff(array $first, array $second): array
{
Expand All @@ -46,41 +50,41 @@ function buildDiff(array $first, array $second): array
(is_array($valueSecond) && !array_is_list($valueSecond))
) {
return [
'compare' => NESTED,
'key' => $key,
'value' => buildDiff($valueFirst, $valueSecond),
'type' => NESTED,
'children' => buildDiff($valueFirst, $valueSecond),
];
}

if (!array_key_exists($key, $first)) {
return [
'compare' => ADDED,
'key' => $key,
'type' => ADDED,
'value' => $valueSecond,
];
}

if (!array_key_exists($key, $second)) {
return [
'compare' => DELETED,
'key' => $key,
'type' => DELETED,
'value' => $valueFirst,
];
}

if ($valueFirst === $valueSecond) {
return [
'compare' => UNCHANGED,
'key' => $key,
'type' => UNCHANGED,
'value' => $valueFirst,
];
}

return [
'compare' => CHANGED,
'key' => $key,
'valueFirst' => $valueFirst,
'valueSecond' => $valueSecond,
'type' => CHANGED,
'value1' => $valueFirst,
'value2' => $valueSecond,
];
}, $sortedArray);
}
Loading

0 comments on commit bd738d4

Please sign in to comment.