Skip to content

Commit a6c3bcc

Browse files
committed
reduced methods count in files diff command
1 parent cf65188 commit a6c3bcc

File tree

1 file changed

+19
-45
lines changed

1 file changed

+19
-45
lines changed

src/Differs/FilesDiffCommand.php

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -67,45 +67,21 @@ private function normalizeData(mixed $data): mixed
6767
return $normalizedValue;
6868
}
6969

70-
private function getNextLevel(mixed $currentLevel): int
71-
{
72-
return is_integer($currentLevel) ? $currentLevel + 1 : 0;
73-
}
74-
75-
private function getNextLevelHistory(
76-
string $currentHistory,
77-
string $fileKey
78-
): string {
79-
return ($currentHistory === "") ?
80-
$fileKey : $currentHistory . "." . $fileKey;
81-
}
82-
83-
/**
84-
* @return array<int,string>
85-
*/
86-
private function getInitContentKeys(
87-
mixed $file1Content,
88-
mixed $file2Content
89-
): array {
90-
$contentKeys = array_keys(array_merge(
91-
is_array($file1Content) ? $file1Content : [],
92-
is_array($file2Content) ? $file2Content : []
93-
));
94-
asort($contentKeys);
95-
96-
return $contentKeys;
97-
}
98-
9970
/**
10071
* @return array<int,string>
10172
*/
10273
private function getNextContentKeys(
10374
mixed $file1Item,
10475
mixed $file2Item,
105-
string $fileKey
76+
string $fileKey = ""
10677
): array {
107-
$file1Content = $this->getNextItemContent($file1Item, $fileKey);
108-
$file2Content = $this->getNextItemContent($file2Item, $fileKey);
78+
if ($fileKey !== "") {
79+
$file1Content = $this->getNextItemContent($file1Item, $fileKey);
80+
$file2Content = $this->getNextItemContent($file2Item, $fileKey);
81+
} else {
82+
$file1Content = $file1Item;
83+
$file2Content = $file2Item;
84+
}
10985

11086
$contentKeys = array_keys(array_merge(
11187
is_array($file1Content) ? $file1Content : [],
@@ -181,11 +157,10 @@ function ($contentDescriptor, $fileKey) {
181157
$currentHistory = is_array($contentDescriptor) ? $contentDescriptor["history"] : "";
182158
$currentLevel = is_array($contentDescriptor) ? $contentDescriptor["level"] : 0;
183159

184-
$nextLevel = $this->getNextLevel($currentLevel);
185-
$nextHistory = $this->getNextLevelHistory(
186-
is_string($currentHistory) ? $currentHistory : "",
187-
$fileKey
188-
);
160+
$nextLevel = is_integer($currentLevel) ? $currentLevel + 1 : 0;
161+
$nextHistory = (($currentHistory !== "") && (is_string($currentHistory))) ?
162+
$currentHistory . "." . $fileKey : $fileKey;
163+
189164
$fileContent = $this->getNextItemContent($fileItem, $fileKey);
190165

191166
$nextInitContentDescriptor = $this->getNextInitContentDescriptor(
@@ -341,11 +316,10 @@ function ($differenceDescriptor, $fileKey) {
341316
$fileKey,
342317
is_string($currentStatus) ? $currentStatus : ""
343318
);
344-
$nextLevel = $this->getNextLevel($currentLevel);
345-
$nextHistory = $this->getNextLevelHistory(
346-
is_string($currentHistory) ? $currentHistory : "",
347-
$fileKey
348-
);
319+
$nextLevel = is_integer($currentLevel) ? $currentLevel + 1 : 0;
320+
$nextHistory = (($currentHistory !== "") && (is_string($currentHistory))) ?
321+
$currentHistory . "." . $fileKey : $fileKey;
322+
349323
$file1Content = $this->getNextItemContent($file1Item, $fileKey);
350324
$file2Content = $this->getNextItemContent($file2Item, $fileKey);
351325

@@ -427,7 +401,7 @@ public function execute(CLPI $command, FPI $fileParser): FDCI
427401
history: "",
428402
fileContent: $file1Content
429403
);
430-
$fileKeys = $this->getInitContentKeys($file1Content, $file1Content);
404+
$fileKeys = $this->getNextContentKeys($file1Content, $file1Content);
431405

432406
$this->content1Descriptor = $this->getContent(
433407
$fileKeys,
@@ -440,7 +414,7 @@ public function execute(CLPI $command, FPI $fileParser): FDCI
440414
history: "",
441415
fileContent: $file2Content
442416
);
443-
$fileKeys = $this->getInitContentKeys($file2Content, $file2Content);
417+
$fileKeys = $this->getNextContentKeys($file2Content, $file2Content);
444418

445419
$this->content2Descriptor = $this->getContent(
446420
$fileKeys,
@@ -455,7 +429,7 @@ public function execute(CLPI $command, FPI $fileParser): FDCI
455429
file1Content: $file1Content,
456430
file2Content: $file2Content
457431
);
458-
$mergedFileKeys = $this->getInitContentKeys($file1Content, $file2Content);
432+
$mergedFileKeys = $this->getNextContentKeys($file1Content, $file2Content);
459433

460434
$this->differenceDescriptor = $this->getDifference(
461435
$mergedFileKeys,

0 commit comments

Comments
 (0)