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

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

uzbek/classtools
================

Find, extract and process classes from file system

0.0.2(3y ago)026411WTFPLPHPPHP &gt;=8.1

Since Aug 19Pushed 3y ago1 watchersCompare

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

READMEChangelog (2)Dependencies (4)Versions (3)Used By (1)

uzbek/classtools
================

[](#uzbekclasstools)

[![Packagist Version](https://camo.githubusercontent.com/fcc832e995c3bca107f39c365b37a836ef8e2725a506c5d9ad6cd3698acc383c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f757a62656b2f636c617373746f6f6c732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/uzbek/classtools)[![Build Status](https://camo.githubusercontent.com/6840a0425fdde4c2e606d37cfc13c4aceb7f3b463cc655bcf005fbfd263700ff/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f757a62656b2f636c617373746f6f6c732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/uzbek/classtools)[![Quality Score](https://camo.githubusercontent.com/8462e0c3f993f51ac1ebe731e5ef53282be3b073461af9116652b481cb473888/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f757a62656b2f636c617373746f6f6c732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/uzbek/classtools)

---

Forked from
-----------------------------------------------------

[](#forked-from-httpsgithubcomhanneskodclasstools)

---

Find, extract and process classes from the file system.

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

[](#installation)

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

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

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

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

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

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

[](#using-the-transformer)

### Wrap code in namespace

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

```
$reader = new Reader("
