1. <?php
  2. /**
  3. * Collects file path metrics.
  4. *
  5. * @param string $directory The directory to scan.
  6. * @return array An array of file path metrics.
  7. */
  8. function collectFilePaths(string $directory): array
  9. {
  10. $filePaths = [];
  11. $files = scandir($directory);
  12. if ($files === false) {
  13. return ["error" => "Could not scan directory: " . $directory]; // Handle directory scan failure
  14. }
  15. foreach ($files as $file) {
  16. if ($file != "." && $file != "..") {
  17. $filePath = $directory . "/" . $file; // Construct the full file path
  18. if (is_file($filePath)) {
  19. $filePaths[] = $filePath; // Add the file path to the array
  20. }
  21. }
  22. }
  23. return ["count" => count($filePaths), "paths" => $filePaths];
  24. }
  25. // Example usage:
  26. $directory = "./"; // Current directory
  27. $metrics = collectFilePaths($directory);
  28. if (isset($metrics['error'])) {
  29. echo "Error: " . $metrics['error'] . PHP_EOL;
  30. } else {
  31. echo "File path metrics: " . PHP_EOL;
  32. echo "Count: " . $metrics['count'] . PHP_EOL;
  33. echo "Paths: " . implode(PHP_EOL, $metrics['paths']) . PHP_EOL;
  34. }
  35. ?>

Add your comment