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

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

megachriz/classtools
====================

Find, extract and process classes from file system

1.3.0(5mo ago)0354↑88.9%1WTFPLPHPPHP ^8.1|^8.2|^8.3

Since May 15Pushed 5mo agoCompare

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

READMEChangelogDependencies (5)Versions (15)Used By (1)

megachriz/classtools
====================

[](#megachrizclasstools)

[![Packagist Version](https://camo.githubusercontent.com/774bbb86cb3593e29d8e1fc1ee092c5af66e80f685659ff4ea9e93dab6db68e1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d656761636872697a2f636c617373746f6f6c732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/megachriz/classtools)[![Build Status](https://camo.githubusercontent.com/22a27714a6904114dcc72646c91849a68ed55db8e272621658c939a311bf62be/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6d656761636872697a2f636c617373746f6f6c732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/megachriz/classtools)[![Quality Score](https://camo.githubusercontent.com/2a566658997239cec8006fe21289ed534e29cc47e86e3351530e42c325eeb286/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6d656761636872697a2f636c617373746f6f6c732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/megachriz/classtools)

Find, extract and process classes from the file system.

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

[](#installation)

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

```
composer require megachriz/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 megachriz\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 megachriz\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 megachriz\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 megachriz\classtools\Iterator\ClassIterator($finder->in('src'));
$iter->enableAutoloading();

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

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

// Print implementations of the Filter interface
foreach ($iter->type('megachriz\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 megachriz\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 megachriz\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 megachriz\classtools\Transformer\MinimizingWriter);
```

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

[](#using-the-transformer)

### Wrap code in namespace

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

```
$reader = new Reader("
