Skip to content

Commit c112ae0

Browse files
authored
refactor: Move the calculation of the longest common path to a dedicated method (#1017)
1 parent 53a5f3f commit c112ae0

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/Console/ConsoleScoper.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Webmozart\Assert\Assert;
3030
use function array_keys;
3131
use function array_map;
32+
use function array_unique;
3233
use function array_values;
3334
use function count;
3435
use function dirname;
@@ -242,17 +243,7 @@ private static function getFiles(Configuration $config, string $outputDir): arra
242243
$filesWithContent = $config->getFilesWithContents();
243244
$excludedFilesWithContents = $config->getExcludedFilesWithContents();
244245

245-
$commonDirectoryPath = Path::getLongestCommonBasePath(
246-
...array_map(
247-
Path::getDirectory(...),
248-
array_keys($filesWithContent),
249-
),
250-
...array_map(
251-
Path::getDirectory(...),
252-
array_keys($excludedFilesWithContents),
253-
),
254-
);
255-
Assert::notNull($commonDirectoryPath);
246+
$commonDirectoryPath = self::getCommonDirectoryPath($config);
256247

257248
$mapFiles = static fn (array $inputFileTuple) => new File(
258249
Path::normalize($inputFileTuple[0]),
@@ -276,6 +267,28 @@ private static function getFiles(Configuration $config, string $outputDir): arra
276267
];
277268
}
278269

270+
private static function getCommonDirectoryPath(Configuration $config): string
271+
{
272+
$filesWithContent = $config->getFilesWithContents();
273+
$excludedFilesWithContents = $config->getExcludedFilesWithContents();
274+
275+
$directoryPaths = [
276+
...array_map(
277+
Path::getDirectory(...),
278+
array_keys($filesWithContent),
279+
),
280+
...array_map(
281+
Path::getDirectory(...),
282+
array_keys($excludedFilesWithContents),
283+
),
284+
];
285+
286+
$commonPath = Path::getLongestCommonBasePath(...array_unique($directoryPaths));
287+
Assert::notNull($commonPath, 'Expected to find a common path.');
288+
289+
return $commonPath;
290+
}
291+
279292
private static function findVendorDir(array $outputFilePaths): ?string
280293
{
281294
$vendorDirsAsKeys = [];

0 commit comments

Comments
 (0)