Skip to content

dbublik/unused-class-hunter

Repository files navigation

Unused class hunter

Detects unused classes in your PHP codebase.

Installation

composer require --dev dbublik/unused-class-hunter

Usage

After installation, you can run the following command to start hunting:

./vendor/bin/unused-class-hunter check

And that’s it! The Hunter will scan your entire codebase and find all unused classes.

Customization

Config:

Most likely, after the first run, the Hunter will find classes that are actually used. In this case you can help it by creating a configuration file .unused-class-hunter.php in the root of the project:

<?php

declare(strict_types=1);

use DBublik\UnusedClassHunter\Config;
use Symfony\Component\Finder\Finder;

$finder = Finder::create()
    ->in(__DIR__);

return (new Config())
    ->setFinder($finder);

And the following command:

./vendor/bin/unused-class-hunter check --config=.unused-class-hunter.php

Ignoring classes:

If you want to ignore certain classes, you have three options. The first two are to specify the classes or the attributes of these classes in the config:

<?php

declare(strict_types=1);

use DBublik\UnusedClassHunter\Config;
use Symfony\Component\Finder\Finder;

$finder = Finder::create()
    ->in(__DIR__);

return (new Config())
    ->setFinder($finder)
    ->withIgnoredClasses(
        \ExampleNamespace\FirstExampleClass::class,
        \ExampleNamespace\SecondExampleClass::class,
    )
    ->withIgnoredAttributes(
        \ExampleNamespace\Attribute\ExampleAttribute::class,
    );

The third option is to use one of our "filter". In the engine of this project we use two filters - ClassFilter and AttributeFilter.

But there are other filters - for example ApiTagFilter (which filters classes with the tag @api). See all filters in directory DBublik\UnusedClassHunter\Filter.

Or you can create your own custom filter:

<?php

declare(strict_types=1);

namespace ExampleNamespace\Filter;

use DBublik\UnusedClassHunter\Filter\FilterInterface;
use DBublik\UnusedClassHunter\ValueObject\ClassNode;
use DBublik\UnusedClassHunter\ValueObject\ReaderResult;

final readonly class ExampleFilter implements FilterInterface
{
    #[\Override]
    public function isIgnored(ClassNode $class, ReaderResult $reader): bool
    {
        return str_starts_with($class->getName(), 'BadName');
    }
}
<?php

declare(strict_types=1);

use DBublik\UnusedClassHunter\Config;
use Symfony\Component\Finder\Finder;

$finder = Finder::create()
    ->in(__DIR__);

return (new Config())
    ->setFinder($finder)
    ->withFilters(
        new \ExampleNamespace\Filter\ExampleFilter(),
    );

Custom cache directory:

<?php

declare(strict_types=1);

use DBublik\UnusedClassHunter\Config;
use Symfony\Component\Finder\Finder;

$finder = Finder::create()
    ->in(__DIR__);

return (new Config())
    ->setFinder($finder)
    ->setCacheDir(__DIR__ . '/var/cache');

Sets for some libraries:

The Hunter contains several predefined sets for different libraries, which can be enabled with this config:

<?php

declare(strict_types=1);

use DBublik\UnusedClassHunter\Config;
use Symfony\Component\Finder\Finder;

$finder = Finder::create()
    ->in(__DIR__);

return (new Config())
    ->setFinder($finder)
    ->withSets(
        symfony: true,
        doctrine: true,
        twig: true,
        phpunit: true,
        codeception: true,
    );

Supported PHP versions

PHP 8.3 and later.

Releases

No releases published

Packages

No packages published

Languages