Skip to content

Commit 7e35eed

Browse files
authored
refactor: Make the file sorting more consistent (#1541)
refactor: Simplify the buffered files sorting
1 parent bb40665 commit 7e35eed

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/Box.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
use RecursiveDirectoryIterator;
3636
use RuntimeException;
3737
use Seld\PharUtils\Timestamps;
38-
use SplFileInfo;
3938
use Symfony\Component\Finder\Finder;
39+
use Symfony\Component\Finder\SplFileInfo;
4040
use Webmozart\Assert\Assert;
4141
use function array_map;
4242
use function array_unshift;
@@ -46,11 +46,15 @@
4646
use function file_exists;
4747
use function getcwd;
4848
use function is_object;
49+
use function iter\fromPairs;
50+
use function iter\map;
4951
use function openssl_pkey_export;
5052
use function openssl_pkey_get_details;
5153
use function openssl_pkey_get_private;
5254
use function sprintf;
55+
use function strcmp;
5356
use function strval;
57+
use function uksort;
5458

5559
/**
5660
* Box is a utility class to generate a PHAR.
@@ -156,19 +160,22 @@ public function endBuffering(?callable $dumpAutoload): void
156160
$this->scoper->getExcludedFilePaths(),
157161
);
158162

159-
$unknownFiles = Finder::create()
160-
->files()
161-
->in($tmp)
162-
->notPath(array_keys($files));
163+
$unknownFiles = fromPairs(
164+
map(
165+
static fn (SplFileInfo $fileInfo) => [
166+
$fileInfo->getRelativePathname(),
167+
$fileInfo->getPathname(),
168+
],
169+
Finder::create()
170+
->files()
171+
->in($tmp)
172+
->notPath(array_keys($files)),
173+
),
174+
);
163175

164176
$files = [...$files, ...$unknownFiles];
165177

166-
uasort($files, static function (SplFileInfo|string $a, SplFileInfo|string $b) {
167-
$a = is_string($a) ? $a : $a->getPath();
168-
$b = is_string($b) ? $b : $b->getPath();
169-
170-
return strcmp($a, $b);
171-
});
178+
uksort($files, strcmp(...));
172179

173180
$this->phar->buildFromIterator(new ArrayIterator($files), $tmp);
174181
} finally {

0 commit comments

Comments
 (0)