php 53 lines · 7 steps

Recursively finding files with SPL iterators in PHP

A FileFinder walks a directory tree and returns paths matching an extension and minimum size.

Explained by highlit
1<?php
2 
3namespace App\Support;
4 
5use FilesystemIterator;
6use RecursiveDirectoryIterator;
7use RecursiveIteratorIterator;
8use SplFileInfo;
9 
10final class FileFinder
11{
12 public function __construct(
13 private readonly string $extension = 'php',
14 private readonly int $minBytes = 0,
15 ) {
16 }
17 
18 public function find(string $rootPath): array
19 {
20 if (!is_dir($rootPath)) {
21 throw new \InvalidArgumentException("Not a directory: {$rootPath}");
22 }
23 
24 $directory = new RecursiveDirectoryIterator(
25 $rootPath,
26 FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS,
27 );
28 
29 $iterator = new RecursiveIteratorIterator(
30 $directory,
31 RecursiveIteratorIterator::LEAVES_ONLY,
32 );
33 
34 $matches = [];
35 
36 foreach ($iterator as $file) {
37 /** @var SplFileInfo $file */
38 if (!$file->isFile() || $file->getExtension() !== $this->extension) {
39 continue;
40 }
41 
42 if ($file->getSize() < $this->minBytes) {
43 continue;
44 }
45 
46 $matches[] = $file->getRealPath();
47 }
48 
49 sort($matches);
50 
51 return $matches;
52 }
53}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1PHP's recursive iterators let you flatten a whole directory tree into a single foreach loop.
  2. 2Readonly constructor-promoted properties turn a class into a tidy, immutable configuration object.
  3. 3Filtering with early continue statements keeps a scanning loop flat and easy to read.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Recursively finding files with SPL iterators in PHP — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code