Skip to content

Commit 22057e0

Browse files
claudedeviantintegral
authored andcommitted
fix: add directory validation in getIds() before scandir()
Previously, scandir() was called in HarFileRepository::getIds() without checking if the directory exists, which could trigger PHP warnings. This adds a validation check using is_dir() before calling scandir(), returning an empty array if the directory doesn't exist. This complements the earlier fix in loadJson() and ensures consistent error handling. Also adds a test case to verify the behavior when getIds() is called with a non-existent directory path.
1 parent c62a75a commit 22057e0

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/Repository/HarFileRepository.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public function loadMultiple(array $ids = []): \Generator
4747

4848
public function getIds(): array
4949
{
50+
if (!is_dir($this->repositoryPath)) {
51+
return [];
52+
}
53+
5054
$hars = scandir($this->repositoryPath);
5155
if (!$hars) {
5256
return [];

tests/src/Unit/Repository/HarFileRepositoryTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,12 @@ public function testLoadJsonThrowsExceptionForInvalidFile()
8383
$this->expectException(\RuntimeException::class);
8484
$this->repository->loadJson('non-existent-file.har');
8585
}
86+
87+
public function testGetIdsReturnsEmptyArrayForNonExistentDirectory()
88+
{
89+
$repository = new HarFileRepository('/path/to/non-existent-directory');
90+
$ids = $repository->getIds();
91+
$this->assertIsArray($ids);
92+
$this->assertEmpty($ids);
93+
}
8694
}

0 commit comments

Comments
 (0)