Skip to content

Commit

Permalink
Normalize input by removing a leading dot if present
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Dec 14, 2024
1 parent cf7eb8e commit a4d4701
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/framework/src/Foundation/Kernel/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,11 @@ public function findFiles(string $directory, string|false $matchExtension = fals

// Optionally match file extensions (case-insensitively)
if ($matchExtension !== false) {
$finder->name('/\.' . preg_quote($matchExtension, '/') . '$/i');
// Normalize input by removing a leading dot if present
$normalizedExtension = ltrim($matchExtension, '.');

// Use a case-insensitive regex to match file extensions
$finder->name('/\.' . preg_quote($normalizedExtension, '/') . '$/i');
}

// Collect relative paths
Expand Down
7 changes: 7 additions & 0 deletions packages/framework/tests/Unit/FilesystemFindFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ public function testFindFilesWithCaseInsensitiveExtensionFilter()
$this->assertSameArray(['file.MD', 'another_file.md'], 'directory', 'MD');
}

public function testFindFilesWithLeadingDotInFileExtension()
{
$this->files(['directory/file.md', 'directory/another_file.md', 'directory/ignored.txt']);
$this->assertSameArray(['file.md', 'another_file.md'], 'directory', 'md');
$this->assertSameArray(['file.md', 'another_file.md'], 'directory', '.md');
}

public function testFindFilesHandlesLargeNumberOfFiles()
{
$this->files(array_map(fn ($i) => "directory/file$i.md", range(1, 100)));
Expand Down

0 comments on commit a4d4701

Please sign in to comment.