PHPackages                             hackpack/hack-class-scanner - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. hackpack/hack-class-scanner

Abandoned → [fredemmott/definition-finder](/?search=fredemmott%2Fdefinition-finder)Library[Utility &amp; Helpers](/categories/utility)

hackpack/hack-class-scanner
===========================

Recursively scan a directory for hack classes and/or interfaces

2.0.1(10y ago)1146[1 issues](https://github.com/HackPack/HackClassScanner/issues)2MITHack

Since Feb 25Pushed 10y ago2 watchersCompare

[ Source](https://github.com/HackPack/HackClassScanner)[ Packagist](https://packagist.org/packages/hackpack/hack-class-scanner)[ Docs](https://github.com/HackPack/HackClassScanner)[ RSS](/packages/hackpack-hack-class-scanner/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (4)Used By (2)

HackClassScanner
================

[](#hackclassscanner)

[![Build Status](https://camo.githubusercontent.com/0992b7f64c5f0bc2bcc63f1a4f120fa6ba50cf1be3ed63bd198e1da5a69a1489/68747470733a2f2f7472617669732d63692e6f72672f4861636b5061636b2f4861636b436c6173735363616e6e65722e737667)](https://travis-ci.org/HackPack/HackClassScanner)[![HHVM Status](https://camo.githubusercontent.com/f1da09b5f56b23ee001ac20c333a128e28883844c553ae673bb34efdf006be6a/687474703a2f2f6868766d2e683463632e64652f62616467652f6861636b7061636b2f6861636b2d636c6173732d7363616e6e65722e737667)](http://hhvm.h4cc.de/package/hackpack/hack-class-scanner)

A class that recursively scans a directory for [Hack](http://hacklang.org/) definitions of:

- Classes
- Interfaces
- Traits
- Enums
- Type definitions
- NewType definitions
- Functions
- Constants

Installation
============

[](#installation)

Install [Composer](https://getcomposer.org/download/) then use the following in your project directory:

```
composer require hackpack/hack-class-scanner
```

Use
===

[](#use)

To use the class, simply instantiate it with a set of files/base directories to scan and a set of files/directories to ignore.

```
use HackPack\Scanner\ClassScanner;
use HackPack\Scanner\NameType;

$scanner = new ClassScanner(
  Set{‘directory/to/scan’, ‘other/directory’, ‘file/to/scan.txt’},
  Set{‘other/directory/to/ignore’, ‘other/directory/file_to_ignore.txt’}
);

$classes = $scanner->getNameToFileMap(NameType::className);
$interfaces = $scanner->getNameToFileMap(NameType::interfaceName);
$traits = $scanner->getNameToFileMap(NameType::traitName);
$enums = $scanner->getNameToFileMap(NameType::enumName);
$types = $scanner->getNameToFileMap(NameType::typeName);
$newtypes = $scanner->getNameToFileMap(NameType::newtypeName);
$functions = $scanner->getNameToFileMap(NameType::functionName);
$constants = $scanner->getNameToFileMap(NameType::constantName);
```

The `getNameToFileMap` method takes one parameter specifying the type of the definition desired. `getNameToFileMap` will return a `Map` where the keys are the names of the classes, interfaces, traits, etc... including the full name space and the values are the full path to the file in which the definition resides.

Please note that ALL files will be scanned by default (not just .php and/or .hh files). If you wish to only scan files with a particular extension, see the file name filter section below.

Filters
-------

[](#filters)

You can filter the results based on the name of the definition and/or the name of the file. Each filter must be a closure with a signature of `function(string) : bool`.

### File Name Filters

[](#file-name-filters)

To register a file name filter, call `ClassScanner->addFileNameFilter()` with the filter callback (see example below).

The input for a file name filter is the full path to the file (via `realpath`). If all registered filter functions return `true` for a particular file name, the file will be scanned. If at least one registered file filter returns `false`, the file will not be read (via `file_get_contents`).

### Definition Name Filters

[](#definition-name-filters)

To register a name filter, call `ClassScanner->addNameFilter()` with the name type and the filter callback (see example below).

The input for a name filter is the name of the class, interface, trait, etc... including the full namespace. If at least one registered filter function returns `false` for a particular name, that name is guaranteed to not appear in the results. Note that the file in which a filtered definition appears may still be in the list if other non-filtered definitions are also in said file. If you would like to guarantee a file to be skipped, define a file name filter.

### Example Filters

[](#example-filters)

In this example, a simple regular expression is used to filter both file names and class names.

```
use HackPack\Scanner\ClassScanner;
use HackPack\Scanner\NameType;

$includes = Set{...};
$excludes = Set{...};
$scanner = new ClassScanner($includes, $excludes);

// Define the filter callbacks
$classFilter = $className ==> preg_match(‘/pattern/’, $className);
$fileFilter = $fileName ==> preg_match(‘/pattern/’, $fileName);

// Attach the filters
$scanner->addDefinitionNameFilter(NameType::className, $classFilter);
$scanner->addFileNameFilter($fileFilter);

// Retreive the class definitions
$classMap = $scanner->mapDefinitionToFile(NameType::className);
```

In this example, we are specifically looking for XHP classes, using the assumption that all XHP classes are defined in `.xhp` files.

```
use HackPack\Scanner\ClassScanner;
use HackPack\Scanner\NameType;

$includes = Set{...};
$excludes = Set{...};
$scanner = new ClassScanner($includes, $excludes);

// Define the filters
$classFilter = $className ==> substr($className, 0, 4) === 'xhp_';
$fileFilter = $fileName ==> substr($fileName, -4) === '.xhp';

// Attach the filters
$scanner->addNameFilter(NameType::className, $classFilter);
$scanner->addFileNameFilter($fileFilter);

// Retreive the class definitions
$xhpClasses = $scanner->mapClassToFile();
```

Thanks
======

[](#thanks)

The file parsing algorithm and the majority of the tests were authored by [Fred Emmott](https://github.com/fredemmott) in [fredemmott/definitions-finder](https://github.com/fredemmott/definitions-finder)

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~80 days

Total

3

Last Release

3941d ago

Major Versions

1.0.0 → 2.0.02015-08-03

### Community

Maintainers

![](https://www.gravatar.com/avatar/78bcca38df593ddb1cf9c1ff715a433b3a2f991e95b73362e44b397e9ee01b80?d=identicon)[kilahm](/maintainers/kilahm)

---

Top Contributors

[![kilahm](https://avatars.githubusercontent.com/u/3050967?v=4)](https://github.com/kilahm "kilahm (33 commits)")

---

Tags

autoloadhhvmhackscanClassScanner

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hackpack-hack-class-scanner/health.svg)

```
[![Health](https://phpackages.com/badges/hackpack-hack-class-scanner/health.svg)](https://phpackages.com/packages/hackpack-hack-class-scanner)
```

###  Alternatives

[composer/composer

Composer helps you declare, manage and install dependencies of PHP projects. It ensures you have the right stack everywhere.

29.4k187.2M2.6k](/packages/composer-composer)[nette/robot-loader

🍀 Nette RobotLoader: high performance and comfortable autoloader that will search and autoload classes within your application.

89352.7M321](/packages/nette-robot-loader)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
