Skip to content

Commit d5b51d0

Browse files
committed
pref: simple sortFileTreeRecursive function
1 parent 3099d9e commit d5b51d0

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

src/TorrentFile.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ public function getFileList()
700700
return $this->parse()['files'];
701701
}
702702

703-
private static function sortFileTreeRecursive(array &$fileTree, $sortByString = false, $sortByFolder = false): array
703+
private static function sortFileTreeRecursive(array &$fileTree, $sortByString = false, $sortByFolder = false)
704704
{
705705
if ($sortByString) {
706706
ksort($fileTree, SORT_NATURAL | SORT_FLAG_CASE);
@@ -709,14 +709,15 @@ private static function sortFileTreeRecursive(array &$fileTree, $sortByString =
709709
$isoFile = [];
710710
foreach ($fileTree as $key => &$item) {
711711
if (is_array($item)) {
712-
$fileTree[$key] = self::sortFileTreeRecursive($item, $sortByString, $sortByFolder);
712+
self::sortFileTreeRecursive($item, $sortByString, $sortByFolder);
713713
} elseif ($sortByFolder) {
714714
$isoFile[$key] = $item;
715715
unset($fileTree[$key]);
716716
}
717717
}
718-
$fileTree = array_merge($fileTree, $isoFile);
719-
return $fileTree;
718+
if ($sortByFolder && !empty($isoFile)) {
719+
$fileTree = array_merge($fileTree, $isoFile);
720+
}
720721
}
721722

722723
/**
@@ -726,7 +727,7 @@ private static function sortFileTreeRecursive(array &$fileTree, $sortByString =
726727
* "directory" => [
727728
* "filename2" => 2345
728729
* ],
729-
* "filename1" => 123
730+
* "filename1" => 123 // 123 is file size
730731
* ]
731732
* ]
732733
*/
@@ -736,7 +737,6 @@ public function getFileTree($sortType = self::FILETREE_SORT_NORMAL)
736737

737738
$sortByString = ($sortType & self::FILETREE_SORT_STRING) === self::FILETREE_SORT_STRING;
738739
$sortByFolder = ($sortType & self::FILETREE_SORT_FOLDER) === self::FILETREE_SORT_FOLDER;
739-
740740
if ($sortByString || $sortByFolder) {
741741
self::sortFileTreeRecursive($fileTree, $sortByString, $sortByFolder);
742742
}

tests/TorrentFileTreeSortTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class TorrentFileTreeSortTest extends TestCase
99

1010
protected function setUp(): void
1111
{
12-
$this->torrent = TorrentFile::load("tests/asserts/test_tree_sort.torrent");
12+
$this->torrent = TorrentFile::load("tests/asserts/test-tree-sort.torrent");
1313
$this->torrent->parse();
1414
}
1515

@@ -28,7 +28,8 @@ public function testGetFileTreeByParse()
2828
]
2929
]
3030
]),
31-
json_encode($this->torrent->getFileTree()));
31+
json_encode($this->torrent->getFileTree())
32+
);
3233
}
3334

3435
public function testGetFileTreeByString()
@@ -46,7 +47,8 @@ public function testGetFileTreeByString()
4647
],
4748
]
4849
]),
49-
json_encode($this->torrent->getFileTree(TorrentFile::FILETREE_SORT_STRING)));
50+
json_encode($this->torrent->getFileTree(TorrentFile::FILETREE_SORT_STRING))
51+
);
5052
}
5153

5254
public function testGetFileTreeByFolder()
@@ -64,7 +66,8 @@ public function testGetFileTreeByFolder()
6466
'file.txt' => 1048576
6567
]
6668
]),
67-
json_encode($this->torrent->getFileTree(TorrentFile::FILETREE_SORT_FOLDER)));
69+
json_encode($this->torrent->getFileTree(TorrentFile::FILETREE_SORT_FOLDER))
70+
);
6871
}
6972

7073
public function testGetFileTreeByNatural()
@@ -82,6 +85,7 @@ public function testGetFileTreeByNatural()
8285
'file.txt' => 1048576
8386
]
8487
]),
85-
json_encode($this->torrent->getFileTree(TorrentFile::FILETREE_SORT_NATURAL)));
88+
json_encode($this->torrent->getFileTree(TorrentFile::FILETREE_SORT_NATURAL))
89+
);
8690
}
8791
}

0 commit comments

Comments
 (0)