Skip to content

Commit 92f5093

Browse files
committed
added getDifference method
1 parent 5bb91bc commit 92f5093

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

src/FilesDiffCommand.php

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,53 @@ class FilesDiffCommand implements CommandInterface
1414

1515
private function constructContent($accum, $item): string
1616
{
17-
return $accum .= "\n " . $this->normalizeData($item);
17+
return $accum .= "\n " . $item;
1818
}
1919

20-
private function normalizeData($data): string
20+
private function normalizeData($data)
2121
{
22+
$normalizedValue = $data;
2223
if (is_bool($data)) {
2324
if ($data) {
24-
return "true";
25+
$normalizedValue = "true";
2526
} else {
26-
return "false";
27+
$normalizedValue = "false";
2728
}
28-
} else {
29-
return $data;
29+
} elseif (is_null($data)) {
30+
$normalizedValue = "null";
31+
} elseif (is_array($data)) {
32+
$normalizedValue = array_map(
33+
fn ($item) => $this->normalizeData($item),
34+
$data
35+
);
3036
}
37+
38+
return $normalizedValue;
39+
}
40+
41+
private function getDifference($contentListKeys, $file1Content, $file2Content): array
42+
{
43+
return array_map(
44+
function ($fileKey) use ($file1Content, $file2Content) {
45+
if (array_key_exists($fileKey, $file2Content)) {
46+
if (
47+
array_key_exists($fileKey, $file1Content) &&
48+
!strcmp($file1Content[$fileKey], $file2Content[$fileKey])
49+
) {
50+
$result = " " . $fileKey . ": " . $file1Content[$fileKey] . "\n";
51+
} elseif (array_key_exists($fileKey, $file1Content)) {
52+
$result = " - " . $fileKey . ": " . $file1Content[$fileKey] . "\n" .
53+
" + " . $fileKey . ": " . $file2Content[$fileKey] . "\n";
54+
} else {
55+
$result = " + " . $fileKey . ": " . $file2Content[$fileKey] . "\n";
56+
}
57+
return $result;
58+
} else {
59+
return " - " . $fileKey . ": " . $file1Content[$fileKey] . "\n";
60+
}
61+
},
62+
$contentListKeys
63+
);
3164
}
3265

3366
public function __construct()
@@ -54,20 +87,29 @@ public function execute(CommandInterface $command = null): CommandInterface
5487
$this->filesDataItems[] = $this->fileReader->readFile($filePath);
5588
}
5689

57-
$file1Content = $this->filesDataItems[0];
58-
$file2Content = $this->filesDataItems[1];
90+
$file1Content = array_map(
91+
fn ($item) => $this->normalizeData($item),
92+
$this->filesDataItems[0],
93+
);
94+
95+
$file2Content = array_map(
96+
fn ($item) => $this->normalizeData($item),
97+
$this->filesDataItems[1]
98+
);
5999

60100
$filename1Path = explode("/", $this->filesPaths[0]);
61101
$filename2Path = explode("/", $this->filesPaths[1]);
62102

63-
$this->filesContent[] = end($filename1Path) . " content:\n";
103+
$file1Name = end($filename1Path);
104+
$this->filesContent[] = $file1Name . " content:\n";
64105
$this->filesContent[] = array_reduce(
65106
$file1Content,
66107
[$this, 'constructContent'],
67108
"{"
68109
) . "\n}\n";
69110

70-
$this->filesContent[] = end($filename2Path) . " content:\n";
111+
$file2Name = end($filename2Path);
112+
$this->filesContent[] = $file2Name . " content:\n";
71113
$this->filesContent[] = array_reduce(
72114
$file2Content,
73115
[$this, 'constructContent'],
@@ -79,26 +121,10 @@ public function execute(CommandInterface $command = null): CommandInterface
79121
$file1Keys = array_keys($file1Content);
80122
$file2Keys = array_keys($file2Content);
81123
$mergedFileKeys = array_unique(array_merge($file1Keys, $file2Keys));
82-
$this->filesDiffs = array_map(
83-
function ($fileKey) use ($file1Content, $file2Content) {
84-
if (array_key_exists($fileKey, $file2Content)) {
85-
if (
86-
array_key_exists($fileKey, $file1Content) &&
87-
!strcmp($file1Content[$fileKey], $file2Content[$fileKey])
88-
) {
89-
$result = " " . $fileKey . ": " . $this->normalizeData($file1Content[$fileKey]) . "\n";
90-
} elseif (array_key_exists($fileKey, $file1Content)) {
91-
$result = " - " . $fileKey . ": " . $this->normalizeData($file1Content[$fileKey]) . "\n" .
92-
" + " . $fileKey . ": " . $this->normalizeData($file2Content[$fileKey]) . "\n";
93-
} else {
94-
$result = " + " . $fileKey . ": " . $this->normalizeData($file2Content[$fileKey]) . "\n";
95-
}
96-
return $result;
97-
} else {
98-
return " - " . $fileKey . ": " . $this->normalizeData($file1Content[$fileKey]) . "\n";
99-
}
100-
},
101-
$mergedFileKeys
124+
$this->filesDiffs = $this->getDifference(
125+
$mergedFileKeys,
126+
$file1Content,
127+
$file2Content
102128
);
103129
$this->filesDiffsString = "{\n" . implode("", $this->filesDiffs) . "}\n";
104130
}

0 commit comments

Comments
 (0)