Skip to content

Commit

Permalink
Refactor and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Dec 15, 2024
1 parent 5e5e50b commit d3eaa77
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions packages/framework/src/Framework/Actions/Internal/FileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
class FileFinder
{
/**
* @param string|array<string>|false $matchExtensions
* @param array<string>|string|false $matchExtensions
* @return \Illuminate\Support\Collection<int, string>
*/
public static function handle(string $directory, string|array|false $matchExtensions = false, bool $recursive = false): Collection
public static function handle(string $directory, array|string|false $matchExtensions = false, bool $recursive = false): Collection
{
if (! Filesystem::isDirectory($directory)) {
return collect();
Expand All @@ -43,14 +43,24 @@ public static function handle(string $directory, string|array|false $matchExtens
/** @param array<string> $extensions */
protected static function buildFileExtensionPattern(array $extensions): string
{
// Normalize array by splitting any CSV strings within
$extensions = array_merge(...array_map(function (string $item): array {
$extensions = self::expandCommaSeparatedValues($extensions);

return '/\.('.self::normalizeExtensionForRegexPattern($extensions).')$/i';
}

/** @param array<string> $extensions */
private static function expandCommaSeparatedValues(array $extensions): array
{
return array_merge(...array_map(function (string $item): array {
return array_map(fn (string $item): string => trim($item), explode(',', $item));
}, $extensions));
}

// Remove leading dots, escape extensions, and build the regex pattern
return '/\.(' . implode('|', array_map(function (string $extension): string {
return preg_quote(ltrim($extension, '.'), '/');
}, $extensions)) . ')$/i';
/** @param array<string> $extensions */
private static function normalizeExtensionForRegexPattern(array $extensions): string
{
return implode('|', array_map(function (string $extension): string {
return preg_quote(ltrim($extension, '.'), '/');
}, $extensions));
}
}

0 comments on commit d3eaa77

Please sign in to comment.