phpdocumentor/reflection
PHPDoc reflection library used by phpDocumentor to parse docblocks and reflect types, namespaces, and symbols from PHP code. Helps tools extract documentation and metadata without executing code, powering static analysis and doc generation workflows.
Start by installing the package via Composer: composer require phpdocumentor/reflection. Unlike phpdocumentor/reflection-docblock, this is a newer, modern library (v4+) designed for static analysis of PHP codebases—ideal for tools like linters, IDEs, or documentation generators. The core entry points are File, Folder, and ProjectBuilder. To begin analyzing a single file:
use phpDocumentor\Reflection\ProjectBuilder;
use phpDocumentor\Reflection\Fixture\File;
$projectBuilder = new ProjectBuilder();
$project = $projectBuilder->buildFromDirectory(__DIR__ . '/src');
foreach ($project->getFiles() as $file) {
echo "Analyzed file: " . $file->getFilename() . "\n";
}
Read the README and the API docs for detailed usage.
ProjectBuilder to build a complete model of a PHP codebase, enabling cross-file analysis (e.g., finding all usages of a class across a project).$file->getStatements() for custom static analysis rules (e.g., detecting code smells).FileListCollector and serialized project caches to speed up repeated analysis in long-running tools.ProjectBuilder::buildFromDirectory() with a filter callback to limit scope, or analyze files incrementally.getNamespaces(): If your project has no declared namespace, getNamespaces() returns empty. Always check $file->getNamespaceName() for null or empty strings.File->getDocBlock() cautiously—test with a variety of comment styles.ProjectBuilder or implement custom NodeVisitors to inject domain-specific analysis logic (e.g., enforce internal naming conventions).ErrorHandler::disable() to prevent silent parse failures during development—actual errors get thrown as ParseError exceptions.How can I help you explore Laravel packages today?