PHPackages                             hanneskod/classtools - 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. hanneskod/classtools

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

hanneskod/classtools
====================

Find, extract and process classes from file system

1.2.1(6y ago)1319.1M↓13.4%31[2 issues](https://github.com/hanneskod/classtools/issues)[3 PRs](https://github.com/hanneskod/classtools/pulls)20WTFPLPHPPHP &gt;=7.1

Since May 15Pushed 4y ago4 watchersCompare

[ Source](https://github.com/hanneskod/classtools)[ Packagist](https://packagist.org/packages/hanneskod/classtools)[ Docs](https://github.com/hanneskod/classtools)[ RSS](/packages/hanneskod-classtools/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (14)Used By (20)

hanneskod/classtools
====================

[](#hanneskodclasstools)

[![Packagist Version](https://camo.githubusercontent.com/88b27fd2f28e3a33bce0daa7150d327b7bc69697a7bdb39bb1e9e2d5db7bf917/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68616e6e65736b6f642f636c617373746f6f6c732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hanneskod/classtools)[![Build Status](https://camo.githubusercontent.com/058ab86ee382a5e77207cd864df5d359caf6f5ed1727323a2361a1f238ed7edb/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f68616e6e65736b6f642f636c617373746f6f6c732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/hanneskod/classtools)[![Quality Score](https://camo.githubusercontent.com/36c2400a6788f48d8d77735541c3a8db61f6a59678aa6ccf1c8fec0e12906a34/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f68616e6e65736b6f642f636c617373746f6f6c732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/hanneskod/classtools)

Find, extract and process classes from the file system.

Installation
------------

[](#installation)

Install using **[composer](http://getcomposer.org/)**. Exists as **[hanneskod/classtools](https://packagist.org/packages/hanneskod/classtools)**in the **[packagist](https://packagist.org/)** repository. From the command line use:

```
composer require hanneskod/classtools

```

Using the iterator
------------------

[](#using-the-iterator)

[ClassIterator](src/Iterator/ClassIterator.php) consumes a [symfony finder](http://symfony.com/doc/current/components/finder.html) and scans files for php classes, interfaces and traits.

### Access the class map

[](#access-the-class-map)

`getClassMap()` returns a map of class names to [SplFileInfo](http://api.symfony.com/2.5/Symfony/Component/Finder/SplFileInfo.html)objects.

```
$finder = new Symfony\Component\Finder\Finder;
$iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src'));

// Print the file names of classes, interfaces and traits in 'src'
foreach ($iter->getClassMap() as $classname => $splFileInfo) {
    echo $classname.': '.$splFileInfo->getRealPath();
}
```

### Find syntax errors

[](#find-syntax-errors)

Source files containing syntax errors can not be parsed and hence no information on contained classes can be retrieved. Use `getErrors()` to read the list of encountered errors.

```
$finder = new Symfony\Component\Finder\Finder;
$iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src'));

print_r($iter->getErrors());
```

### Iterate over ReflectionClass objects

[](#iterate-over-reflectionclass-objects)

ClassIterator is also a [Traversable](http://php.net/manual/en/class.traversable.php), that on iteration yields class names as keys and [ReflectionClass](http://php.net/manual/en/class.reflectionclass.php) objects as values.

Note that to use reflection the classes found in filesystem must be included in the environment. Enable autoloading to dynamically load classes from a ClassIterator.

```
$finder = new Symfony\Component\Finder\Finder();
$iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src'));

// Enable reflection by autoloading found classes
$iter->enableAutoloading();

// Print all classes, interfaces and traits in 'src'
foreach ($iter as $class) {
    echo $class->getName();
}
```

### Filter based on class properties

[](#filter-based-on-class-properties)

[ClassIterator](src/Iterator/ClassIterator.php) is filterable and filters are chainable.

```
$finder = new Symfony\Component\Finder\Finder();
$iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src'));
$iter->enableAutoloading();

// Print all Filter types (including the interface itself)
foreach ($iter->type('hanneskod\classtools\Iterator\Filter') as $class) {
    echo $class->getName();
}

// Print definitions in the Iterator namespace whose name contains 'Class'
foreach ($iter->inNamespace('hanneskod\classtools\Iterator\Filter')->name('/type/i') as $class) {
    echo $class->getName();
}

// Print implementations of the Filter interface
foreach ($iter->type('hanneskod\classtools\Iterator\Filter')->where('isInstantiable') as $class) {
    echo $class->getName();
}
```

### Negate filters

[](#negate-filters)

Filters can also be negated by wrapping them in `not()` method calls.

```
$finder = new Symfony\Component\Finder\Finder();
$iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src'));
$iter->enableAutoloading();

// Print all classes, interfaces and traits NOT instantiable
foreach ($iter->not($iter->where('isInstantiable')) as $class) {
    echo $class->getName();
}
```

### Transforming classes

[](#transforming-classes)

Found class, interface and trait definitions can be transformed and written to a single file.

```
$finder = new Symfony\Component\Finder\Finder();
$iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src'));
$iter->enableAutoloading();

// Print all found definitions in one snippet
echo $iter->minimize();

// The same can be done using
echo $iter->transform(new hanneskod\classtools\Transformer\MinimizingWriter);
```

Using the transformer
---------------------

[](#using-the-transformer)

### Wrap code in namespace

[](#wrap-code-in-namespace)

```
$reader = new Reader("
