delolmo / class-finder
Find the instantiable PHP classes in a directory, optionally filtered by parent class or interface.
Requires
- php: ~8.4.27 || ^8.5.12
- nikic/php-parser: ^5.9
- symfony/finder: ^7.4 || ^8.0
Requires (Dev)
- captainhook/captainhook-phar: ^5.29
- captainhook/plugin-composer: ^5.3
- doctrine/coding-standard: ^14.0
- ergebnis/composer-normalize: ^2.54
- infection/infection: ^0.35
- php-parallel-lint/php-parallel-lint: ^1.4
- phpstan/phpstan: ^2.3
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.1
- phpstan/phpstan-strict-rules: ^2.1
- phpunit/phpunit: ^13.4
- psalm/plugin-phpunit: ^0.19
- ramsey/conventional-commits: ^1.7
- rector/rector: ^2.8
- roave/security-advisories: dev-latest
- shipmonk/composer-dependency-analyser: ^1.8
- vimeo/psalm: ^6.20
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-29 14:22:41 UTC
README
Find the instantiable classes declared in a directory, optionally narrowed to the subclasses or implementations of a given type.
Requirements
- PHP ~8.4.27 || ^8.5.12
Installation
composer require delolmo/class-finder
Usage
use App\Handler\HandlerInterface; use DelOlmo\ClassFinder\ClassFinder; $classFinder = new ClassFinder(); // Every instantiable class declared in src/Handler $classes = $classFinder->findAll(__DIR__ . '/src/Handler'); // Only those that extend or implement HandlerInterface $handlers = $classFinder->findByClassName(HandlerInterface::class, __DIR__ . '/src/Handler');
Both methods return a list of class names. findByClassName() is typed with a template, so static analysers know $handlers is a list<class-string<HandlerInterface>>.
Filtering the files
The constructor takes an optional Symfony\Component\Finder\Finder to use as a template. Every search runs on a fresh copy of it, so its filters apply to every call while the directories of one call never leak into the next:
use Symfony\Component\Finder\Finder; $classFinder = new ClassFinder( new Finder()->exclude('Tests')->notName('*Fixture.php'), );
A custom PhpParser\Parser can be passed as the second argument.
What is found
- Every named class a file declares, several per file included.
- Only instantiable classes: abstract classes, classes with a non-public constructor, interfaces, traits and enums are left out.
- Only autoloadable classes. Checking this runs the autoloader, which loads the file: scan only directories whose files are safe to include.
- Files that cannot be read or do not parse are skipped.