<?php
/**
* Collects file path metrics.
*
* @param string $directory The directory to scan.
* @return array An array of file path metrics.
*/
function collectFilePaths(string $directory): array
{
$filePaths = [];
$files = scandir($directory);
if ($files === false) {
return ["error" => "Could not scan directory: " . $directory]; // Handle directory scan failure
}
foreach ($files as $file) {
if ($file != "." && $file != "..") {
$filePath = $directory . "/" . $file; // Construct the full file path
if (is_file($filePath)) {
$filePaths[] = $filePath; // Add the file path to the array
}
}
}
return ["count" => count($filePaths), "paths" => $filePaths];
}
// Example usage:
$directory = "./"; // Current directory
$metrics = collectFilePaths($directory);
if (isset($metrics['error'])) {
echo "Error: " . $metrics['error'] . PHP_EOL;
} else {
echo "File path metrics: " . PHP_EOL;
echo "Count: " . $metrics['count'] . PHP_EOL;
echo "Paths: " . implode(PHP_EOL, $metrics['paths']) . PHP_EOL;
}
?>
Add your comment