Skip to content

Commit a6df788

Browse files
authored
refactor: Make Box::endBuffering a bit more readable (#1539)
1 parent 13765fb commit a6df788

File tree

1 file changed

+68
-27
lines changed

1 file changed

+68
-27
lines changed

src/Box.php

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
use Seld\PharUtils\Timestamps;
3939
use Symfony\Component\Finder\Finder;
4040
use Symfony\Component\Finder\SplFileInfo;
41+
use Traversable;
4142
use Webmozart\Assert\Assert;
43+
use function array_keys;
4244
use function array_map;
4345
use function array_unshift;
4446
use function chdir;
@@ -144,39 +146,18 @@ public function endBuffering(?callable $dumpAutoload): void
144146
}
145147

146148
try {
147-
$files = [];
148-
$bufferedFiles = $this->bufferedFiles;
149-
$this->bufferedFiles = [];
150-
151-
foreach ($bufferedFiles as $file) {
152-
$files[$file->getPath()] = $tmp.DIRECTORY_SEPARATOR.$file->getPath();
153-
154-
FS::dumpFile(
155-
$file->getPath(),
156-
$file->getContents(),
157-
);
158-
}
149+
$bufferedFiles = $this->dumpBufferedFiles($tmp);
159150

160151
$completeDumpAutoload();
161152

162-
$unknownFiles = fromPairs(
163-
map(
164-
static fn (SplFileInfo $fileInfo) => [
165-
$fileInfo->getRelativePathname(),
166-
$fileInfo->getPathname(),
167-
],
168-
Finder::create()
169-
->files()
170-
->in($tmp)
171-
->notPath(array_keys($files)),
172-
),
153+
$remainingFiles = self::collectRemainingFiles(
154+
$tmp,
155+
array_keys($bufferedFiles),
173156
);
174157

175-
$files = [...$files, ...$unknownFiles];
176-
177-
uksort($files, strcmp(...));
158+
$iterator = self::createFileSortedIterator($bufferedFiles, $remainingFiles);
178159

179-
$this->phar->buildFromIterator(new ArrayIterator($files), $tmp);
160+
$this->phar->buildFromIterator($iterator, $tmp);
180161
} finally {
181162
FS::remove($tmp);
182163
// Must happen _after_ the remove as the latter has higher priority.
@@ -204,6 +185,66 @@ private function createDumpAutoload(?callable $dumpAutoload): Closure
204185
);
205186
}
206187

188+
/**
189+
* @return array<string, string>
190+
*/
191+
private function dumpBufferedFiles(string $tmp): array
192+
{
193+
$files = [];
194+
$bufferedFiles = $this->bufferedFiles;
195+
$this->bufferedFiles = [];
196+
197+
foreach ($bufferedFiles as $file) {
198+
$files[$file->getPath()] = $tmp.DIRECTORY_SEPARATOR.$file->getPath();
199+
200+
FS::dumpFile(
201+
$file->getPath(),
202+
$file->getContents(),
203+
);
204+
}
205+
206+
return $files;
207+
}
208+
209+
/**
210+
* @param list<string> $bufferedFileNames
211+
*
212+
* @return iterable<string, string>
213+
*/
214+
private static function collectRemainingFiles(
215+
string $tmp,
216+
array $bufferedFileNames,
217+
): iterable {
218+
return fromPairs(
219+
map(
220+
static fn (SplFileInfo $fileInfo) => [
221+
$fileInfo->getRelativePathname(),
222+
$fileInfo->getPathname(),
223+
],
224+
Finder::create()
225+
->files()
226+
->in($tmp)
227+
->notPath($bufferedFileNames),
228+
),
229+
);
230+
}
231+
232+
/**
233+
* @param array<string, string> $bufferedFiles
234+
* @param iterable<string, string> $remainingFiles
235+
*
236+
* @return Traversable<string, string>
237+
*/
238+
private static function createFileSortedIterator(
239+
array $bufferedFiles,
240+
iterable $remainingFiles
241+
): Traversable {
242+
$files = [...$bufferedFiles, ...$remainingFiles];
243+
uksort($files, strcmp(...));
244+
245+
return new ArrayIterator($files);
246+
}
247+
207248
/**
208249
* @param non-empty-string $normalizedVendorDir Normalized path ("/" path separator and no trailing "/") to the Composer vendor directory
209250
*/

0 commit comments

Comments
 (0)