Skip to content

Commit 299033f

Browse files
authored
fix: handle null results in get_dir_file_info() (#10407)
1 parent f3829fe commit 299033f

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

system/Helpers/filesystem_helper.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,11 @@ function get_dir_file_info(string $sourceDir, bool $topLevelOnly = true, bool $r
281281
if (is_dir($sourceDir . $file) && $file[0] !== '.' && $topLevelOnly === false) {
282282
get_dir_file_info($sourceDir . $file . DIRECTORY_SEPARATOR, $topLevelOnly, true);
283283
} elseif ($file[0] !== '.') {
284-
$fileData[$file] = get_file_info($sourceDir . $file);
285-
$fileData[$file]['relative_path'] = $relativePath;
284+
$info = get_file_info($sourceDir . $file);
285+
if ($info !== null) {
286+
$fileData[$file] = $info;
287+
$fileData[$file]['relative_path'] = $relativePath;
288+
}
286289
}
287290
}
288291

tests/system/Helpers/FilesystemHelperTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,22 @@ public function testGetDirFileInfoFailure(): void
477477
$this->assertSame($expected, get_dir_file_info(SUPPORTPATH . 'Files#baker'));
478478
}
479479

480+
public function testGetDirFileInfoIgnoresDirectoriesWhenTopLevelOnlyIsTrue(): void
481+
{
482+
$dir = WRITEPATH . 'test_get_dir_file_info';
483+
mkdir($dir . '/subdir', 0777, true);
484+
file_put_contents($dir . '/file.txt', 'test');
485+
486+
$result = get_dir_file_info($dir, true);
487+
488+
unlink($dir . '/file.txt');
489+
rmdir($dir . '/subdir');
490+
rmdir($dir);
491+
492+
$this->assertArrayHasKey('file.txt', $result);
493+
$this->assertArrayNotHasKey('subdir', $result);
494+
}
495+
480496
public function testGetFileInfo(): void
481497
{
482498
$file = SUPPORTPATH . 'Files/baker/banana.php';

user_guide_src/source/changelogs/v4.7.5.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Bugs Fixed
3232

3333
- **CLIRequest:** Fixed a bug where ``parseCommand()`` could throw a TypeError when ``argv`` is missing.
3434
- **Content Security Policy:** Fixed a bug where empty ``Content-Security-Policy``, ``Content-Security-Policy-Report-Only``, and ``Reporting-Endpoints`` response headers were generated when no corresponding values existed.
35+
- **Helpers:** Fixed a bug where ``get_dir_file_info()`` returned incomplete entries for subdirectories and missing files instead of omitting them.
3536
- **Logger:** Fixed a bug where interpolating a log message with array or non-stringable context values could raise PHP warnings or errors.
3637

3738
See the repo's

0 commit comments

Comments
 (0)